shellphone.app/app/messages/api/webhook/incoming-message.ts

132 lines
3.4 KiB
TypeScript
Raw Normal View History

2021-07-31 17:22:48 +00:00
import type { BlitzApiRequest, BlitzApiResponse } from "blitz";
import twilio from "twilio";
2021-07-31 14:33:18 +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
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") {
const statusCode = 405;
2021-07-31 14:33:18 +00:00
const apiError: ApiError = {
statusCode,
errorMessage: `Method ${req.method} Not Allowed`,
};
logger.error(apiError);
2021-07-31 14:33:18 +00:00
res.setHeader("Allow", ["POST"]);
res.status(statusCode).send(apiError);
return;
2021-07-31 14:33:18 +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)) {
const statusCode = 400;
2021-07-31 14:33:18 +00:00
const apiError: ApiError = {
statusCode,
errorMessage: "Invalid header X-Twilio-Signature",
};
logger.error(apiError);
2021-07-31 14:33:18 +00:00
res.status(statusCode).send(apiError);
return;
2021-07-31 14:33:18 +00:00
}
console.log("req.body", req.body);
2021-07-31 14:33:18 +00:00
try {
const phoneNumber = req.body.To;
2021-07-31 14:33:18 +00:00
const customerPhoneNumber = await db.phoneNumber.findFirst({
where: { phoneNumber },
});
2021-07-31 14:33:18 +00:00
const customer = await db.customer.findFirst({
where: { id: customerPhoneNumber!.customerId },
});
const url = "https://phone.mokhtar.dev/api/webhook/incoming-message";
2021-07-31 14:33:18 +00:00
const isRequestValid = twilio.validateRequest(
customer!.authToken!,
twilioSignature,
url,
req.body
);
2021-07-31 14:33:18 +00:00
if (!isRequestValid) {
const statusCode = 400;
2021-07-31 14:33:18 +00:00
const apiError: ApiError = {
statusCode,
errorMessage: "Invalid webhook",
};
logger.error(apiError);
2021-07-31 14:33:18 +00:00
res.status(statusCode).send(apiError);
return;
2021-07-31 14:33:18 +00:00
}
await db.message.create({
data: {
customerId: customer!.id,
to: req.body.To,
from: req.body.From,
status: MessageStatus.Received,
direction: Direction.Inbound,
sentAt: req.body.DateSent,
content: encrypt(req.body.Body, customer!.encryptionKey),
},
});
2021-07-31 14:33:18 +00:00
} catch (error) {
const statusCode = error.statusCode ?? 500;
2021-07-31 14:33:18 +00:00
const apiError: ApiError = {
statusCode,
errorMessage: error.message,
};
logger.error(error);
2021-07-31 14:33:18 +00:00
res.status(statusCode).send(apiError);
2021-07-31 14:33:18 +00:00
}
}
function translateDirection(direction: MessageInstance["direction"]): Direction {
switch (direction) {
case "inbound":
return Direction.Inbound;
2021-07-31 14:33:18 +00:00
case "outbound-api":
case "outbound-call":
case "outbound-reply":
default:
return Direction.Outbound;
2021-07-31 14:33:18 +00:00
}
}
function translateStatus(status: MessageInstance["status"]): MessageStatus {
switch (status) {
case "accepted":
return MessageStatus.Accepted;
2021-07-31 14:33:18 +00:00
case "canceled":
return MessageStatus.Canceled;
2021-07-31 14:33:18 +00:00
case "delivered":
return MessageStatus.Delivered;
2021-07-31 14:33:18 +00:00
case "failed":
return MessageStatus.Failed;
2021-07-31 14:33:18 +00:00
case "partially_delivered":
return MessageStatus.PartiallyDelivered;
2021-07-31 14:33:18 +00:00
case "queued":
return MessageStatus.Queued;
2021-07-31 14:33:18 +00:00
case "read":
return MessageStatus.Read;
2021-07-31 14:33:18 +00:00
case "received":
return MessageStatus.Received;
2021-07-31 14:33:18 +00:00
case "receiving":
return MessageStatus.Receiving;
2021-07-31 14:33:18 +00:00
case "scheduled":
return MessageStatus.Scheduled;
2021-07-31 14:33:18 +00:00
case "sending":
return MessageStatus.Sending;
2021-07-31 14:33:18 +00:00
case "sent":
return MessageStatus.Sent;
2021-07-31 14:33:18 +00:00
case "undelivered":
return MessageStatus.Undelivered;
2021-07-31 14:33:18 +00:00
}
}