Web

会话消息回复(Thread)

更新时间: 2024/03/14 19:21:11

QChat SDK 的QChatMsgServiceInterface接口提供会话消息回复(Thread)相关方法。通过会话消息回复,用户可针对某一条消息进行提问、反馈或补充相关背景信息,且不会对频道内的会话流造成干扰。

圈组的会话消息回复功能只能在圈组内使用,且相关接口和即时通讯 IM 的不一样。

功能介绍

什么是 Thread

Thread 指以一条消息作为根消息的消息回复树状结构,示例见下图。

Thread消息.png

上图中:

  • 消息 A 是消息 B 的父消息,消息 B1 是消息 C 的父消息
  • 消息 C 是消息 B1 的子消息
  • 消息 A 是消息 B 和消息 C 的根消息
  • 消息 A、B、C 统称为 Threaded Message(串联起来的消息)
  • 一条 Threaded Message 必须有一条父消息或至少一条子消息。如果一条消息既没有父消息,也没有子消息,则为普通消息。
  • 若未开通会话消息回复功能,回复时系统会自动将所发消息转换为一条普通消息。

重要属性介绍

Thread 中,除了根消息(如上图中的消息 A),其他消息都具有下表所示的属性。

属性 说明
threadRefer.fromAccount 根消息的发送者账号
threadRefer.time 根消息的发送时间
threadRefer.msgIdServer 根消息在服务端(云信服务器)生成的消息 ID,该消息 ID 不一定存在,例如开启安全通(易盾反垃圾)的情况
threadRefer.msgIdClient 根消息的客户端消息 ID
replyRefer.fromAccount 父消息的发送者账号
replyRefer.time 父消息的发送时间
replyRefer.msgIdServer 父消息在服务端(云信服务器)生成的消息 ID,该消息 ID 不一定存在(如开启易盾反垃圾的情况)
replyRefer.msgIdClient 父消息的客户端消息 ID

UI 示例

会话消息回复(Thread)的 UI 示例代码如下:

Thread聊天.png

前提条件

开始会话消息回复相关功能集成前,请确保:

  • 开通圈组的消息回复功能。圈组的会话消息回复功能需要在开通圈组功能的基础上额外开通后才能使用。
  • 已完成圈组初始化。

实现方法

实现会话消息回复

API调用时序

uml diagram

具体流程

本节仅对上图中标为橙色的流程进行说明,其他流程请参考相关文档。例如:

  1. 发送方和接收方在登录圈组前,注册message消息接收事件回调函数。

    示例代码如下:

    const qchat = new QChat({
      // 初始化参数
    })
    qchat.on('message',(message)=>{})
    
  2. 接收方在收到消息后,调用replyMessage方法,对接收到的消息进行针对性的回复。

    • 需要拥有发送消息的权限(sendMsg)才能回复消息。
    • 两条消息的serverIdchannelId 必须相同,因为只能在同一个服务器和频道内回复消息。

    假设接收方收到的消息的 msgIdServer6407677 ,那么回复该条消息的示例代码如下:

    const message = await qchat.qchatMsg.replyMessage({
      "serverId": "1771109",
      "channelId": "1623869",
      "type": "text",
      "body": "天空海阔",
      "replyMessage": {
        "serverId": "1771109",
        "channelId": "1623869",
        "fromAccount": "cs1",
        "fromClientType": "Web",
        "fromDeviceId": "d95be95e20ee322643120ec3cdfedd34",
        "fromNick": "nibaba",
        "time": 1650613338241,
        "updateTime": 1650613338241,
        "type": "text",
        "body": "test111111",
        "msgIdClient": "4bef9fb59c09fc81b58e2c091592fca6",
        "msgIdServer": "6407677",
        "status": 0,
        "callbackExt": "callbackext123",
        "historyEnable": true,
        "pushEnable": true,
        "needBadge": true,
        "needPushNick": true,
        "notifyReason": "notifyAll",
        "routeEnable": true,
        "deliveryStatus": "success"
      }
    })
    
    
  3. message消息接收事件回调函数触发,发送方收到接收方回复的消息。

相关查询

