帮你快速理解、总结文档立即下载

消息相关

最近更新时间:2026-08-13 16:20:00
我的收藏

消息类介绍

AIDeskCore 中 IMessageModel 表示消息对象,用于描述一条消息具有的属性,例如类型、消息的内容、所属的会话 ID 等。

enum MESSAGE_TYPES {
MSG_TEXT = 'TIMTextElem', // 文本消息
MSG_IMAGE = 'TIMImageElem', // 图片消息
MSG_AUDIO = 'TIMSoundElem', // 语音消息
MSG_FILE = 'TIMFileElem', // 文件消息
MSG_FACE = 'TIMFaceElem', // 表情消息
MSG_VIDEO = 'TIMVideoFileElem', // 视频消息
MSG_LOCATION = 'TIMLocationElem', // 位置消息
MSG_GRP_TIP = 'TIMGroupTipElem', // 群提示消息
MSG_GRP_SYS_NOTICE = 'TIMGroupSystemNoticeElem', // 群系统消息
MSG_CUSTOM = 'TIMCustomElem', // 自定义消息
MSG_MERGER = 'TIMRelayElem', // 合并消息
MSG_STREAM = 'TIMStreamElem' // 消息流
}

enum CONVERSATION_TYPES {
CONV_C2C = 'C2C', // 单聊
CONV_GROUP = 'GROUP', // 群聊
CONV_TOPIC = 'TOPIC', // 话题群
CONV_SYSTEM = '@TIM#SYSTEM', // 系统会话
}

interface IMessageModel {
/** 会话 ID。 */
sessionId: string;
/** 消息 ID。 */
ID: string;
/** 消息类型。 */
type: MESSAGE_TYPES | '';
/** 消息内容。 */
payload: any;
/** 消息所属的会话 ID。 */
conversationID: string;
/** 消息所属会话的类型。 */
conversationType: CONVERSATION_TYPES | '';
/** 接收方的 userID。 */
to: string;
/** 发送方的 userID。 */
from: string;
/** 消息的流向。 */
flow: string;
/** 消息时间戳,单位:秒。 */
time: number;
/** 消息状态:unSend(未发送)、success(发送成功)、fail(发送失败)。 */
status: string;
/** 是否为已撤回消息。 */
isRevoked: boolean;
/** 消息发送者的昵称。 */
nick: string;
/** 消息发送者的头像。 */
avatar: string;
/** C2C 消息对端是否已读。 */
isPeerRead: boolean;
/** 消息自定义数据,会保存到云端并发送给对端。 */
cloudCustomData: string;
/** 是否为已删除消息。 */
isDeleted: boolean;
/** 是否需要已读回执。 */
needReadReceipt: boolean;
/** 消息已读回执信息。 */
readReceiptInfo: {
/** 消息已读数。 */
readCount?: number;
/** 消息未读数。 */
unReadCount?: number;
/** C2C 消息对端是否已发送已读回执。 */
isPeerRead?: boolean;
};
/** 图片、视频、语音、文件消息的上传进度,默认值为 0。 */
progress: number;
[key: string]: any;
}

消息相关 API

发送文本消息

发送文本消息的接口,调用此接口客服将在会话中发出一条文本消息。
接口
AIDeskCoreInstance.chatService.sendTextMessage(options);
参数
参数 options 为 object 类型,包含的属性值如下:
参数
类型
默认值
描述
sessionId
string
-
会话的标识 ID。
payload
object
-
消息内容的容器。
cloudCustomData
string
-
消息的自定义属性,请写入 json 字符串。
payload 的描述如下:
参数
类型
描述
text
string
消息文本内容。
返回值
示例
import AIDeskCoreInstance from '@tencentcloud/ai-desk-core';

await AIDeskCoreInstance.chatService.sendTextMessage({
sessionId: 'sessionId',
payload: {
text: 'test',
},
});

发送图片消息

发送图片消息的接口,调用此接口客服将在会话中发出一条图片消息。
接口
AIDeskCoreInstance.chatService.sendImageMessage(options);
参数
参数 options 为 object 类型,包含的属性值如下表所示:
参数
类型
默认值
描述
sessionId
string
-
会话的标识 ID。
payload
object
-
消息内容的容器。
cloudCustomData
string
-
消息的自定义属性,请写入 json 字符串。
payload 的描述如下:
参数
类型
描述
file
File
用于选择图片的 File 对象(Web)。
返回值
示例
import AIDeskCoreInstance from '@tencentcloud/ai-desk-core';

