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

146
app/routes/webhook/call.ts Normal file
View File

@ -0,0 +1,146 @@
import { type ActionFunction } from "@remix-run/node";
import { type CallInstance } from "twilio/lib/rest/api/v2010/account/call";
import { badRequest, serverError } from "remix-utils";
import { Direction, Prisma, SubscriptionStatus } from "@prisma/client";
import logger from "~/utils/logger.server";
import db from "~/utils/db.server";
import twilio from "twilio";
import { voiceUrl, translateCallStatus } from "~/utils/twilio.server";
import { decrypt } from "~/utils/encryption";
export const action: ActionFunction = async ({ request }) => {
const twilioSignature = request.headers.get("X-Twilio-Signature") || request.headers.get("x-twilio-signature");
if (!twilioSignature || Array.isArray(twilioSignature)) {
return badRequest("Invalid header X-Twilio-Signature");
}
const body: Body = Object.fromEntries(await request.formData()) as any;
const isOutgoingCall = body.From.startsWith("client:");
if (isOutgoingCall) {
const recipient = body.To;
const accountSid = body.From.slice("client:".length).split("__")[0];
try {
const twilioAccount = await db.twilioAccount.findUnique({ where: { accountSid } });
if (!twilioAccount) {
// this shouldn't be happening
return new Response(null, { status: 402 });
}
const phoneNumber = await db.phoneNumber.findUnique({
where: { twilioAccountSid_isCurrent: { twilioAccountSid: twilioAccount.accountSid, isCurrent: true } },
include: {
twilioAccount: {
include: {
organization: {
select: {
subscriptions: {
where: {
OR: [
{ status: { not: SubscriptionStatus.deleted } },
{
status: SubscriptionStatus.deleted,
cancellationEffectiveDate: { gt: new Date() },
},
],
},
orderBy: { lastEventTime: Prisma.SortOrder.desc },
},
},
},
},
},
},
});
if (phoneNumber?.twilioAccount.organization.subscriptions.length === 0) {
// decline the outgoing call because
// the organization is on the free plan
console.log("no active subscription"); // TODO: uncomment the line below
// return new Response(null, { status: 402 });
}
const encryptedAuthToken = phoneNumber?.twilioAccount.authToken;
const authToken = encryptedAuthToken ? decrypt(encryptedAuthToken) : "";
if (
!phoneNumber ||
!encryptedAuthToken ||
!twilio.validateRequest(authToken, twilioSignature, voiceUrl, body)
) {
return badRequest("Invalid webhook");
}
await db.phoneCall.create({
data: {
id: body.CallSid,
recipient: body.To,
from: phoneNumber.number,
to: body.To,
status: translateCallStatus(body.CallStatus),
direction: Direction.Outbound,
duration: "0",
phoneNumberId: phoneNumber.id,
},
});
const voiceResponse = new twilio.twiml.VoiceResponse();
const dial = voiceResponse.dial({
answerOnBridge: true,
callerId: phoneNumber!.number,
});
dial.number(recipient);
console.log("twiml voiceResponse", voiceResponse.toString());
return new Response(voiceResponse.toString(), { headers: { "Content-Type": "text/xml" } });
} catch (error: any) {
logger.error(error);
return serverError(error.message);
}
}
};
type OutgoingCallBody = {
AccountSid: string;
ApiVersion: string;
ApplicationSid: string;
CallSid: string;
CallStatus: CallInstance["status"];
Called: string;
Caller: string;
Direction: `outbound${string}`;
From: string;
To: string;
};
type IncomingCallBody = {
AccountSid: string;
ApiVersion: string;
ApplicationSid: string;
CallSid: string;
CallStatus: CallInstance["status"];
Called: string;
CalledCity: string;
CalledCountry: string;
CalledState: string;
CalledZip: string;
Caller: string;
CallerCity: string;
CallerCountry: string;
CallerState: string;
CallerZip: string;
Direction: "inbound";
From: string;
FromCity: string;
FromCountry: string;
FromState: string;
FromZip: string;
To: string;
ToCity: string;
ToCountry: string;
ToState: string;
ToZip: string;
};
type Body = OutgoingCallBody | IncomingCallBody;

View File

@ -0,0 +1,106 @@
import { type ActionFunction } from "@remix-run/node";
import { badRequest, html, notFound, serverError } from "remix-utils";
import { Prisma, SubscriptionStatus } from "@prisma/client";
import insertIncomingMessageQueue from "~/queues/insert-incoming-message.server";
import logger from "~/utils/logger.server";
import db from "~/utils/db.server";
import twilio from "twilio";
import { smsUrl } from "~/utils/twilio.server";
import { decrypt } from "~/utils/encryption";
export const action: ActionFunction = async ({ request }) => {
const twilioSignature = request.headers.get("X-Twilio-Signature") || request.headers.get("x-twilio-signature");
if (!twilioSignature || Array.isArray(twilioSignature)) {
return badRequest("Invalid header X-Twilio-Signature");
}
const body: Body = await request.json();
try {
const phoneNumbers = await db.phoneNumber.findMany({
where: { number: body.To },
include: {
twilioAccount: {
include: {
organization: {
select: {
subscriptions: {
where: {
OR: [
{ status: { not: SubscriptionStatus.deleted } },
{
status: SubscriptionStatus.deleted,
cancellationEffectiveDate: { gt: new Date() },
},
],
},
orderBy: { lastEventTime: Prisma.SortOrder.desc },
},
},
},
},
},
},
});
if (phoneNumbers.length === 0) {
// phone number is not registered by any organization
return notFound("Phone number not found");
}
const phoneNumbersWithActiveSub = phoneNumbers.filter(
(phoneNumber) => phoneNumber.twilioAccount.organization.subscriptions.length > 0,
);
if (phoneNumbersWithActiveSub.length === 0) {
// accept the webhook but don't store incoming message
// because the organization is on the free plan
return html("<Response></Response>");
}
const phoneNumber = phoneNumbersWithActiveSub.find((phoneNumber) => {
// if multiple organizations have the same number
// find the organization currently using that phone number
// maybe we shouldn't let that happen by restricting a phone number to one org?
const encryptedAuthToken = phoneNumber.twilioAccount.authToken;
const authToken = encryptedAuthToken ? decrypt(encryptedAuthToken) : "";
return twilio.validateRequest(authToken, twilioSignature, smsUrl, body);
});
if (!phoneNumber) {
return badRequest("Invalid webhook");
}
const messageSid = body.MessageSid;
const phoneNumberId = phoneNumber.id;
await insertIncomingMessageQueue.add(`insert message ${messageSid} for ${phoneNumberId}`, {
messageSid,
phoneNumberId,
});
return html("<Response></Response>");
} catch (error: any) {
logger.error(error);
return serverError(error.message);
}
};
type Body = {
ToCountry: string;
ToState: string;
SmsMessageSid: string;
NumMedia: string;
ToCity: string;
FromZip: string;
SmsSid: string;
FromState: string;
SmsStatus: string;
FromCity: string;
Body: string;
FromCountry: string;
To: string;
ToZip: string;
NumSegments: string;
MessageSid: string;
AccountSid: string;
From: string;
ApiVersion: string;
};