本节仅介绍与会话消息回复相关的重点查询方法,其他查询方法的说明,请参见下文的 API 参考

查询 Thread 的消息列表

调用getThreadMessages方法,可根据某个 Thread 中的任意一条消息分页查询该 Thread 的消息列表(即该 Thread 的聊天历史)。

假设我们需要查询 msgIdServer6358905 的消息所在的 Thread 的消息列表,示例代码如下:

jsawait qchat.qchatMsg.getThreadMessages({
  "message": {
    "fromAccount": "ctt1",
    "serverId": "1771109",
    "channelId": "1623869",
    "type": "text",
    "body": "点点滴滴",
    "fileInput": "external-file",
    "msgIdClient": "4a09be6680134b57d78ff4c10aa73265",
    "fromClientType": "Web",
    "fromDeviceId": "dc8221e74db03af6d0b52839665493c3",
    "fromNick": "ctt1",
    "time": 1649916251868,
    "updateTime": 1649916251868,
    "msgIdServer": "6358905",
    "status": 0,
    "callbackExt": "callbackext123",
    "historyEnable": true,
    "pushEnable": true,
    "needBadge": true,
    "needPushNick": true,
    "routeEnable": true,
    "deliveryStatus": "success"
  },
  "messageQueryOption": { "reverse": true }
})

查询某消息的父消息以及根消息

调用getReferMessages方法,可查询某消息的回复消息以及其根消息。

示例代码如下:

jsconst result = await qchat.qchatMsg.getReferMessages({
  "message": {
    "serverId": "1771109",
    "channelId": "1623869",
    "fromAccount": "cs1",
    "fromClientType": "Web",
    "fromDeviceId": "16739a6f1a8efd489b2f85982ecd7edc",
    "fromNick": "大阴人212",
    "time": 1649767046449,
    "updateTime": 1649767046449,
    "type": "text",
    "body": "test123",
    "msgIdClient": "f790022bc768d08e6d3ee4b9d9ffb9b7",
    "msgIdServer": "6358275",
    "status": 0,
    "callbackExt": "callbackext123",
    "historyEnable": true,
    "pushEnable": true,
    "needBadge": true,
    "needPushNick": true,
    "notifyReason": "notifyAll",
    "routeEnable": true,
    "replyRefer": {
        "fromAccount": "ctt1",
        "time": 1649767022146,
        "msgIdServer": "6358274",
        "msgIdClient": "fb58529af074bf6ab130a6673322e584"
    },
    "threadRefer": {
        "fromAccount": "ctt1",
        "time": 1649767022146,
        "msgIdServer": "6358274",
        "msgIdClient": "fb58529af074bf6ab130a6673322e584"
    },
    "deliveryStatus": "success"
  },
  "referType": "all"
})

console.log(result.replyMessage, result.threadMessage)

根据消息 ID 批量查询回复消息

调用getMessageHistoryByIds方法,可根据消息的msgIdServer查询历史回复消息。查询结果不分页,一次最多查询 100 条。

示例代码如下:

jsconst messages = await qchat.qchatMsg.getMessageHistoryByIds({
  "serverId": "1771109",
  "channelId": "1623869",
  "messageReferList": [
    {
      "msgIdServer": "6128930",
      "time": 1648610230231
    }
  ]
})

API 参考

API
说明
message 消息接收事件回调函数
replyMessage 回复消息
getThreadMessages 根据某个 Thread 中的任意一条消息分页查询该 Thread 的消息列表。
getReferMessages 查询某消息的回复消息以及其根消息。
getMessageHistoryByIds 根据消息的msgIdServer查询历史回复消息
getThreadRootMessagesMeta 批量查询根消息的 meta 信息(例如总回复数、最后回复时间)
此文档是否对你有帮助?
有帮助
去反馈
  • 功能介绍
  • 什么是 Thread
  • 重要属性介绍
  • UI 示例
  • 前提条件
  • 实现方法
  • 实现会话消息回复
  • API调用时序
  • 具体流程
  • 相关查询
  • 查询 Thread 的消息列表
  • 查询某消息的父消息以及根消息
  • 根据消息 ID 批量查询回复消息
  • API 参考