make phone calls

This commit is contained in:
m5r 2022-06-11 19:29:58 +02:00
parent dbe209c7fc
commit f1702180b7
9 changed files with 49 additions and 24 deletions

View File

@ -4,6 +4,10 @@ import { IoDownloadOutline } from "react-icons/io5";
export default function ServiceWorkerUpdateNotifier() { export default function ServiceWorkerUpdateNotifier() {
const [hasUpdate, setHasUpdate] = useState(false); const [hasUpdate, setHasUpdate] = useState(false);
useEffect(() => { useEffect(() => {
if (!("serviceWorker" in navigator)) {
return;
}
(async () => { (async () => {
const registration = await navigator.serviceWorker.getRegistration(); const registration = await navigator.serviceWorker.getRegistration();
if (!registration) { if (!registration) {

View File

@ -1,26 +1,26 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { type TwilioError, Call, Device } from "@twilio/voice-sdk";
import { useFetcher } from "@remix-run/react"; import { useFetcher } from "@remix-run/react";
import { type TwilioError, Call, Device } from "@twilio/voice-sdk";
import { useAtom, atom } from "jotai";
import type { TwilioTokenLoaderData } from "~/features/phone-calls/loaders/twilio-token"; import type { TwilioTokenLoaderData } from "~/features/phone-calls/loaders/twilio-token";
export default function useDevice() { export default function useDevice() {
const jwt = useDeviceToken(); const jwt = useDeviceToken();
const [device, setDevice] = useState<Device | null>(null); const [device, setDevice] = useAtom(deviceAtom);
const [isDeviceReady, setIsDeviceReady] = useState(() => device?.state === Device.State.Registered); const [isDeviceReady, setIsDeviceReady] = useState(device?.state === Device.State.Registered);
useEffect(() => { useEffect(() => {
// init token
jwt.refresh(); jwt.refresh();
}, []); }, []);
useEffect(() => { useEffect(() => {
if (jwt.token && device?.state === Device.State.Registered && device?.token !== jwt.token) { // init device
device.updateToken(jwt.token); if (!jwt.token) {
return;
} }
}, [jwt.token, device]); if (device && device.state !== Device.State.Unregistered) {
useEffect(() => {
if (!jwt.token || device?.state === Device.State.Registered) {
return; return;
} }
@ -30,9 +30,16 @@ export default function useDevice() {
[Device.SoundName.Disconnect]: undefined, // TODO [Device.SoundName.Disconnect]: undefined, // TODO
}, },
}); });
newDevice.register(); // TODO throwing an error newDevice.register();
setDevice(newDevice); setDevice(newDevice);
}, [device?.state, jwt.token, setDevice]); }, [device, jwt.token]);
useEffect(() => {
// refresh token
if (jwt.token && device?.state === Device.State.Registered && device?.token !== jwt.token) {
device.updateToken(jwt.token);
}
}, [device, jwt.token]);
useEffect(() => { useEffect(() => {
if (!device) { if (!device) {
@ -100,6 +107,8 @@ export default function useDevice() {
} }
} }
const deviceAtom = atom<Device | null>(null);
function useDeviceToken() { function useDeviceToken() {
const fetcher = useFetcher<TwilioTokenLoaderData>(); const fetcher = useFetcher<TwilioTokenLoaderData>();

View File

@ -32,6 +32,7 @@ export default function useMakeCall({ recipient, onHangUp }: Params) {
const makeCall = useCallback( const makeCall = useCallback(
async function makeCall() { async function makeCall() {
console.log({ device, isDeviceReady });
if (!device || !isDeviceReady) { if (!device || !isDeviceReady) {
console.warn("device is not ready yet, can't make the call"); console.warn("device is not ready yet, can't make the call");
return; return;

View File

@ -2,7 +2,7 @@ import { type LoaderFunction } from "@remix-run/node";
import Twilio from "twilio"; import Twilio from "twilio";
import { refreshSessionData, requireLoggedIn } from "~/utils/auth.server"; import { refreshSessionData, requireLoggedIn } from "~/utils/auth.server";
import { encrypt } from "~/utils/encryption"; import { decrypt, encrypt } from "~/utils/encryption";
import db from "~/utils/db.server"; import db from "~/utils/db.server";
import { commitSession } from "~/utils/session.server"; import { commitSession } from "~/utils/session.server";
import getTwilioClient from "~/utils/twilio.server"; import getTwilioClient from "~/utils/twilio.server";
@ -10,7 +10,7 @@ import getTwilioClient from "~/utils/twilio.server";
export type TwilioTokenLoaderData = string; export type TwilioTokenLoaderData = string;
const loader: LoaderFunction = async ({ request }) => { const loader: LoaderFunction = async ({ request }) => {
const { user, organization, twilio } = await requireLoggedIn(request); const { user, twilio } = await requireLoggedIn(request);
if (!twilio) { if (!twilio) {
throw new Error("unreachable"); throw new Error("unreachable");
} }
@ -39,15 +39,15 @@ const loader: LoaderFunction = async ({ request }) => {
shouldRefreshSession = true; shouldRefreshSession = true;
const apiKey = await twilioClient.newKeys.create({ friendlyName: "Shellphone" }); const apiKey = await twilioClient.newKeys.create({ friendlyName: "Shellphone" });
apiKeySid = apiKey.sid; apiKeySid = apiKey.sid;
apiKeySecret = apiKey.secret; apiKeySecret = encrypt(apiKey.secret);
await db.twilioAccount.update({ await db.twilioAccount.update({
where: { accountSid: twilioAccount.accountSid }, where: { accountSid: twilioAccount.accountSid },
data: { apiKeySid: apiKey.sid, apiKeySecret: encrypt(apiKey.secret) }, data: { apiKeySid, apiKeySecret },
}); });
} }
const accessToken = new Twilio.jwt.AccessToken(twilioAccount.accountSid, apiKeySid, apiKeySecret, { const accessToken = new Twilio.jwt.AccessToken(twilioAccount.accountSid, apiKeySid, decrypt(apiKeySecret), {
identity: `${organization.id}__${user.id}`, identity: `${twilio.accountSid}__${user.id}`,
ttl: 3600, ttl: 3600,
}); });
const grant = new Twilio.jwt.AccessToken.VoiceGrant({ const grant = new Twilio.jwt.AccessToken.VoiceGrant({

View File

@ -1,4 +1,4 @@
import { useCallback } from "react"; import { useCallback, useEffect } from "react";
import type { MetaFunction } from "@remix-run/node"; import type { MetaFunction } from "@remix-run/node";
import { useParams } from "@remix-run/react"; import { useParams } from "@remix-run/react";
import { IoCall } from "react-icons/io5"; import { IoCall } from "react-icons/io5";
@ -38,6 +38,12 @@ export default function OutgoingCallPage() {
[call, pressDigit], [call, pressDigit],
); );
useEffect(() => {
if (isDeviceReady) {
call.makeCall();
}
}, [call, isDeviceReady]);
return ( return (
<div className="w-96 h-full flex flex-col justify-around py-5 mx-auto text-center text-black bg-white"> <div className="w-96 h-full flex flex-col justify-around py-5 mx-auto text-center text-black bg-white">
<div className="h-16 text-3xl text-gray-700"> <div className="h-16 text-3xl text-gray-700">

View File

@ -15,14 +15,14 @@ export const action: ActionFunction = async ({ request }) => {
return badRequest("Invalid header X-Twilio-Signature"); return badRequest("Invalid header X-Twilio-Signature");
} }
const body: Body = await request.json(); const body: Body = Object.fromEntries(await request.formData()) as any;
const isOutgoingCall = body.From.startsWith("client:"); const isOutgoingCall = body.From.startsWith("client:");
if (isOutgoingCall) { if (isOutgoingCall) {
const recipient = body.To; const recipient = body.To;
const organizationId = body.From.slice("client:".length).split("__")[0]; const accountSid = body.From.slice("client:".length).split("__")[0];
try { try {
const twilioAccount = await db.twilioAccount.findUnique({ where: { organizationId } }); const twilioAccount = await db.twilioAccount.findUnique({ where: { accountSid } });
if (!twilioAccount) { if (!twilioAccount) {
// this shouldn't be happening // this shouldn't be happening
return new Response(null, { status: 402 }); return new Response(null, { status: 402 });
@ -57,7 +57,8 @@ export const action: ActionFunction = async ({ request }) => {
if (phoneNumber?.twilioAccount.organization.subscriptions.length === 0) { if (phoneNumber?.twilioAccount.organization.subscriptions.length === 0) {
// decline the outgoing call because // decline the outgoing call because
// the organization is on the free plan // the organization is on the free plan
return new Response(null, { status: 402 }); console.log("no active subscription"); // TODO: uncomment the line below
// return new Response(null, { status: 402 });
} }
const encryptedAuthToken = phoneNumber?.twilioAccount.authToken; const encryptedAuthToken = phoneNumber?.twilioAccount.authToken;

View File

@ -59,6 +59,10 @@ const lastTimeRevalidated: Record<string, number> = {};
export function fetchLoaderData(event: FetchEvent): Promise<Response> { export function fetchLoaderData(event: FetchEvent): Promise<Response> {
const url = new URL(event.request.url); const url = new URL(event.request.url);
if (url.pathname === "/outgoing-call/twilio-token") {
return fetch(event.request);
}
const path = url.pathname + url.search; const path = url.pathname + url.search;
return caches.match(event.request, { cacheName: DATA_CACHE }).then((cachedResponse) => { return caches.match(event.request, { cacheName: DATA_CACHE }).then((cachedResponse) => {

View File

@ -17,9 +17,9 @@ export default function getTwilioClient({
return twilio(accountSid, decrypt(authToken)); return twilio(accountSid, decrypt(authToken));
} }
export const smsUrl = `https://${serverConfig.app.baseUrl}/webhook/message`; export const smsUrl = `${serverConfig.app.baseUrl}/webhook/message`;
export const voiceUrl = `https://${serverConfig.app.baseUrl}/webhook/call`; export const voiceUrl = `${serverConfig.app.baseUrl}/webhook/call`;
export function getTwiMLName() { export function getTwiMLName() {
switch (serverConfig.app.baseUrl) { switch (serverConfig.app.baseUrl) {