Skip to content

Commit 053d22d

Browse files
committed
feat: implement WhatsApp template message functionality in Twilio service
1 parent c9f618d commit 053d22d

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/services/inviteService.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { PrismaClient, RSVP } from '@prisma/client';
22
import { getUserByPhoneNumber } from './userService';
33
import { isEventHostOrCoHost } from './guestService';
44
import { getRsvpPreferencesForGroup } from './rsvpPreferencesService';
5-
import { sendWhatsappMessage } from './twilioService';
5+
import { sendWhatsappMessage, sendWhatsappTemplateMessage } from './twilioService';
66

77
const prisma = new PrismaClient();
88

@@ -1148,7 +1148,10 @@ export const sendWhatsappToNoResponseGuests = async (
11481148
const sent: any[] = [];
11491149
const failed: any[] = [];
11501150

1151-
// Send WhatsApp message to each guest
1151+
// Template SID for approved WhatsApp template
1152+
const templateSid = 'HX65b36e43b753b99847c6a3058bfe5d32';
1153+
1154+
// Send WhatsApp message to each guest using template
11521155
for (const guest of noResponseGuests) {
11531156
try {
11541157
// Skip guests without phone numbers (already filtered but double-check)
@@ -1161,8 +1164,19 @@ export const sendWhatsappToNoResponseGuests = async (
11611164
continue;
11621165
}
11631166

1164-
const message = `Hello ${guest.name}, you are invited to ${eventName}. Please RSVP here: ${linkResult.inviteLink}`;
1165-
const sendResult = await sendWhatsappMessage(guest.phone_no, message);
1167+
// Prepare template variables
1168+
// Template format: "Hello {{1}}, you are invited to {{2}}. Please RSVP here: {{3}}. Thank you!"
1169+
const contentVariables = {
1170+
'1': guest.name || 'Guest',
1171+
'2': eventName,
1172+
'3': linkResult.inviteLink,
1173+
};
1174+
1175+
const sendResult = await sendWhatsappTemplateMessage(
1176+
guest.phone_no,
1177+
templateSid,
1178+
contentVariables
1179+
);
11661180

11671181
if (sendResult.success) {
11681182
sent.push({ name: guest.name, phone_no: guest.phone_no, id: guest.id });

src/services/twilioService.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,28 @@ export const sendWhatsappMessage = async (to: string, body: string, mediaUrl?: s
4949
return { success: false, error };
5050
}
5151
};
52+
53+
export const sendWhatsappTemplateMessage = async (
54+
to: string,
55+
contentSid: string,
56+
contentVariables: { [key: string]: string }
57+
) => {
58+
try {
59+
console.log('[twilioService.sendWhatsappTemplateMessage] Sending template message to:', to);
60+
console.log('[twilioService.sendWhatsappTemplateMessage] ContentSid:', contentSid);
61+
console.log('[twilioService.sendWhatsappTemplateMessage] Variables:', contentVariables);
62+
63+
const message = await client.messages.create({
64+
from: `whatsapp:${twilioWhatsappNumber}`,
65+
to: `whatsapp:${to}`,
66+
contentSid: contentSid,
67+
contentVariables: JSON.stringify(contentVariables),
68+
});
69+
70+
console.log('[twilioService.sendWhatsappTemplateMessage] Template message sent successfully, SID:', message.sid);
71+
return { success: true, sid: message.sid };
72+
} catch (error) {
73+
console.error('[twilioService.sendWhatsappTemplateMessage] Error sending WhatsApp template message via Twilio:', error);
74+
return { success: false, error };
75+
}
76+
};

0 commit comments

Comments
 (0)