Skip to content

Instantly share code, notes, and snippets.

@IgorHalfeld
Last active January 20, 2024 21:07
Show Gist options
  • Save IgorHalfeld/ef8bc20d8a6bc344b6366207a18a2803 to your computer and use it in GitHub Desktop.
Save IgorHalfeld/ef8bc20d8a6bc344b6366207a18a2803 to your computer and use it in GitHub Desktop.
import twilio from 'twilio'
import { MessageListInstanceCreateOptions } from 'twilio/lib/rest/api/v2010/account/message'
interface SendWhatsappMessageOptions {
to: string
from: string
body?: string
contentSid?: string
contentVariables?: Record<string, string>
accountSid: string
authtoken: string
}
export const sendWhatsappMessage = async (options: SendWhatsappMessageOptions) => {
const client = twilio(options.accountSid, options.authtoken)
options.to = `whatsapp:${options.to}`
/*
* should on this way if not using twilio message service
* options.from = `whatsapp:${options.from}`
*/
const payload: MessageListInstanceCreateOptions = {
to: options.to,
from: options.from,
contentSid: options.contentSid,
contentVariables: options.contentVariables && JSON.stringify(options.contentVariables),
}
const message = await client.messages.create(payload)
return message
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment