业务平台 · 聊天记录查询 API

供运营管理后台等业务系统拉取企微会话存档中的客户聊天记录。 数据来源为聊天机器人已解密落库的 cb_wecom_chat_message 表(需开启企微会话存档)。 鉴权方式与用户同步接口一致,使用 X-Chatbot-Webhook-Secret 请求头(与 用户同步 中配置的 Webhook 密钥相同)。

接口基址:/api/public/chatbot/chat-messages

鉴权

请求头必填说明
X-Chatbot-Webhook-Secret 与聊天机器人「用户同步」配置的 Webhook 密钥一致(环境变量 PLATFORM_SYNC_WEBHOOK_SECRET 或管理页保存的值)

查询聊天记录

GET /api/public/chatbot/chat-messages

Query 参数

参数类型说明
platform_user_id integer 运营管理后台用户 ID(推荐)。与用户同步成功后返回的 id 对应。
external_userid string 企微外部联系人 ID。与 platform_user_id 二选一;若两者都传,优先使用 platform_user_id
follow_userid string 企微跟进人成员 ID。与 platform_user_id 配合使用时必填(同一平台用户可添加多位跟进人,以 platform_user_id + follow_userid 联合定位会话)。与 external_userid 配合使用时可选;不传时取本地最近更新的跟进关系。
limit 可选 integer 每页条数,默认 50,最大 200。
before_seq 可选 long 分页游标:返回 seq 小于该值的更早消息。取上一页响应中的 next_before_seq
since 可选 long 消息时间下限(Unix 毫秒,对应企微 msgtime)。
until 可选 long 消息时间上限(Unix 毫秒)。

响应字段

字段类型说明
conversationobject会话标识:external_userid、follow_userid、customer_name、platform_user_id
messagesarray消息列表,按时间升序
messages[].directionstringinbound 客户发来;outbound 员工发出
messages[].sender_rolestringcustomerstaff
messages[].contentstring文本内容或媒体占位;语音若已识别则返回识别文本
messages[].has_mediaboolean媒体是否已下载就绪,为 true 时可调 /chat-media/{msgid}
messages[].media_statusstring媒体处理状态:pending / success / failed
messages[].voice_play_secondsint | null语音时长(秒)
messages[].voice_transcriptstring | null语音识别文本
messages[].file_namestring | null文件名(msgtype=file 时)
messages[].msgtimelong企微消息时间戳(毫秒)
has_moreboolean是否还有更早消息
next_before_seqlong | null下一页游标;无更多时为 null

响应示例

{
  "conversation": {
    "external_userid": "wmxxxxxxxx",
    "follow_userid": "ZhangSan",
    "customer_name": "李女士",
    "platform_user_id": 12345
  },
  "messages": [
    {
      "msgid": "CAxxxx",
      "seq": 1001,
      "direction": "inbound",
      "sender_role": "customer",
      "msgtype": "text",
      "content": "你好,我想咨询一下",
      "msgtime": 1718000000000
    },
    {
      "msgid": "CAyyyy",
      "seq": 1002,
      "direction": "outbound",
      "sender_role": "staff",
      "msgtype": "text",
      "content": "您好,请问有什么可以帮您?",
      "msgtime": 1718000060000
    }
  ],
  "has_more": false,
  "next_before_seq": null
}

请求示例

curl -sS -G 'https://<chatbot-host>/api/public/chatbot/chat-messages' \
  -H 'X-Chatbot-Webhook-Secret: <your-secret>' \
  --data-urlencode 'platform_user_id=12345' \
  --data-urlencode 'limit=50'

分页示例

curl -sS -G 'https://<chatbot-host>/api/public/chatbot/chat-messages' \
  -H 'X-Chatbot-Webhook-Secret: <your-secret>' \
  --data-urlencode 'platform_user_id=12345' \
  --data-urlencode 'before_seq=1001' \
  --data-urlencode 'limit=50'

删除聊天记录

业务平台删除居民时调用:清除该客户会话存档消息,并同步删除相关定时消息、桥接事件与会话事件。 鉴权同查询接口。

DELETE /api/public/chatbot/chat-messages

Query 参数

参数类型说明
platform_user_id integer 运营管理后台用户 ID。与 follow_userid 联用定位;找不到同步关系时需同时传 external_userid 兜底。
external_userid string 企微外部联系人 ID。与 platform_user_id 二选一;推荐始终带上。
follow_userid string 跟进人。传入时仅删该跟进人会话;不传则删该外部联系人相关全部存档消息。

响应字段

字段类型说明
okboolean是否成功
deleted_messagesint删除的存档消息条数
deleted_scheduledint删除的定时消息条数
deleted_bridge_eventsint删除的桥接事件条数
deleted_conversation_eventsint删除的会话事件条数

请求示例

curl -sS -X DELETE 'https://<chatbot-host>/api/public/chatbot/chat-messages' \
  -H 'X-Chatbot-Webhook-Secret: <your-secret>' \
  -G \
  --data-urlencode 'platform_user_id=12345' \
  --data-urlencode 'follow_userid=ZhangSan' \
  --data-urlencode 'external_userid=wmxxxxxxxx'

错误码

HTTP说明
401缺少或错误的 X-Chatbot-Webhook-Secret
400未提供 platform_user_idexternal_userid
404找不到对应客户或平台用户映射
503Webhook 未配置,或企微 corp-id 未配置

前置条件

相关接口:用户同步见 用户同步; 运营管理后台入站接口为 POST /api/public/chatbot/user(由本服务主动调用, event=added 同步用户、event=removed 标记取关; 有企微 unionid 时一并传 union_id,便于小程序同源绑定)。