// Web 端发送图片消息示例 - 传入 File 对象
// 先在页面上添加一个 id 为 "testPasteInput" 的消息输入框
// 如 <input type="text" id="testPasteInput" placeholder="截图后粘贴到输入框中" size="30" />
document.getElementById('testPasteInput').addEventListener('paste', async function(e) {
let clipboardData = e.clipboardData;
let file;
let fileCopy;
if (clipboardData && clipboardData.files && clipboardData.files.length > 0) {
file = clipboardData.files[0];
// 图片消息发送成功后,file 指向的内容可能被浏览器清空,如果接入侧有额外的渲染需求,可以提前复制一份数据
fileCopy = file.slice();
}

if (typeof file === 'undefined') {
console.warn('file 是 undefined,请检查代码或浏览器兼容性!');
return;
}
await AIDeskCoreInstance.chatService.sendImageMessage({
sessionId: 'sessionId',
payload: {
file: file
},
});

发送视频消息

发送视频消息的接口,调用此接口客服将在会话中发出一条视频消息。
接口
AIDeskCoreInstance.chatService.sendVideoMessage(options);
参数
参数 options 为 object 类型,包含的属性值如下:
参数
类型
默认值
描述
sessionId
string
-
会话的标识 ID。
payload
object
-
消息内容的容器。
cloudCustomData
string
-
消息的自定义属性,请写入 json 字符串。
payload 的描述如下:
参数
类型
描述
file
File
用于选择视频的 File 对象(Web)。
返回值
示例
import AIDeskCoreInstance from '@tencentcloud/ai-desk-core';

await AIDeskCoreInstance.chatService.sendVideoMessage({
sessionId: 'sessionId',
payload: {
file: file
},
});

发送语音消息

发送语音消息的接口,调用此接口客服将在会话中发出一条语音消息。
接口
AIDeskCoreInstance.chatService.sendAudioMessage(options);
参数
参数 options 为 object 类型,包含的属性值如下:
参数
类型
默认值
描述
sessionId
string
-
会话的标识 ID。
payload
object
-
消息内容的容器。
cloudCustomData
string
-
消息的自定义属性,请写入 json 字符串。
payload 的描述如下:
参数
类型
描述
file
File
用于选择音频的 File 对象(Web)。
返回值
示例
import AIDeskCoreInstance from '@tencentcloud/ai-desk-core';

await AIDeskCoreInstance.chatService.sendAudioMessage({
sessionId: 'sessionId',
payload: {
file: file
},
});

发送自定义消息

发送自定义消息的接口,调用此接口客服将在会话中发出一条自定义消息。当 AIDeskCore 提供的能力不能满足您的需求时,可以使用自定义消息进行个性化定制。
接口
AIDeskCoreInstance.chatService.sendCustomMessage(options);
参数
参数 options 为 object 类型,包含的属性值如下:
参数
类型
默认值
描述
sessionId
string
-
会话的标识 ID。
payload
object
-
消息内容的容器
cloudCustomData
string
-
消息的自定义属性,请写入 json 字符串
payload 的描述如下:
参数
类型
描述
data
String
自定义消息的数据字段
返回值
示例

import AIDeskCoreInstance from '@tencentcloud/ai-desk-core';

await AIDeskCoreInstance.chatService.sendCustomMessage({
sessionId: 'sessionId',
payload: {
data: JSON.stringify({
src: "30",
content: "这是我们的配置信息\\n\\n![](https://im-console-chatbot-1303031839.cos.ap-guangzhou.myqcloud.com/1312281038/2024_07/1721357223805.%E8%8B%B9%E6%9E%9C%22%3Cimg%20src%3D1%20onerror%3D%22alert%28123%29%22%3E.jpg)\\n\\n[点击进入查看](https://www.qq.com)",
customerServicePlugin: 0,
}),
},
});

发送文件消息

发送文件消息的接口,调用此接口客服将在会话中发出一条文件消息。
接口
AIDeskCoreInstance.chatService.sendFileMessage(options);
参数
参数 options 为 Object 类型,包含的属性值如下:
参数
类型
默认值
描述
sessionId
String
-
会话的标识 ID。
payload
Object
-
消息内容的容器。
cloudCustomData
string
-
消息的自定义属性,请写入 json 字符串。
payload 的描述如下:
参数
类型
描述
file
File
用于选择文件的 File 对象(Web)。
返回值
示例

import AIDeskCoreInstance from '@tencentcloud/ai-desk-core';


await AIDeskCoreInstance.chatService.sendFileMessage({
sessionId: 'sessionId',
payload: {
file: file
},
});

撤回消息

撤回会话的消息。撤回成功后,消息对象的 isRevoked 属性值为 true
接口
AIDeskCoreInstance.chatService.revokeMessage(message);
参数
Name
Type
Description
message
IMessageModel
消息实例。
返回值
示例

import AIDeskCoreInstance from '@tencentcloud/ai-desk-core';

await AIDeskCoreInstance.chatService.revokeMessage(message)


重发消息

当消息发送失败时,可调用该接口进行重发。
接口
AIDeskCoreInstance.chatService.resendMessage(message);
参数
Name
Type
Description
message
IMessageModel
消息实例。
返回值
示例

import AIDeskCoreInstance from '@tencentcloud/ai-desk-core';

await AIDeskCoreInstance.chatService.resendMessage(message)


引用消息

引用之前的消息以表示对特定的消息进行回复,此数据将跟随发送的消息发出。
接口
AIDeskCoreInstance.chatService.quoteMessage(options);
参数 options 为 object 类型,包含的属性值如下:
参数
类型
默认值
描述
messageID
string
-
引用消息的 ID。
messageAbstract
string
-
显示引用消息的内容简介。
messageSender
string
-
引用消息的发送方 ID。
messageSenderNick
string
-
引用消息的发送方昵称。
messageType
number
-
消息类型。
messageTime
number
-
消息的时间戳。
messageSequence
number
-
消息的序列号。
version
number
1
默认传入 1 即可。
返回值
示例

import AIDeskCoreInstance from '@tencentcloud/ai-desk-core';

AIDeskCoreInstance.chatService.quoteMessage({
messageID: message.ID,
messageAbstract: '测试',
messageSender: message.from,
messageSenderNick: message.nick,
messageType: 1, // 1文本,2 自定义,3 图片,4音频,5视频,6 文件,0 其他
messageTime: message.time,
messageSequence: message.sequence,
version: 1,
})

会话已读

将指定会话设为已读,此操作会清空会话的未读数。
接口
AIDeskCoreInstance.chatService.setMessageRead(sessionId);
参数
类型
默认值
描述
sessionId
string
-
会话 ID。
返回值
示例

import AIDeskCoreInstance from '@tencentcloud/ai-desk-core';

AIDeskCoreInstance.chatService.setMessageRead(sessionId);

消息已读回执

将指定会话里的消息标记成已读状态。
接口
AIDeskCoreInstance.chatService.sendMessageReadReceipt(messageList);
参数
类型
默认值
描述
messageList
IMessageModel[]
-
需要标记已读的消息列表。
返回值
示例

import AIDeskCoreInstance from '@tencentcloud/ai-desk-core';

AIDeskCoreInstance.chatService.sendMessageReadReceipt(messageList);

拉取当前会话的消息列表

调用此方法会调用当前选中会话的消息列表,此方法会根据当前选择的会话类型自动分配不同的消息拉取逻辑。重复调用即可在消息存储 store 中获取当前选择会话所有的消息内容。
接口
AIDeskCoreInstance.chatService.getCurrertSessionMessageList();
参数
返回值
示例

import AIDeskCoreInstance from '@tencentcloud/ai-desk-core';

await AIDeskCoreInstance.chatService.getCurrertSessionMessageList();

批量翻译文本

调用此接口可对文本进行批量翻译。
注意:
使用此功能会进行计费,如需要请联系我们进行功能权限开通。
接口
AIDeskCoreInstance.chatService.translateText(options);
参数 options 为 object 类型,包含的属性值如下:
参数
类型
默认值
描述
sourceTextList
string[]
-
需要翻译的文本列表。
sourceLanguage
string
'auto'
文本源语言,一般使用默认自动检测。
targetLanguage
string
'zh'
翻译结果的语言。
返回值
Promise<string[]>
示例
// 当前支持的翻译语言
{ key: 'zh', label: ('中文') },
{ key: 'zh-TW', value: ('繁体中文') },
{ key: 'en', value: ('英文') },
{ key: 'ja', value: ('日语') },
{ key: 'ko', label: ('韩语') },
{ key: 'fr', label: ('法语') },
{ key: 'es', label: ('西班牙语') },
{ key: 'de', label: ('德语') },
{ key: 'ru', label: ('俄语') },
{ key: 'pt', label: ('葡萄牙语') },
{ key: 'vi', label: ('越南语') },
{ key: 'id', label: ('印尼语') },
{ key: 'th', label: ('泰语') },
{ key: 'ms', label: ('马来语') },
{ key: 'ar', label: ('阿拉伯语') },

import AIDeskCoreInstance from '@tencentcloud/ai-desk-core';

const translateResult = await AIDeskCoreInstance.chatService.translateText({
sourceTextList: [sourceText],
sourceLanguage: 'auto',
targetLanguage: 'zh',
});

翻译文本消息

调用此接口可翻译单条消息中的文本内容。翻译结果在页面刷新或切换客服前会缓存在翻译的 store 中。
注意:
使用此功能会进行计费,如需要请联系我们进行功能权限开通。
接口
AIDeskCoreInstance.chatService.translateTextMessage(options);
参数 options 为 object 类型,包含的属性值如下:
参数
类型
默认值
描述
message
IMessageModel
-
需要翻译的文本消息。
sourceLanguage
string
'auto'
文本源语言,一般使用默认自动检测。
targetLanguage
string
'zh'
翻译结果的语言。
返回值
Promise<string[]>
示例
// 当前支持的翻译语言
{ key: 'zh', label: ('中文') },
{ key: 'zh-TW', value: ('繁体中文') },
{ key: 'en', value: ('英文') },
{ key: 'ja', value: ('日语') },
{ key: 'ko', label: ('韩语') },
{ key: 'fr', label: ('法语') },
{ key: 'es', label: ('西班牙语') },
{ key: 'de', label: ('德语') },
{ key: 'ru', label: ('俄语') },
{ key: 'pt', label: ('葡萄牙语') },
{ key: 'vi', label: ('越南语') },
{ key: 'id', label: ('印尼语') },
{ key: 'th', label: ('泰语') },
{ key: 'ms', label: ('马来语') },
{ key: 'ar', label: ('阿拉伯语') },

import AIDeskCoreInstance from '@tencentcloud/ai-desk-core';

const translateResult = await AIDeskCoreInstance.chatService.translateTextMessage({
message: message,
sourceLanguage: 'auto',
targetLanguage: 'zh',
});

获取快捷回复

调用此接口可获取全部可用快捷回复及其分组信息。
接口
AIDeskCoreInstance.chatService.getAllAvailableQuickReply();
参数
返回值
Promise<QuickReplyItem[]>
QuickReplyItem 包含的属性值如下:
参数
类型
描述
groupId
string
快捷回复分组 ID。
groupName
string
快捷回复分组名称。
quickReplyList
object
快捷回复内容。
quickReplyList 包含的属性值如下:
参数
类型
描述
quickReplyId
string
快捷回复 ID。
groupId
string
快捷回复分组 ID。
title
string
快捷回复标题。
content
string
快捷回复内容。
format
string
快捷回复类型,包括'text'|'image'|'video'。
thumbImage
string | undefined
如果是富文本类型的快捷回复,此处是缩略图 url。
示例
import AIDeskCoreInstance from '@tencentcloud/ai-desk-core';

const quickReplies = await AIDeskCoreInstance.chatService.getAllAvailableQuickReply();

文本润色

调用此接口可使用 AI 根据会话的信息润色客服发出的消息文本。
接口
AIDeskCoreInstance.chatService.AIMessagePolish(options);
参数 options 为 object 类型,包含的属性值如下:
参数
类型
默认值
描述
sessionId
string
-
需要润色的会话 ID。
msgContent
string
-
需要润色的内容。
enhanceId
string
-
润色请求 ID。
返回值
Promise<object>
object 包含的属性值如下:
参数
类型
描述
enhanceId
string
润色请求 ID。
enhancedMsgContent
string
润色结果。
示例
import AIDeskCoreInstance from '@tencentcloud/ai-desk-core';

const polishResult = await AIDeskCoreInstance.chatService.AIMessagePolish({
sessionId: 'sessionId',
msgContent: 'text',
enhanceId: 'uuid', //因为润色是异步请求,所以在请求时需要传入一个 uuid 作为结果对应标识。
});

辅助回复

调用此接口可使用 AI 根据会话的信息生成辅助回复消息。
接口
AIDeskCoreInstance.chatService.AIAssistAnswer(options);
参数 options 为 object 类型,包含的属性值如下:
参数
类型
默认值
描述
sessionId
string
-
需要辅助回复的会话 ID。
replyId
string
-
当次请求的 uuid。
返回值
示例
import AIDeskCoreInstance from '@tencentcloud/ai-desk-core';

const polishResult = await AIDeskCoreInstance.chatService.AIAssistAnswer({
sessionId: 'sessionId',
replyId: 'uuid', //因为辅助回复是后台异步返回结果,所以在请求时需要传入一个 uuid 作为结果对应标识。
});