2022-05-14 23:29:51 +00:00
|
|
|
import twilio from "twilio";
|
2022-05-17 23:54:06 +00:00
|
|
|
import type { MessageInstance } from "twilio/lib/rest/api/v2010/account/message";
|
|
|
|
import type { CallInstance } from "twilio/lib/rest/api/v2010/account/call";
|
2022-05-21 19:33:23 +00:00
|
|
|
import { type TwilioAccount, CallStatus, Direction, MessageStatus } from "@prisma/client";
|
2022-05-14 23:29:51 +00:00
|
|
|
|
|
|
|
import serverConfig from "~/config/config.server";
|
2022-06-08 22:33:19 +00:00
|
|
|
import { decrypt } from "~/utils/encryption";
|
2022-05-14 23:29:51 +00:00
|
|
|
|
2022-05-21 19:33:23 +00:00
|
|
|
export default function getTwilioClient({
|
|
|
|
accountSid,
|
2022-06-08 22:33:19 +00:00
|
|
|
authToken,
|
|
|
|
}: Pick<TwilioAccount, "accountSid" | "authToken">): twilio.Twilio {
|
|
|
|
if (!accountSid || !authToken) {
|
2022-05-14 23:29:51 +00:00
|
|
|
throw new Error("unreachable");
|
|
|
|
}
|
|
|
|
|
2022-06-08 22:33:19 +00:00
|
|
|
return twilio(accountSid, decrypt(authToken));
|
2022-05-14 23:29:51 +00:00
|
|
|
}
|
2022-05-17 23:54:06 +00:00
|
|
|
|
2022-06-11 17:29:58 +00:00
|
|
|
export const smsUrl = `${serverConfig.app.baseUrl}/webhook/message`;
|
2022-05-21 19:33:23 +00:00
|
|
|
|
2022-06-11 17:29:58 +00:00
|
|
|
export const voiceUrl = `${serverConfig.app.baseUrl}/webhook/call`;
|
2022-05-21 19:33:23 +00:00
|
|
|
|
|
|
|
export function getTwiMLName() {
|
|
|
|
switch (serverConfig.app.baseUrl) {
|
|
|
|
case "local.shellphone.app":
|
|
|
|
return "Shellphone LOCAL";
|
|
|
|
case "dev.shellphone.app":
|
|
|
|
return "Shellphone DEV";
|
|
|
|
case "www.shellphone.app":
|
|
|
|
return "Shellphone";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 23:54:06 +00:00
|
|
|
export function translateMessageStatus(status: MessageInstance["status"]): MessageStatus {
|
|
|
|
switch (status) {
|
|
|
|
case "accepted":
|
|
|
|
return MessageStatus.Accepted;
|
|
|
|
case "canceled":
|
|
|
|
return MessageStatus.Canceled;
|
|
|
|
case "delivered":
|
|
|
|
return MessageStatus.Delivered;
|
|
|
|
case "failed":
|
|
|
|
return MessageStatus.Failed;
|
|
|
|
case "partially_delivered":
|
|
|
|
return MessageStatus.PartiallyDelivered;
|
|
|
|
case "queued":
|
|
|
|
return MessageStatus.Queued;
|
|
|
|
case "read":
|
|
|
|
return MessageStatus.Read;
|
|
|
|
case "received":
|
|
|
|
return MessageStatus.Received;
|
|
|
|
case "receiving":
|
|
|
|
return MessageStatus.Receiving;
|
|
|
|
case "scheduled":
|
|
|
|
return MessageStatus.Scheduled;
|
|
|
|
case "sending":
|
|
|
|
return MessageStatus.Sending;
|
|
|
|
case "sent":
|
|
|
|
return MessageStatus.Sent;
|
|
|
|
case "undelivered":
|
|
|
|
return MessageStatus.Undelivered;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function translateMessageDirection(direction: MessageInstance["direction"]): Direction {
|
|
|
|
switch (direction) {
|
|
|
|
case "inbound":
|
|
|
|
return Direction.Inbound;
|
|
|
|
case "outbound-api":
|
|
|
|
case "outbound-call":
|
|
|
|
case "outbound-reply":
|
|
|
|
default:
|
|
|
|
return Direction.Outbound;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function translateCallStatus(status: CallInstance["status"]): CallStatus {
|
|
|
|
switch (status) {
|
|
|
|
case "busy":
|
|
|
|
return CallStatus.Busy;
|
|
|
|
case "canceled":
|
|
|
|
return CallStatus.Canceled;
|
|
|
|
case "completed":
|
|
|
|
return CallStatus.Completed;
|
|
|
|
case "failed":
|
|
|
|
return CallStatus.Failed;
|
|
|
|
case "in-progress":
|
|
|
|
return CallStatus.InProgress;
|
|
|
|
case "no-answer":
|
|
|
|
return CallStatus.NoAnswer;
|
|
|
|
case "queued":
|
|
|
|
return CallStatus.Queued;
|
|
|
|
case "ringing":
|
|
|
|
return CallStatus.Ringing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function translateCallDirection(direction: CallInstance["direction"]): Direction {
|
|
|
|
switch (direction) {
|
|
|
|
case "inbound":
|
|
|
|
return Direction.Inbound;
|
|
|
|
case "outbound":
|
|
|
|
default:
|
|
|
|
return Direction.Outbound;
|
|
|
|
}
|
|
|
|
}
|