//native
export type DynSelect_template_name = string
// Resolver: lists the WABA's approved message templates for the dropdown.
export async function template_name(auth: RT.WhatsappBusiness) {
const apiVersion = auth.api_version || "v25.0"
const r = await fetch(
`https://graph.facebook.com/${apiVersion}/${auth.business_account_id}/message_templates?fields=name,language,status&limit=100`,
{
headers: {
Authorization: `Bearer ${auth.token}`,
Accept: "application/json",
},
}
)
const { data } = (await r.json()) as {
data: { name: string; language: string; status: string }[]
}
return (data ?? []).map((t) => ({
value: t.name,
label: `${t.name} (${t.language}, ${t.status})`,
}))
}
/**
* Send Template Message
* Send a pre-approved template message to a WhatsApp user. Templates are required to start conversations outside the 24-hour customer service window.
*/
export async function main(
auth: RT.WhatsappBusiness,
to: string,
template_name: DynSelect_template_name,
language: string,
components: { [key: string]: any }[] | undefined
) {
const apiVersion = auth.api_version || "v25.0"
const url = new URL(
`https://graph.facebook.com/${apiVersion}/${auth.phone_number_id}/messages`
)
const template: { [key: string]: any } = {
name: template_name,
language: { code: language },
}
if (components !== undefined && components.length > 0) {
template.components = components
}
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({
messaging_product: "whatsapp",
recipient_type: "individual",
to,
type: "template",
template,
}),
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
Submitted by hugo989 5 hours ago