Files
calendar/packages/features/ee/workflows/lib/reminders/providers/twilioProvider.ts
T
076868d243 test: testing workflow triggers (#12823)
* add workflows to bookingScenario

* activate sandbox mode for unit/integreation tests

* add sendgrid specific code to SendgridProvider

* Refactor WIP

* remove duplicate sendgridProvider file

* first implementation for testing workflows

* revert unintended changes

* comment out Workflow trigger tests

* move sendgrid check after test mode

* Update signup.tsx

* fix esLint

* test webhooks on all tests in fresh-booking.test.ts

* fix subjectPattern as title can be different

* add workflow tests to reschedule.test.ts

* code clean up

* code clean up

* fix sendgrid credentials missing message

* code clean up

---------

Co-authored-by: CarinaWolli <wollencarina@gmail.com>
2023-12-21 18:57:06 +05:30

93 lines
2.7 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");
}
function getDefaultSender(whatsapp = false) {
let defaultSender = process.env.TWILIO_PHONE_NUMBER;
if (whatsapp) {
defaultSender = `whatsapp:+${process.env.TWILIO_WHATSAPP_PHONE_NUMBER}`;
}
return defaultSender;
}
function getSMSNumber(phone: string, whatsapp = false) {
return whatsapp ? `whatsapp:${phone}` : phone;
}
export const sendSMS = async (phoneNumber: string, body: string, sender: string, whatsapp = false) => {
assertTwilio(twilio);
const response = await twilio.messages.create({
body: body,
messagingServiceSid: process.env.TWILIO_MESSAGING_SID,
to: getSMSNumber(phoneNumber, whatsapp),
from: whatsapp ? getDefaultSender(whatsapp) : sender ? sender : getDefaultSender(),
});
return response;
};
export const scheduleSMS = async (
phoneNumber: string,
body: string,
scheduledDate: Date,
sender: string,
whatsapp = false
) => {
assertTwilio(twilio);
const response = await twilio.messages.create({
body: body,
messagingServiceSid: process.env.TWILIO_MESSAGING_SID,
to: getSMSNumber(phoneNumber, whatsapp),
scheduleType: "fixed",
sendAt: scheduledDate,
from: whatsapp ? getDefaultSender(whatsapp) : sender ? sender : getDefaultSender(),
});
return response;
};
export const cancelSMS = async (referenceId: string) => {
assertTwilio(twilio);
await twilio.messages(referenceId).update({ status: "canceled" });
};
export const sendVerificationCode = async (phoneNumber: string) => {
assertTwilio(twilio);
if (process.env.TWILIO_VERIFY_SID) {
await twilio.verify
.services(process.env.TWILIO_VERIFY_SID)
.verifications.create({ to: phoneNumber, channel: "sms" });
}
};
export const verifyNumber = async (phoneNumber: string, code: string) => {
assertTwilio(twilio);
if (process.env.TWILIO_VERIFY_SID) {
try {
const verification_check = await twilio.verify.v2
.services(process.env.TWILIO_VERIFY_SID)
.verificationChecks.create({ to: phoneNumber, code: code });
return verification_check.status;
} catch (e) {
return "failed";
}
}
};