2021-07-31 17:22:48 +00:00
|
|
|
import type { BlitzApiRequest, BlitzApiResponse } from "blitz";
|
2021-08-01 10:54:51 +00:00
|
|
|
import { getConfig } from "blitz";
|
2021-07-31 15:57:43 +00:00
|
|
|
import twilio from "twilio";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
import appLogger from "../../../../integrations/logger";
|
2021-08-01 10:54:51 +00:00
|
|
|
import db from "../../../../db";
|
|
|
|
import insertIncomingMessageQueue from "../queue/insert-incoming-message";
|
2021-08-30 11:24:05 +00:00
|
|
|
import { smsUrl } from "../../../../integrations/twilio";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-08-27 21:09:45 +00:00
|
|
|
type ApiError = {
|
|
|
|
statusCode: number;
|
|
|
|
errorMessage: string;
|
|
|
|
};
|
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
const logger = appLogger.child({ route: "/api/webhook/incoming-message" });
|
2021-08-01 10:54:51 +00:00
|
|
|
const { serverRuntimeConfig } = getConfig();
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 17:22:48 +00:00
|
|
|
export default async function incomingMessageHandler(req: BlitzApiRequest, res: BlitzApiResponse) {
|
2021-07-31 14:33:18 +00:00
|
|
|
if (req.method !== "POST") {
|
2021-07-31 15:57:43 +00:00
|
|
|
const statusCode = 405;
|
2021-07-31 14:33:18 +00:00
|
|
|
const apiError: ApiError = {
|
|
|
|
statusCode,
|
|
|
|
errorMessage: `Method ${req.method} Not Allowed`,
|
2021-07-31 15:57:43 +00:00
|
|
|
};
|
|
|
|
logger.error(apiError);
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
res.setHeader("Allow", ["POST"]);
|
|
|
|
res.status(statusCode).send(apiError);
|
|
|
|
return;
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
const twilioSignature = req.headers["X-Twilio-Signature"] || req.headers["x-twilio-signature"];
|
2021-07-31 14:33:18 +00:00
|
|
|
if (!twilioSignature || Array.isArray(twilioSignature)) {
|
2021-07-31 15:57:43 +00:00
|
|
|
const statusCode = 400;
|
2021-07-31 14:33:18 +00:00
|
|
|
const apiError: ApiError = {
|
|
|
|
statusCode,
|
|
|
|
errorMessage: "Invalid header X-Twilio-Signature",
|
2021-07-31 15:57:43 +00:00
|
|
|
};
|
|
|
|
logger.error(apiError);
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
res.status(statusCode).send(apiError);
|
|
|
|
return;
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 10:54:51 +00:00
|
|
|
const body: Body = req.body;
|
2021-07-31 14:33:18 +00:00
|
|
|
try {
|
2021-08-05 17:07:15 +00:00
|
|
|
const phoneNumbers = await db.phoneNumber.findMany({
|
|
|
|
where: { number: body.To },
|
|
|
|
include: { organization: true },
|
2021-07-31 15:57:43 +00:00
|
|
|
});
|
2021-08-05 17:07:15 +00:00
|
|
|
if (phoneNumbers.length === 0) {
|
|
|
|
// phone number is not registered by any organization
|
2021-08-02 13:43:27 +00:00
|
|
|
res.status(500).end();
|
2021-08-01 10:36:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-05 17:07:15 +00:00
|
|
|
const phoneNumber = phoneNumbers.find((phoneNumber) => {
|
|
|
|
// if multiple organizations have the same number
|
|
|
|
// find the organization currently using that phone number
|
|
|
|
// maybe we shouldn't let multiple organizations use the same phone number
|
|
|
|
const authToken = phoneNumber.organization.twilioAuthToken ?? "";
|
2021-08-30 11:24:05 +00:00
|
|
|
return twilio.validateRequest(authToken, twilioSignature, smsUrl, req.body);
|
2021-08-05 17:07:15 +00:00
|
|
|
});
|
|
|
|
if (!phoneNumber) {
|
2021-07-31 15:57:43 +00:00
|
|
|
const statusCode = 400;
|
2021-07-31 14:33:18 +00:00
|
|
|
const apiError: ApiError = {
|
|
|
|
statusCode,
|
|
|
|
errorMessage: "Invalid webhook",
|
2021-07-31 15:57:43 +00:00
|
|
|
};
|
|
|
|
logger.error(apiError);
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
res.status(statusCode).send(apiError);
|
|
|
|
return;
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 10:36:32 +00:00
|
|
|
const messageSid = body.MessageSid;
|
2021-08-05 17:07:15 +00:00
|
|
|
const organizationId = phoneNumber.organization.id;
|
|
|
|
const phoneNumberId = phoneNumber.id;
|
|
|
|
await insertIncomingMessageQueue.enqueue(
|
|
|
|
{
|
|
|
|
messageSid,
|
|
|
|
organizationId,
|
|
|
|
phoneNumberId,
|
|
|
|
},
|
|
|
|
{ id: `insert-${messageSid}-${organizationId}-${phoneNumberId}` },
|
|
|
|
);
|
2021-08-01 10:54:51 +00:00
|
|
|
|
2021-08-02 13:43:27 +00:00
|
|
|
res.setHeader("content-type", "text/html");
|
|
|
|
res.status(200).send("<Response></Response>");
|
2021-08-27 18:05:44 +00:00
|
|
|
} catch (error: any) {
|
2021-07-31 15:57:43 +00:00
|
|
|
const statusCode = error.statusCode ?? 500;
|
2021-07-31 14:33:18 +00:00
|
|
|
const apiError: ApiError = {
|
|
|
|
statusCode,
|
|
|
|
errorMessage: error.message,
|
2021-07-31 15:57:43 +00:00
|
|
|
};
|
|
|
|
logger.error(error);
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
res.status(statusCode).send(apiError);
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 10:36:32 +00:00
|
|
|
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;
|
|
|
|
};
|