Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FFDuBhMKvoWjrWT3ZnGmmr
43 lines
956 B
TypeScript
43 lines
956 B
TypeScript
export async function sendWhatsAppMessage(
|
|
phoneNumber: string,
|
|
templateName: string,
|
|
parameters: string[],
|
|
phoneNumberId: string,
|
|
accessToken: string,
|
|
): Promise<void> {
|
|
const sanitized = phoneNumber.replace(/[^0-9]/g, '')
|
|
if (!sanitized) return
|
|
|
|
const body = {
|
|
messaging_product: 'whatsapp',
|
|
to: sanitized,
|
|
type: 'template',
|
|
template: {
|
|
name: templateName,
|
|
language: { code: 'en_US' },
|
|
components: [
|
|
{
|
|
type: 'body',
|
|
parameters: parameters.map(text => ({ type: 'text', text })),
|
|
},
|
|
],
|
|
},
|
|
}
|
|
|
|
const res = await fetch(
|
|
`https://graph.facebook.com/v19.0/${phoneNumberId}/messages`,
|
|
{
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${accessToken}`,
|
|
},
|
|
body: JSON.stringify(body),
|
|
}
|
|
)
|
|
|
|
if (!res.ok) {
|
|
throw new Error(`WhatsApp API error: ${res.status}`)
|
|
}
|
|
}
|