forked from calcom/cal.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: New workflow action to send Whatsapp message (calcom#8818)
Co-authored-by: Carina Wollendorfer <30310907+CarinaWolli@users.noreply.github.com> Co-authored-by: CarinaWolli <wollencarina@gmail.com>
- Loading branch information
1 parent
fdef157
commit d58924e
Showing
33 changed files
with
862 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: Cron - scheduleWhatsappReminders | ||
|
||
on: | ||
# "Scheduled workflows run on the latest commit on the default or base branch." | ||
# — https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#schedule | ||
schedule: | ||
# Runs “At minute 0, 15, 30, and 45.” (see https://crontab.guru) | ||
- cron: "0,15,30,45 * * * *" | ||
jobs: | ||
cron-scheduleWhatsappReminders: | ||
env: | ||
APP_URL: ${{ secrets.APP_URL }} | ||
CRON_API_KEY: ${{ secrets.CRON_API_KEY }} | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: cURL request | ||
if: ${{ env.APP_URL && env.CRON_API_KEY }} | ||
run: | | ||
curl ${{ secrets.APP_URL }}/api/cron/workflows/scheduleWhatsappReminders \ | ||
-X POST \ | ||
-H 'content-type: application/json' \ | ||
-H 'authorization: ${{ secrets.CRON_API_KEY }}' \ | ||
--fail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "@calcom/features/ee/workflows/api/scheduleWhatsappReminders"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
packages/features/ee/workflows/api/scheduleWhatsappReminders.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* Schedule any workflow reminder that falls within 7 days for WHATSAPP */ | ||
import { WorkflowActions, WorkflowMethods, WorkflowTemplates } from "@prisma/client"; | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import dayjs from "@calcom/dayjs"; | ||
import { defaultHandler } from "@calcom/lib/server"; | ||
import prisma from "@calcom/prisma"; | ||
|
||
import * as twilio from "../lib/reminders/smsProviders/twilioProvider"; | ||
import { getWhatsappTemplateFunction } from "../lib/actionHelperFunctions"; | ||
|
||
async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
const apiKey = req.headers.authorization || req.query.apiKey; | ||
if (process.env.CRON_API_KEY !== apiKey) { | ||
res.status(401).json({ message: "Not authenticated" }); | ||
return; | ||
} | ||
|
||
//delete all scheduled whatsapp reminders where scheduled date is past current date | ||
await prisma.workflowReminder.deleteMany({ | ||
where: { | ||
method: WorkflowMethods.WHATSAPP, | ||
scheduledDate: { | ||
lte: dayjs().toISOString(), | ||
}, | ||
}, | ||
}); | ||
|
||
//find all unscheduled WHATSAPP reminders | ||
const unscheduledReminders = await prisma.workflowReminder.findMany({ | ||
where: { | ||
method: WorkflowMethods.WHATSAPP, | ||
scheduled: false, | ||
scheduledDate: { | ||
lte: dayjs().add(7, "day").toISOString(), | ||
}, | ||
}, | ||
include: { | ||
workflowStep: true, | ||
booking: { | ||
include: { | ||
eventType: true, | ||
user: true, | ||
attendees: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
if (!unscheduledReminders.length) res.json({ ok: true }); | ||
|
||
for (const reminder of unscheduledReminders) { | ||
if (!reminder.workflowStep || !reminder.booking) { | ||
continue; | ||
} | ||
try { | ||
const sendTo = | ||
reminder.workflowStep.action === WorkflowActions.WHATSAPP_NUMBER | ||
? reminder.workflowStep.sendTo | ||
: reminder.booking?.smsReminderNumber; | ||
|
||
const userName = | ||
reminder.workflowStep.action === WorkflowActions.WHATSAPP_ATTENDEE | ||
? reminder.booking?.attendees[0].name | ||
: ""; | ||
|
||
const attendeeName = | ||
reminder.workflowStep.action === WorkflowActions.WHATSAPP_ATTENDEE | ||
? reminder.booking?.user?.name | ||
: reminder.booking?.attendees[0].name; | ||
|
||
const timeZone = | ||
reminder.workflowStep.action === WorkflowActions.WHATSAPP_ATTENDEE | ||
? reminder.booking?.attendees[0].timeZone | ||
: reminder.booking?.user?.timeZone; | ||
|
||
const templateFunction = getWhatsappTemplateFunction(reminder.workflowStep.template) | ||
const message = templateFunction( | ||
false, | ||
reminder.workflowStep.action, | ||
reminder.booking?.startTime.toISOString() || "", | ||
reminder.booking?.eventType?.title || "", | ||
timeZone || "", | ||
attendeeName || "", | ||
userName | ||
); | ||
|
||
if (message?.length && message?.length > 0 && sendTo) { | ||
const scheduledSMS = await twilio.scheduleSMS(sendTo, message, reminder.scheduledDate, "", true); | ||
|
||
await prisma.workflowReminder.update({ | ||
where: { | ||
id: reminder.id, | ||
}, | ||
data: { | ||
scheduled: true, | ||
referenceId: scheduledSMS.sid, | ||
}, | ||
}); | ||
} | ||
} catch (error) { | ||
console.log(`Error scheduling WHATSAPP with error ${error}`); | ||
} | ||
} | ||
|
||
res.status(200).json({ message: "WHATSAPP scheduled" }); | ||
} | ||
|
||
export default defaultHandler({ | ||
POST: Promise.resolve({ default: handler }), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.