Files
cal.diy/packages/features/ee/workflows/lib/reminders/smsProviders/twilioProvider.ts
T
54f4e665a3 Add alphanumeric sender ID to SMS workflow actions (#5471)
* add sender id

* add sender to twilio from

* added missing sender

* add migration

* fix design of add action dialog

* add cal as sender when creating new workflow

* fix type errors

Co-authored-by: CarinaWolli <[email protected]>
Co-authored-by: Alex van Andel <[email protected]>
2022-11-11 15:01:17 +00:00

52 lines
1.4 KiB
TypeScript

import TwilioClient from "twilio";
declare global {
// eslint-disable-next-line no-var
var twilio: TwilioClient.Twilio | undefined;
}
export const twilio =
globalThis.twilio ||
(process.env.TWILIO_SID && process.env.TWILIO_TOKEN && process.env.TWILIO_MESSAGING_SID)
? TwilioClient(process.env.TWILIO_SID, process.env.TWILIO_TOKEN)
: undefined;
if (process.env.NODE_ENV !== "production") {
globalThis.twilio = twilio;
}
function assertTwilio(twilio: TwilioClient.Twilio | undefined): asserts twilio is TwilioClient.Twilio {
if (!twilio) throw new Error("Twilio credentials are missing from the .env file");
}
export const sendSMS = async (phoneNumber: string, body: string, sender: string) => {
assertTwilio(twilio);
const response = await twilio.messages.create({
body: body,
messagingServiceSid: process.env.TWILIO_MESSAGING_SID,
to: phoneNumber,
from: sender,
});
return response;
};
export const scheduleSMS = async (phoneNumber: string, body: string, scheduledDate: Date, sender: string) => {
assertTwilio(twilio);
const response = await twilio.messages.create({
body: body,
messagingServiceSid: process.env.TWILIO_MESSAGING_SID,
to: phoneNumber,
scheduleType: "fixed",
sendAt: scheduledDate,
from: sender,
});
return response;
};
export const cancelSMS = async (referenceId: string) => {
assertTwilio(twilio);
await twilio.messages(referenceId).update({ status: "canceled" });
};