WhatsApp 纯文本消息
纯文本是最简单的自由格式内容分支:仅含正文,无附件,并可为正文中的第一个链接提供可选预览。
发送文本消息
设置 text.body:
const msg = await bird.whatsapp.send({
to: "+16505551234",
from: "+13124495648",
text: { body: "Your driver is 2 minutes away." },
});
console.log(msg.id, msg.status);msg = client.whatsapp.send(
to="+16505551234",
from_="+13124495648",
text={"body": "Your driver is 2 minutes away."},
)
print(msg.id, msg.status)package main
import (
"context"
"fmt"
"log"
"os"
bird "github.com/messagebird/bird-sdk-go"
"github.com/messagebird/bird-sdk-go/option"
)
func main() {
client, err := bird.NewClient(option.WithAPIKey(os.Getenv("BIRD_API_KEY")))
if err != nil {
log.Fatal(err)
}
msg, err := client.Whatsapp.Send(context.Background(), bird.WhatsappSendParams{
To: "+16505551234",
From: "+13124495648",
Text: &bird.WhatsAppTextSend{Body: "Your driver is 2 minutes away."},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(msg.Id, *msg.Status)
}$text = (new WhatsAppMessageSendRequestText())
->setBody('Your driver is 2 minutes away.');
$message = $bird->whatsapp->send(
to: '+16505551234',
from: '+13124495648',
text: $text,
);
echo $message->getId(), ' ', $message->getStatus();bird whatsapp send \
--from +13124495648 \
--text 'Your driver is 2 minutes away.' \
--to +16505551234{
"name": "whatsapp_send",
"arguments": {
"from": "+13124495648",
"text": {
"body": "Your driver is 2 minutes away."
},
"to": "+16505551234"
}
}curl -X POST "https://us1.platform.bird.com/v1/whatsapp/messages" \
-H "Authorization: Bearer $BIRD_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": "+16505551234",
"from": "+13124495648",
"text": {
"body": "Your driver is 2 minutes away."
}
}'完整结构在 preview_url 基础上添加了所有自由格式发送可携带的字段:
代码示例
{
"to": "+16505551234",
"from": "+13124495648",
"text": {
"body": "Your order shipped: https://example.com/track/A1B2C3",
"preview_url": true
},
"in_reply_to_message_id": "wam_01kya19eknftrs2s6p82asmvnh",
"tags": [{ "name": "category", "value": "shipping" }],
"metadata": { "order_id": "A1B2C3" }
}from 在每条服务消息中都是必填项:必须是您的工作区拥有的号码,而非 Bird 托管的号码。in_reply_to_message_id 引用同一会话中的早期消息;请参阅引用消息了解其匹配逻辑及可能遗漏的情况。
限制
| 字段 | 约束 | 执行方 |
|---|---|---|
| body | 1 至 4096 个字符 | Bird,在接受时 (422) |
| preview_url | 布尔值,默认为 false | 不适用,仅供参考 |
仅含空白字符的 body 可以通过 schema 自身的 minLength: 1,但 Bird 仍会拦截:去除首尾空白后为空的 body 会被拒绝,返回 422 E15015 WhatsAppContentRequired。超过 4096 个字符的正文会被拒绝,返回普通的 422,且没有专用目录代码。
读取入站文本消息
限制与边界情况
- 客户服务窗口必须处于打开状态。 纯文本是服务消息,只能在打开的窗口内投递;请参阅中心的客户服务窗口。
- preview_url 仅影响第一个链接,且仅影响接收方客户端渲染的内容。 默认值为 false。将其设置为预览 body 中的第一个 URL;同一正文中后续的 URL 不会获得预览。如果接收方客户端无法获取该链接的预览,它会静默回退为普通可点击链接。读取时无法得知预览是否实际渲染。
- WhatsApp markdown 是接收方客户端对 body 的渲染,不属于 API 契约的一部分。 Bird 原样传递 body;它不会验证、剥离或编码 *bold*、_italic_、~strikethrough~ 或三反引号等宽字体标记。这些标记是否渲染完全取决于打开消息的客户端。
- 入站 body 不保证非空,尽管读取 schema 如此声明。 Meta 可能将入站消息报告为 "text": {} 或带有空的 body,而 Bird 会原样存储而非合成占位符。这是一个已知的未解决差异:不要编写信任 schema 的 required: body 的消费者。
后续步骤
- WhatsApp 服务消息:客户服务窗口及所有服务消息共享的模型
- 发送 WhatsApp 消息:请求信封、202 模型与安全重试
- 交互式消息:当您需要点击操作而非输入回复时