通过 bat/messagelist 发送 WhatsApp
WhatsApp Business 消息与 SMS 和 Viber 使用相同的请求发送:POST /bat/messagelist,并设置 "type": "whatsapp"。消息内容放在 whatsAppMessage 对象中。
本页每个示例的现成请求都在 Postman 集合中,请参阅下方的“Postman 集合”部分。
两种消息类型
| 类型 | whatsAppMessage.type | 何时可以发送 |
|---|---|---|
| 会话消息:文本、媒体、位置、联系人、交互式 | text, media, location, contact, interactive | 仅限客户最后一条来信之后的 24 小时会话窗口内。 |
| 模板 | template | 任何时候。模板必须已在您的 WhatsApp Business Account(WABA)中获批。 |
没有会话窗口时,请发送已获批的模板。当 WhatsApp 是回退链中的一个步骤时,此规则同样适用。
通用字段
{
"callbackUrl": "https://example.com/smsbat/status",
"messages": [
{
"from": "WABA_PHONE",
"to": "RECIPIENT_PHONE",
"type": "whatsapp",
"ttl": 60,
"whatsAppMessage": { "type": "text", "text": "Your order is ready." }
}
]
}
| 字段 | 说明 |
|---|---|
callbackUrl | 送达回调的地址。可选。 |
from | 发送消息所用的 WhatsApp Business 号码。 |
to | 收件人号码。 |
type | whatsapp. |
ttl | 消息有效期,单位为秒。示例使用 60;请根据您的场景设置。 |
whatsAppMessage | 消息内容,请参阅下方各部分。 |
身份验证:使用带有组织密钥的 X-Authorization-Key 请求头,与所有 SMSBAT API 请求相同。
会话消息
本部分的每个示例都需要 24 小时会话窗口。
文本
"whatsAppMessage": {
"type": "text",
"text": "Your order is ready."
}
带说明文字的图片
"whatsAppMessage": {
"type": "media",
"media": {
"type": "image",
"url": "https://example.com/image.png",
"caption": "Your order is ready."
}
}
media.url 是图片的公开 HTTPS 地址。caption 为可选项。
带说明文字的视频
"whatsAppMessage": {
"type": "media",
"media": {
"type": "video",
"url": "https://example.com/video.mp4",
"caption": "Your order is ready."
}
}
音频
"whatsAppMessage": {
"type": "media",
"media": {
"type": "audio",
"url": "https://example.com/audio.mp3"
}
}
文档
"whatsAppMessage": {
"type": "media",
"media": {
"type": "document",
"url": "https://example.com/invoice.pdf",
"filename": "invoice.pdf",
"caption": "Your invoice"
}
}
filename 设置收件人看到的文件名。caption 为可选项。
media 字段:
| 字段 | 说明 |
|---|---|
type | image、video、audio 或 document。 |
url | 文件的公开 HTTPS 地址。 |
caption | 说明文字。可选;示例中用于图片、视频和文档。 |
filename | 文档的文件名。 |
位置
"whatsAppMessage": {
"type": "location",
"location": {
"latitude": 48.1486,
"longitude": 17.1077,
"name": "Pickup point",
"address": "Bratislava"
}
}
需要时可添加地点名称和地址。
联系人
"whatsAppMessage": {
"type": "contact",
"contact": {
"name": {
"formatted_name": "Support Team",
"first_name": "Support"
},
"phones": [
{ "phone": "+421900000000", "type": "WORK" }
]
}
}
除完整显示名称(formatted_name)外,请至少再填写一个姓名字段,例如 first_name。
回复按钮
"whatsAppMessage": {
"type": "interactive",
"interactive": {
"type": "button",
"body": { "text": "Confirm your order" },
"action": {
"buttons": [
{ "type": "reply", "reply": { "id": "confirm", "title": "Confirm" } },
{ "type": "reply", "reply": { "id": "cancel", "title": "Cancel" } }
]
}
}
}
使用 reply.id 识别收件人按下了哪个按钮。reply.title 是按钮标签。
列表
"whatsAppMessage": {
"type": "interactive",
"interactive": {
"type": "list",
"body": { "text": "Choose a pickup point" },
"action": {
"button": "View options",
"sections": [
{
"title": "Pickup points",
"rows": [
{ "id": "center", "title": "City centre" },
{ "id": "station", "title": "Main station" }
]
}
]
}
}
}
action.button 是打开列表的按钮的标签。sections[].rows[] 是选项,每个选项都有 id 和 title。
模板
模板可以在任何时候发送,无需会话窗口。模板必须已在您用于发送的 WABA 中获批。
如何填写模板
| 字段 | 说明 |
|---|---|
template.id | 已获批模板的准确名称。 |
template.language | 该模板的语言代码,例如 uk 或 en_US。 |
template.body.parameters | 文本变量的值,按模板中的顺序排列:[{ "text": "..." }]。如果没有变量,请省略 body。 |
template.header | 仅用于带有变量页眉的模板。静态页眉不需要。 |
template.buttons | 仅用于动态 URL 按钮和快速回复按钮。 |
静态按钮 URL 和拨号按钮的电话号码在模板本身中设置,发送时无需传递。参数和按钮的数量必须与您已获批的模板一致。
文本模板
"whatsAppMessage": {
"type": "template",
"template": {
"id": "YOUR_TEMPLATE_NAME",
"language": "uk",
"body": {
"parameters": [ { "text": "Alex" } ]
}
}
}
动态 URL 按钮
"whatsAppMessage": {
"type": "template",
"template": {
"id": "YOUR_TEMPLATE_NAME",
"language": "uk",
"body": {
"parameters": [
{ "text": "Oleh" },
{ "text": "1 January 2024" },
{ "text": "+1 555-745-2867" }
]
},
"buttons": [
{ "type": "url", "index": 0, "url_parameter": "ORDER-123" }
]
}
}
index 是按钮在模板中的位置,从 0 开始。在 url_parameter 中只传递地址的可变部分。例如,对于按钮 URL https://example.com/orders/{{1}},值 ORDER-123 会生成 https://example.com/orders/ORDER-123。如果按钮 URL 是静态的,请不要传递 buttons。
快速回复按钮
"buttons": [
{ "type": "quick_reply", "index": 0, "quick_reply_payload": "CONFIRM" },
{ "type": "quick_reply", "index": 1, "quick_reply_payload": "CANCEL" }
]
quick_reply_payload 可帮助您判断收件人按下了哪个按钮。按钮索引必须与模板一致。
页眉:文本
"header": { "type": "text", "text": "Order 123" }
在 header.text 中传递页眉变量的值。
页眉:图片、视频、文档
"header": { "type": "image", "image": { "url": "https://example.com/image.png" } }
"header": { "type": "video", "video": { "url": "https://example.com/video.mp4" } }
"header": {
"type": "document",
"document": { "url": "https://example.com/file.pdf", "filename": "sample.pdf" }
}
URL 必须是公开的 HTTPS 地址。对于文档,filename 设置附件名称。
送达状态
WhatsApp 消息状态会以送达回调格式发送到 callbackUrl:delivered、undeliverable 或 permanenterror。
Postman 集合
下载“OmniSMSBat · BAT /messagelist”集合:包含 WhatsApp(会话消息和 7 种模板)、Viber Business、SMS 及回退的现成请求。
- 将文件导入 Postman。
- 填写集合变量:
orgKey(组织密钥)、toPhone(收件人号码)、wabaPhone(WhatsApp 发送号码)、alphaName(Viber Business 和 SMS 的字母名称),如有需要还有callbackUrl。 - 在模板请求中,将
template.id里的PLACEHOLDER_...替换为您已获批模板的名称,并检查template.language。
请求会发送到生产服务器 https://restapi.smsbat.com,并且可能产生费用。没有单独的测试环境。