Skip to content

Commit b7aaf00

Browse files
committed
feat: add support for sending WhatsApp template messages with media in Twilio service
1 parent 053d22d commit b7aaf00

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

src/services/inviteService.ts

Lines changed: 23 additions & 7 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, sendWhatsappTemplateMessage } from './twilioService';
5+
import { sendWhatsappMessage, sendWhatsappTemplateMessage, sendWhatsappTemplateMessageWithMedia } from './twilioService';
66

77
const prisma = new PrismaClient();
88

@@ -1584,18 +1584,34 @@ export const sendGroupWhatsappMessage = async (
15841584
failed: [] as any[],
15851585
};
15861586

1587-
// Format the message with title if provided
1588-
let messageBody = body;
1589-
if (title) {
1590-
messageBody = `*${title}*\n\n${body}`;
1591-
}
1587+
// Template SID for approved WhatsApp group message template
1588+
const templateSid = 'HXd4934214cc9eac4940a428b8b4c44df2';
15921589

15931590
for (const guest of groupWithGuests.guests) {
15941591
const phone_no = guest.phone_no || guest.user?.mobile_number;
15951592
const name = guest.user?.name || guest.name;
15961593

15971594
if (phone_no) {
1598-
const result = await sendWhatsappMessage(phone_no, messageBody, mediaUrl);
1595+
// Prepare template variables as an array
1596+
// The variables are indexed starting from 1 in the template
1597+
const bodyVariables: string[] = [];
1598+
1599+
if (title) {
1600+
// If title is provided: {{1}} = title, {{2}} = body
1601+
bodyVariables.push(title);
1602+
bodyVariables.push(body);
1603+
} else {
1604+
// If no title: {{1}} = body
1605+
bodyVariables.push(body);
1606+
}
1607+
1608+
const result = await sendWhatsappTemplateMessageWithMedia(
1609+
phone_no,
1610+
templateSid,
1611+
bodyVariables,
1612+
mediaUrl // Pass media URL separately
1613+
);
1614+
15991615
if (result.success) {
16001616
results.sent.push({ name, phone_no });
16011617
} else {

src/services/twilioService.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,47 @@ export const sendWhatsappTemplateMessage = async (
7474
return { success: false, error };
7575
}
7676
};
77+
78+
export const sendWhatsappTemplateMessageWithMedia = async (
79+
to: string,
80+
contentSid: string,
81+
bodyVariables: string[],
82+
headerMediaUrl?: string
83+
) => {
84+
try {
85+
console.log('[twilioService.sendWhatsappTemplateMessageWithMedia] Sending template message with media to:', to);
86+
console.log('[twilioService.sendWhatsappTemplateMessageWithMedia] ContentSid:', contentSid);
87+
console.log('[twilioService.sendWhatsappTemplateMessageWithMedia] Body Variables:', bodyVariables);
88+
console.log('[twilioService.sendWhatsappTemplateMessageWithMedia] Header Media URL:', headerMediaUrl);
89+
90+
// Build content variables in the correct format for Twilio Content API
91+
const contentVariables: any = {};
92+
93+
// Add body variables (indexed from 1)
94+
bodyVariables.forEach((value, index) => {
95+
contentVariables[`${index + 1}`] = value;
96+
});
97+
98+
console.log('[twilioService.sendWhatsappTemplateMessageWithMedia] Formatted Variables:', JSON.stringify(contentVariables));
99+
100+
const messageOptions: any = {
101+
from: `whatsapp:${twilioWhatsappNumber}`,
102+
to: `whatsapp:${to}`,
103+
contentSid: contentSid,
104+
contentVariables: JSON.stringify(contentVariables),
105+
};
106+
107+
// Add media URL separately if provided
108+
if (headerMediaUrl) {
109+
messageOptions.mediaUrl = [headerMediaUrl];
110+
}
111+
112+
const message = await client.messages.create(messageOptions);
113+
114+
console.log('[twilioService.sendWhatsappTemplateMessageWithMedia] Template message sent successfully, SID:', message.sid);
115+
return { success: true, sid: message.sid };
116+
} catch (error) {
117+
console.error('[twilioService.sendWhatsappTemplateMessageWithMedia] Error sending WhatsApp template message via Twilio:', error);
118+
return { success: false, error };
119+
}
120+
};

0 commit comments

Comments
 (0)