@@ -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