基于 WhatsApp Business Cloud API 实现,采用 System User Token Bearer 鉴权。
包信息
- NuGet 包:
Bitzsoft.Integrations.InstantMessaging.WhatsApp - 接口:
IInstantMessagingService - 鉴权:Cloud API Bearer(System User Token)
- Provider ID:
whatsapp
配置
| 字段 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
AccessToken | string | 是 | — | System User Token(Bearer 鉴权凭据) |
PhoneNumberId | string | 是 | — | 已注册的电话号码 ID |
BaseUrl | string | 否 | https://graph.facebook.com/v18.0 | Graph API 基地址(不含 phone_number_id) |
HttpClientName | string | 否 | InstantMessagingWhatsApp | 业务 HttpClient 命名 |
{ "InstantMessaging": { "WhatsApp": { "AccessToken": "your-access-token", "PhoneNumberId": "your-phone-number-id" } }}注册
// 方式一:IConfiguration(默认读取 "InstantMessaging:WhatsApp" 配置节)services.AddWhatsAppInstantMessaging(configuration);
// 方式二:委托配置services.AddWhatsAppInstantMessaging(options =>{ options.AccessToken = "your-access-token"; options.PhoneNumberId = "your-phone-number-id";});使用示例
// 发送文本消息var result = await im.SendTextMessageAsync(new SendTextMessageRequest{ Channel = "1234567890", Text = "Your order has been shipped!"});
// 更新消息await im.UpdateMessageAsync("msg-id", "Updated content");鉴权机制
- 每个请求通过
Authorization: Bearer {AccessToken}头携带 System User Token。 phone_number_id在运行时拼入请求路径(/{PhoneNumberId}/messages)。
已知限制
WhatsApp Cloud API 无频道列表查询和成员邀请能力(与 Slack/Discord 的频道模型不同)。ListChannelsAsync/CreateChannelAsync/InviteMemberAsync 的语义按 WhatsApp 实际能力实现或受限。