2021-07-31 17:22:48 +00:00
|
|
|
import type { BlitzApiRequest, BlitzApiResponse } 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 type { ApiError } from "../../../api/_types";
|
|
|
|
import appLogger from "../../../../integrations/logger";
|
|
|
|
import { encrypt } from "../../../../db/_encryption";
|
|
|
|
import db, { Direction, MessageStatus } from "../../../../db";
|
|
|
|
import { MessageInstance } from "twilio/lib/rest/api/v2010/account/message";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
const logger = appLogger.child({ route: "/api/webhook/incoming-message" });
|
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-07-31 15:57:43 +00:00
|
|
|
console.log("req.body", req.body);
|
2021-08-01 10:36:32 +00:00
|
|
|
// TODO: return 200 and process this in the background
|
2021-07-31 14:33:18 +00:00
|
|
|
try {
|
2021-07-31 15:57:43 +00:00
|
|
|
const phoneNumber = req.body.To;
|
2021-07-31 14:33:18 +00:00
|
|
|
const customerPhoneNumber = await db.phoneNumber.findFirst({
|
|
|
|
where: { phoneNumber },
|
2021-07-31 15:57:43 +00:00
|
|
|
});
|
2021-08-01 10:36:32 +00:00
|
|
|
console.log("customerPhoneNumber", customerPhoneNumber);
|
|
|
|
if (!customerPhoneNumber) {
|
|
|
|
// phone number is not registered by any customer
|
|
|
|
res.status(200).end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-31 14:33:18 +00:00
|
|
|
const customer = await db.customer.findFirst({
|
2021-08-01 10:36:32 +00:00
|
|
|
where: { id: customerPhoneNumber.customerId },
|
2021-07-31 15:57:43 +00:00
|
|
|
});
|
2021-08-01 10:36:32 +00:00
|
|
|
console.log("customer", customer);
|
|
|
|
if (!customer || !customer.authToken) {
|
|
|
|
res.status(200).end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = "https://4cbc3f38c23a.ngrok.io/api/webhook/incoming-message";
|
2021-07-31 14:33:18 +00:00
|
|
|
const isRequestValid = twilio.validateRequest(
|
2021-08-01 10:36:32 +00:00
|
|
|
customer.authToken,
|
2021-07-31 14:33:18 +00:00
|
|
|
twilioSignature,
|
|
|
|
url,
|
|
|
|
req.body
|
2021-07-31 15:57:43 +00:00
|
|
|
);
|
2021-08-01 10:36:32 +00:00
|
|
|
console.log("isRequestValid", isRequestValid);
|
2021-07-31 14:33:18 +00:00
|
|
|
if (!isRequestValid) {
|
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
|
|
|
// TODO: send notification
|
|
|
|
|
|
|
|
const body: Body = req.body;
|
|
|
|
const messageSid = body.MessageSid;
|
|
|
|
const message = await twilio(customer.accountSid!, customer.authToken)
|
|
|
|
.messages.get(messageSid)
|
|
|
|
.fetch();
|
2021-07-31 14:33:18 +00:00
|
|
|
await db.message.create({
|
|
|
|
data: {
|
2021-08-01 10:36:32 +00:00
|
|
|
customerId: customer.id,
|
|
|
|
to: message.to,
|
|
|
|
from: message.from,
|
|
|
|
status: translateStatus(message.status),
|
|
|
|
direction: translateDirection(message.direction),
|
|
|
|
sentAt: message.dateCreated,
|
|
|
|
content: encrypt(message.body, customer.encryptionKey),
|
2021-07-31 14:33:18 +00:00
|
|
|
},
|
2021-07-31 15:57:43 +00:00
|
|
|
});
|
2021-07-31 14:33:18 +00:00
|
|
|
} catch (error) {
|
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;
|
|
|
|
};
|
|
|
|
|
2021-07-31 14:33:18 +00:00
|
|
|
function translateDirection(direction: MessageInstance["direction"]): Direction {
|
|
|
|
switch (direction) {
|
|
|
|
case "inbound":
|
2021-07-31 15:57:43 +00:00
|
|
|
return Direction.Inbound;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "outbound-api":
|
|
|
|
case "outbound-call":
|
|
|
|
case "outbound-reply":
|
|
|
|
default:
|
2021-07-31 15:57:43 +00:00
|
|
|
return Direction.Outbound;
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function translateStatus(status: MessageInstance["status"]): MessageStatus {
|
|
|
|
switch (status) {
|
|
|
|
case "accepted":
|
2021-07-31 15:57:43 +00:00
|
|
|
return MessageStatus.Accepted;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "canceled":
|
2021-07-31 15:57:43 +00:00
|
|
|
return MessageStatus.Canceled;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "delivered":
|
2021-07-31 15:57:43 +00:00
|
|
|
return MessageStatus.Delivered;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "failed":
|
2021-07-31 15:57:43 +00:00
|
|
|
return MessageStatus.Failed;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "partially_delivered":
|
2021-07-31 15:57:43 +00:00
|
|
|
return MessageStatus.PartiallyDelivered;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "queued":
|
2021-07-31 15:57:43 +00:00
|
|
|
return MessageStatus.Queued;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "read":
|
2021-07-31 15:57:43 +00:00
|
|
|
return MessageStatus.Read;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "received":
|
2021-07-31 15:57:43 +00:00
|
|
|
return MessageStatus.Received;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "receiving":
|
2021-07-31 15:57:43 +00:00
|
|
|
return MessageStatus.Receiving;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "scheduled":
|
2021-07-31 15:57:43 +00:00
|
|
|
return MessageStatus.Scheduled;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "sending":
|
2021-07-31 15:57:43 +00:00
|
|
|
return MessageStatus.Sending;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "sent":
|
2021-07-31 15:57:43 +00:00
|
|
|
return MessageStatus.Sent;
|
2021-07-31 14:33:18 +00:00
|
|
|
case "undelivered":
|
2021-07-31 15:57:43 +00:00
|
|
|
return MessageStatus.Undelivered;
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
|
|
|
}
|