对话消息结构
微语系统支持多种消息类型,满足不同场景下的沟通需求。每种消息类型有其特定的数据结构和显示方式。
消息发送函数说明
本文档中所有代码示例都使用 mqttSendMessage 函数来发送消息。该函数的详细实现和使用方法请参考 MQTT长连接文档。
函数签名:
mqttSendMessage(messageUid, messageType, messageContent, currentThread)
参数说明:
messageUid: 消息唯一标识符messageType: 消息类型(如 "TEXT", "IMAGE", "FILE" 等)messageContent: 消息内容(文本消息为字符串,其他类型为JSON字符串)currentThread: 当前会话对象
消息基础结构
所有消息都遵循统一的基础数据结构:
type MessageResponse = {
uid?: string; // 消息唯一标识
type: string; // 消息类型
content?: string; // 消息内容(JSON字符串或纯文本)
status: string; // 消息状态
createdAt?: string; // 创建时间
channel?: string; // 发送渠道
extra?: MessageExtra; // 扩展信息
timestamp?: number; // 时间戳
thread?: THREAD.ThreadResponse; // 会话信息
user?: USER.UserProtobuf; // 用户信息
};
type MessageExtra = {
translation?: string; // 翻译内容
orgUid?: string; // 企业ID
};
消息类型
文本消息
最基础的消息类型,用于发送纯文本内容。文本消息的content字段直接存储字符串,无需JSON格式包装。
代码示例:
// 发送文本消息
const sendTextMessage = (content) => {
const message = {
uid: generateMessageId(),
type: "TEXT",
content: content, // 直接传入字符串,不需要JSON.stringify
status: "SENDING",
createdAt: new Date().toISOString(),
channel: "web"
};
mqttSendMessage(message.uid, message.type, message.content, currentThread);
};
// 使用示例
sendTextMessage("您好,请问有什么可以帮助您的吗?");
使用场景:
- 一般文字交流
- 简单问答
- 指令发送
图片消息
用于发送和显示图片。
数据结构:
type ImageContent = {
url: string; // 图片URL
width?: string; // 图片宽度
height?: string; // 图片高度
label?: string; // 图片标签/说明
mimeType?: string; // MIME类型 (如: image/jpeg, image/png)
size?: string; // 文件大小 (字节)
hash?: string; // 文件哈希值 (SHA256)
thumbnail?: string; // 缩略图URL
filename?: string; // 文件名
};
代码示例:
// 发送图片消息
const sendImageMessage = (imageUrl, options = {}) => {
const imageContent = {
url: imageUrl,
width: options.width || "300",
height: options.height || "200",
mimeType: options.mimeType || "image/jpeg",
size: options.size,
filename: options.filename,
label: options.label || "图片"
};
const message = {
uid: generateMessageId(),
type: "IMAGE",
content: JSON.stringify(imageContent),
status: "SENDING",
createdAt: new Date().toISOString(),
channel: "web"
};
mqttSendMessage(message.uid, message.type, message.content, currentThread);
};
// 使用示例
sendImageMessage("https://example.com/product.jpg", {
width: "400",
height: "300",
filename: "product.jpg",
label: "产品展示图"
});
使用场景:
- 产品展示
- 问题截图
- 图片资料分享
语音消息
用于发送语音录音。
数据结构:
type VoiceContent = {
url: string; // 语音文件URL
duration?: string; // 语音时长(秒)
format?: string; // 文件格式(如ogg, mp3)
caption?: string; // 语音说明文字
label?: string; // 语音标签
};
代码示例:
// 发送语音消息
const sendVoiceMessage = (voiceUrl, duration, options = {}) => {
const voiceContent = {
url: voiceUrl,
duration: duration.toString(),
format: options.format || "ogg",
caption: options.caption,
label: options.label || "语音消息"
};
const message = {
uid: generateMessageId(),
type: "VOICE",
content: JSON.stringify(voiceContent),
status: "SENDING",
createdAt: new Date().toISOString(),
channel: "web"
};
mqttSendMessage(message.uid, message.type, message.content, currentThread);
};
// 使用示例
sendVoiceMessage("https://example.com/voice.ogg", 15, {
format: "ogg",
caption: "语音回复",
label: "客服语音"
});
使用场景:
- 语音留言
- 语音回复
- 语音指导
视频消息
用于发送视频内容。
数据结构:
type VideoContent = {
url: string; // 视频URL
coverUrl?: string; // 视频封面URL
duration?: string; // 视频时长 (秒)
width?: string; // 视频宽度
height?: string; // 视频高度
format?: string; // 视频格式 ( 兼容性字段)
mimeType?: string; // MIME类型 (如: video/mp4, video/avi)
label?: string; // 视频标签/说明
size?: string; // 文件大小 (字节)
hash?: string; // 文件哈希值 (SHA256)
filename?: string; // 文件名
caption?: string; // 视频说明文字
};
代码示例:
// 发送视频消息
const sendVideoMessage = (videoUrl, options = {}) => {
const videoContent = {
url: videoUrl,
coverUrl: options.coverUrl,
duration: options.duration?.toString(),
width: options.width || "640",
height: options.height || "480",
mimeType: options.mimeType || "video/mp4",
size: options.size,
filename: options.filename,
caption: options.caption,
label: options.label || "视频"
};
const message = {
uid: generateMessageId(),
type: "VIDEO",
content: JSON.stringify(videoContent),
status: "SENDING",
createdAt: new Date().toISOString(),
channel: "web"
};
mqttSendMessage(message.uid, message.type, message.content, currentThread);
};
// 使用示例
sendVideoMessage("https://example.com/demo.mp4", {
coverUrl: "https://example.com/cover.jpg",
duration: 120,
width: "800",
height: "600",
filename: "product_demo.mp4",
caption: "产品演示视频"
});
使用场景:
- 产品演示视频
- 操作指导视频
- 视频反馈
文件消息
用于发送各类文件。
数据结构:
type FileContent = {
url: string; // 文件URL
name?: string; // 文件名称
size?: string; // 文件大小 (字节)
type?: string; // 文件MIME类型
label?: string; // 文件标签/说明
hash?: string; // 文件哈希值 (SHA256)
filename?: string; // 文件名 (兼容性字段)
};
代码示例:
// 发送文件消息
const sendFileMessage = (fileUrl, options = {}) => {
const fileContent = {
url: fileUrl,
name: options.name || options.filename,
size: options.size?.toString(),
type: options.type || "application/octet-stream",
label: options.label || "文件",
hash: options.hash,
filename: options.filename
};
const message = {
uid: generateMessageId(),
type: "FILE",
content: JSON.stringify(fileContent),
status: "SENDING",
createdAt: new Date().toISOString(),
channel: "web"
};
mqttSendMessage(message.uid, message.type, message.content, currentThread);
};
// 使用示例
sendFileMessage("https://example.com/contract.pdf", {
name: "服务合同.pdf",
size: "1024000",
type: "application/pdf",
label: "合同文件",
filename: "contract.pdf"
});
使用场景:
- 文档传输
- 合同发送
- 资料共享
文档消息
专门用于各类文档的发送和显示。
数据结构:
type DocumentContent = {
url: string; // 文档文件URL
name?: string; // 文件名称
size?: string; // 文件大小 (字节)
type?: string; // 文件MIME类型
caption?: string; // 文档说明文字
thumbnail?: string; // 缩略图URL
label?: string; // 文档标签
hash?: string; // 文件哈希值 (SHA256)
filename?: string; // 文件名 (兼容性字段)
};
代码示例:
// 发送文档消息
const sendDocumentMessage = (docUrl, options = {}) => {
const documentContent = {
url: docUrl,
name: options.name,
size: options.size?.toString(),
type: options.type || "application/pdf",
caption: options.caption,
thumbnail: options.thumbnail,
label: options.label || "文档",
hash: options.hash,
filename: options.filename
};
const message = {
uid: generateMessageId(),
type: "DOCUMENT",
content: JSON.stringify(documentContent),
status: "SENDING",
createdAt: new Date().toISOString(),
channel: "web"
};
mqttSendMessage(message.uid, message.type, message.content, currentThread);
};
// 使用示例
sendDocumentMessage("https://example.com/manual.pdf", {
name: "用户手册.pdf",
size: "2048000",
type: "application/pdf",
caption: "产品使用手册,请仔细阅读",
thumbnail: "https://example.com/manual_thumb.jpg",
label: "用户手册"
});
使用场景:
- PDF文档共享
- Office文档分享
- 技术文档传输
音频消息
用于发送音频文件。
数据结构:
type AudioContent = {
url: string; // 音频URL
duration?: string; // 音频时长 (秒)
format?: string; // 音频格式 (兼容性字段)
mimeType?: string; // MIME类型 (如: audio/mp3, audio/wav)
label?: string; // 音频标签/说明
size?: string; // 文件大小 (字节)
hash?: string; // 文件哈希值 (SHA256)
filename?: string; // 文件名
caption?: string; // 音频说明文字
};
代码示例:
// 发送音频消息
const sendAudioMessage = (audioUrl, options = {}) => {
const audioContent = {
url: audioUrl,
duration: options.duration?.toString(),
mimeType: options.mimeType || "audio/mp3",
label: options.label || "音频",
size: options.size?.toString(),
filename: options.filename,
caption: options.caption
};
const message = {
uid: generateMessageId(),
type: "AUDIO",
content: JSON.stringify(audioContent),
status: "SENDING",
createdAt: new Date().toISOString(),
channel: "web"
};
mqttSendMessage(message.uid, message.type, message.content, currentThread);
};
// 使用示例
sendAudioMessage("https://example.com/audio.mp3", {
duration: 180,
mimeType: "audio/mp3",
filename: "background_music.mp3",
caption: "背景音乐文件",
label: "音频文件"
});
使用场景:
- 音乐分享
- 音频资料