跳转到主要内容
POST
/
message
/
sendbatch
curl -X POST "http://localhost:5001/message/sendbatch" \
  -H "Content-Type: application/json" \
  -d '[
    {
      "header": {
        "no_persist": 0,
        "red_dot": 1,
        "sync_once": 0
      },
      "client_msg_no": "batch_msg_1",
      "from_uid": "system",
      "channel_id": "group123",
      "channel_type": 2,
      "expire": 0,
      "payload": "SGVsbG8gR3JvdXAgMQ==",
      "tag_key": "notification"
    },
    {
      "header": {
        "no_persist": 0,
        "red_dot": 1,
        "sync_once": 0
      },
      "client_msg_no": "batch_msg_2",
      "from_uid": "system",
      "channel_id": "group456",
      "channel_type": 2,
      "expire": 0,
      "payload": "SGVsbG8gR3JvdXAgMg==",
      "tag_key": "notification"
    }
  ]'
[
  {
    "message_id": 123456789,
    "message_seq": 1001,
    "client_msg_no": "batch_msg_1"
  },
  {
    "message_id": 123456790,
    "message_seq": 1002,
    "client_msg_no": "batch_msg_2"
  }
]

概述

批量发送多条消息,提高消息发送效率,适用于群发通知、批量推送等场景。

请求体

请求体是一个消息对象数组,每个消息对象包含以下字段:

必传参数

[].payload
string
required
Base64 编码的消息内容
[].from_uid
string
required
发送者用户 ID
[].channel_id
string
required
目标频道 ID
[].channel_type
integer
required
频道类型 (1=个人频道, 2=群组频道)

可选参数

[].header
object
消息头部信息
[].client_msg_no
string
客户端消息编号
[].stream_no
string
流消息编号
[].expire
integer
消息过期时间(秒),0 表示不过期
[].subscribers
array
指定接收消息的订阅者列表
[].subscribers[]
string
订阅者用户 ID
curl -X POST "http://localhost:5001/message/sendbatch" \
  -H "Content-Type: application/json" \
  -d '[
    {
      "header": {
        "no_persist": 0,
        "red_dot": 1,
        "sync_once": 0
      },
      "client_msg_no": "batch_msg_1",
      "from_uid": "system",
      "channel_id": "group123",
      "channel_type": 2,
      "expire": 0,
      "payload": "SGVsbG8gR3JvdXAgMQ==",
      "tag_key": "notification"
    },
    {
      "header": {
        "no_persist": 0,
        "red_dot": 1,
        "sync_once": 0
      },
      "client_msg_no": "batch_msg_2",
      "from_uid": "system",
      "channel_id": "group456",
      "channel_type": 2,
      "expire": 0,
      "payload": "SGVsbG8gR3JvdXAgMg==",
      "tag_key": "notification"
    }
  ]'
[
  {
    "message_id": 123456789,
    "message_seq": 1001,
    "client_msg_no": "batch_msg_1"
  },
  {
    "message_id": 123456790,
    "message_seq": 1002,
    "client_msg_no": "batch_msg_2"
  }
]

响应字段

响应是一个数组,每个元素对应一条发送的消息:
message_id
integer
required
服务器生成的消息 ID
message_seq
integer
required
消息序列号
client_msg_no
string
required
客户端消息编号(回显)

状态码

状态码说明
200批量消息发送成功
400请求参数错误
403没有发送权限
500服务器内部错误

使用场景

系统通知

  • 公告推送:向多个群组发送系统公告
  • 活动通知:批量发送活动提醒消息
  • 维护通知:系统维护前的批量通知

营销推广

  • 促销消息:向目标用户群发送促销信息
  • 新功能介绍:批量推送新功能使用指南
  • 用户调研:发送问卷调查消息

运营管理

  • 数据统计:批量发送数据报告
  • 任务分配:向团队成员批量分配任务
  • 会议通知:批量发送会议邀请

性能优化

批量大小

  • 建议批量:单次批量发送建议不超过 100 条消息
  • 分批处理:大量消息可分批发送,避免超时
  • 并发控制:控制并发批量请求数量

消息优化

  • 内容压缩:对于相同内容,可以使用模板减少数据传输
  • 异步处理:使用异步方式处理批量发送
  • 错误重试:实现失败消息的重试机制

最佳实践

  1. 消息去重:确保每条消息的 client_msg_no 唯一
  2. 错误处理:处理部分消息发送失败的情况
  3. 权限验证:验证发送者对所有目标频道的发送权限
  4. 内容审核:对批量消息内容进行审核
  5. 频率限制:实施合理的批量发送频率限制
  6. 监控告警:监控批量发送的成功率和性能