Node.js发送含变量的WhatsApp模板消息时出现404错误
问题分析与修复方案
错误原因
你的请求负载存在结构错误:在components数组中,你将body组件与button组件的属性合并到了同一个对象里,导致WhatsApp API无法正确解析请求结构,最终返回404错误。
修复后的代码
正确的做法是将body和button拆分为两个独立的对象,放入components数组中:
let from = req.body.entry[0].changes[0].value.messages[0].from; let flowtest= { messaging_product: "whatsapp", recipient_type: "individual", to: from, type: "template", template: { name: "flowtest", language: { code: "ar", }, components: [ // 独立的正文组件 { type: "body", parameters: [ { type: "text", text: from, }, ] }, // 独立的Flow按钮组件 { type: "button", sub_type: "flow", index: "0", // 需根据模板配置补充Flow令牌参数 parameters: [ { type: "flow_token", flow_token: "你的Flow令牌" } ] }, ], }, };
额外注意事项
- 确认你的WhatsApp模板
flowtest已配置对应数量的正文变量,否则即使结构正确,也会返回参数不匹配的错误。 - Flow按钮组件必须包含
flow_token参数(需与你的Flow配置对应),如果模板无需额外参数,也需保持参数结构的完整性。
内容的提问来源于stack exchange,提问作者Omar Jabban
相关产品推荐
相关产品推荐

