使用WhatsApp Business API创建动态按钮模板遇(#100)参数错误
问题分析与解决方案
错误原因
你遇到的(#100) The parameter name is required.错误主要来自三个问题:
- 请求数据传递方式错误:你将JSON字符串直接传给
requests.post的json参数,requests会将其视为原始文本而非JSON对象,导致服务器解析失败,误判为缺少name参数。 - 按钮示例参数格式不符合Meta规范:URL按钮和PHONE_NUMBER按钮的示例参数需要使用专属字段名,而非通用的
parameters。 - 请求URL占位符未替换:URL中的
whatsapp_busines_id是占位符,必须替换为你的WhatsApp Business Account ID(WABA ID)。
修正后的代码
import requests from requests.structures import CaseInsensitiveDict # 替换为你的WABA ID WABA_ID = "your_whatsapp_business_account_id" url = f"https://graph.facebook.com/v20.0/{WABA_ID}/message_templates" headers = CaseInsensitiveDict() headers["Authorization"] = "Bearer YOUR_ACCESS_TOKEN" headers["Content-Type"] = "application/json" # 使用Python字典而非JSON字符串,确保requests正确序列化 data = { "name": "payment_success_property_contact", "language": "en", "category": "UTILITY", "components": [ { "type": "HEADER", "format": "TEXT", "text": "Payment Successful" }, { "type": "BODY", "text": 'Hi there! Great news! Your payment for "{{1}}" is successful. Please contact the owner via WhatsApp or normal call to visit and check the property. After your visit, you can confirm or cancel the payment.\n\nEnjoy your visit!', "example": { "body_text": [ "Seaside Villa" ] } }, { "type": "FOOTER", "text": "Nanohunt Software" }, { "type": "BUTTONS", "buttons": [ { "type": "URL", "text": "Contact via WhatsApp", "url": "https://wa.me/{{2}}", "example": { # URL按钮使用url_parameters字段 "url_parameters": [ { "type": "text", "text": "YOUR_PHONE_NUMBER" } ] } }, { "type": "PHONE_NUMBER", "text": "Call Owner", "phone_number": "{{2}}", "example": { # 电话号码按钮使用phone_number_parameters字段 "phone_number_parameters": [ { "type": "text", "text": "YOUR_PHONE_NUMBER" } ] } }, { "type": "URL", "text": "Confirm or Cancel Payment", "url": "{{3}}", "example": { "url_parameters": [ { "type": "text", "text": "https://yourwebsite.com/transaction/TRANSACTION_ID" } ] } } ] } ] } # 直接传递字典给json参数,requests会自动序列化为JSON resp = requests.post(url, headers=headers, json=data) print(resp.json())
额外注意事项
- 确保
YOUR_ACCESS_TOKEN是有效的、具有whatsapp_business_management权限的用户访问令牌或系统用户令牌。 - 模板名称
payment_success_property_contact需符合Meta的命名规则:只能包含小写字母、数字和下划线,长度不超过50字符。 - 动态参数的编号(
{{1}}、{{2}}、{{3}})需在模板中连续且唯一,不能重复或跳号。
内容的提问来源于stack exchange,提问作者Mbishu Fabrice
相关产品推荐
相关产品推荐

