文档捉虫大赛:人工智能与机器学习专题> HOT

创建消息

创建文本消息

创建文本消息的接口,此接口返回一个消息实例,可以在需要发送文本消息时调用 发送消息 接口发送消息实例。
接口
chat.createTextMessage(options);
参数
参数 options 为 Object 类型,包含的属性值如下:
参数
类型
默认值
描述
to
String
-
消息接收方的 userID 或 groupID
conversationType
String
-
会话类型,取值TencentCloudChat.TYPES.CONV_C2C(端到端会话)或TencentCloudChat.TYPES.CONV_GROUP(群组会话)
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
消息优先级
payload
Object
-
消息内容的容器
cloudCustomData
String
''
消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
receiverList
Array | undefined
-
定向接收消息的群成员列表(社群和直播群不支持)
isSupportExtension
Boolean
false
是否支持消息扩展,true 支持 false 不支持(需要您购买旗舰版套餐)
payload 的描述如下:
参数
类型
描述
text
String
消息文本内容
返回值
Message
示例
// 发送文本消息,Web 端与小程序端相同
// 1. 创建消息实例,接口返回的实例可以上屏
let message = chat.createTextMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
// 消息优先级,用于群聊。如果某个群的消息超过了频率限制,后台会优先下发高优先级的消息
// priority: TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL,
payload: {
text: 'Hello world!'
},
// 如果您发消息需要已读回执,需购买旗舰版套餐,并且创建消息时将 needReadReceipt 设置为 true
needReadReceipt: true
// 消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
// cloudCustomData: 'your cloud custom data'
});
// 2. 发送消息
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});
// 发送群定向消息
// 注意:群定向消息不计入会话未读,receiverList 最大支持50个接收者。
let message = chat.createTextMessage({
to: 'test',
conversationType: TencentCloudChat.TYPES.CONV_GROUP,
payload: {
text: 'Hello world!'
},
// 如果您需要发群定向消息,需购买旗舰版套餐,并且创建消息时通过 receiverList 指定消息接收者
receiverList: ['user0', 'user1']
});
// 发送消息
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});

创建@消息

创建可以附带 @ 提醒功能的文本消息的接口,此接口返回一个消息实例,可以在需要发送文本消息时调用 发送消息 接口发送消息实例。
注意
此接口仅用于群聊,且社群和社群下的话题不支持 @ALL。
创建群 @ 消息不支持指定消息接收者。
接口
chat.createTextAtMessage(options);
参数
参数 options 为 Object 类型,包含的属性值如下:
参数
类型
默认值
描述
to
String
-
消息接收方的 userID 或 groupID
conversationType
String
-
会话类型,取值TencentCloudChat.TYPES.CONV_C2C(端到端会话)或TencentCloudChat.TYPES.CONV_GROUP(群组会话)
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
消息优先级
payload
Object
-
消息内容的容器
cloudCustomData
String
''
消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
payload 的描述如下:
参数
类型
描述
text
String
消息文本内容
atUserList
Array
需要 @ 的用户列表,如果需要 @ALL,请传入 TencentCloudChat.TYPES.MSG_AT_ALL 。 举个例子,假设该条文本消息希望 @ 提醒 denny 和 lucy 两个用户,同时又希望 @ 所有人,atUserList 传 ['denny', 'lucy', TencentCloudChat.TYPES.MSG_AT_ALL]
返回值
Message
示例
// 发送文本消息,Web 端与小程序端相同
// 1. 创建消息实例,接口返回的实例可以上屏
let message = chat.createTextAtMessage({
to: 'group1',
conversationType: TencentCloudChat.TYPES.CONV_GROUP,
// 消息优先级,用于群聊
// priority: TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL,
payload: {
text: '@denny @lucy @所有人 今晚聚餐,收到的请回复1',
// 'denny' 'lucy' 都是 userID,而非昵称
atUserList: ['denny', 'lucy', TencentCloudChat.TYPES.MSG_AT_ALL]
},
// 消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
// cloudCustomData: 'your cloud custom data'
});
// 2. 发送消息
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});

创建图片消息

创建图片消息的接口,此接口返回一个消息实例,可以在需要发送图片消息时调用 发送消息 接口发送消息实例。
注意:
在开通 云端审核 功能后,如果您发送的图片消息内容不合规,云端审核超时后 SDK 会触发 MESSAGE_MODIFIED 事件,回调的 message 对象 hasRiskContent 值为 true。
接口
chat.createImageMessage(options);
参数
参数 options 为 Object 类型,包含的属性值如下表所示:
参数
类型
默认值
描述
to
String
-
消息的接收方
conversationType
String
-
会话类型,取值 TencentCloudChat.TYPES.CONV_C2CTencentCloudChat.TYPES.CONV_GROUP
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
消息优先级
payload
Object
-
消息内容的容器
onProgress
function
-
获取上传进度的回调函数
cloudCustomData
String
''
消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
payload 的描述如下:
参数
类型
描述
file
HTMLInputElement | Object | File
用于选择图片的 DOM 节点(Web)或者 File 对象(Web)或者微信小程序 wx.chooseImage 接口的 success 回调参数。SDK 会读取其中的数据并上传图片
返回值
Message
示例
// Web 端发送图片消息示例1 - 传入 DOM 节点
// 1. 创建消息实例,接口返回的实例可以上屏
let message = chat.createImageMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
// 消息优先级,用于群聊
// priority: TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL,
payload: {
file: document.getElementById('imagePicker'),
},
// 消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
// cloudCustomData: 'your cloud custom data'
onProgress: function(event) { console.log('file uploading:', event) }
});

// 2. 发送消息
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});
// Web 端发送图片消息示例2- 传入 File 对象
// 先在页面上添加一个 id 为 "testPasteInput" 的消息输入框
// 如 <input type="text" id="testPasteInput" placeholder="截图后粘贴到输入框中" size="30" />
document.getElementById('testPasteInput').addEventListener('paste', 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;
}

// 1. 创建消息实例,接口返回的实例可以上屏
let message = chat.createImageMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
payload: {
file: file
},
onProgress: function(event) { console.log('file uploading:', event) }
});

// 2. 发送消息
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});
});
// 小程序端发送图片
// 1. 选择图片
wx.chooseImage({
sourceType: ['album'], // 从相册选择
count: 1, // 只选一张,目前 SDK 不支持一次发送多张图片
success: function (res) {
// 2. 创建消息实例,接口返回的实例可以上屏
let message = chat.createImageMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
payload: { file: res },
onProgress: function(event) { console.log('file uploading:', event) }
});
// 3. 发送图片
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});
}
})
// uni-app 发送图片
// 微信小程序从基础库 2.21.0 开始, wx.chooseImage 停止维护,请使用 uni.chooseMedia 代替
// 1. 选择图片
uni.chooseMedia({
count: 1,
mediaType: ['image'], // 图片
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album'], // 从相册选择
success: function(res) {
let message = chat.createImageMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
payload: { file: res },
onProgress: function(event) { console.log('file uploading:', event) }
});
// 2. 发送消息
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});
}
});

创建音频消息

创建音频消息实例的接口,此接口返回一个消息实例,可以在需要发送音频消息时调用 发送消息 接口发送消息。
注意:
在开通 云端审核 功能后,如果您发送的音频消息内容不合规,云端异步审核后 SDK 会触发 MESSAGE_MODIFIED 事件,回调的 message 对象 hasRiskContent 值为 true。
接口
chat.createAudioMessage(options);
参数
参数 options 为 Object 类型,包含的属性值如下:
参数
类型
默认值
描述
to
String
-
消息的接收方
conversationType
String
-
会话类型,取值 TencentCloudChat.TYPES.CONV_GROUPTencentCloudChat.TYPES.CONV_GROUP
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
消息优先级
payload
Object
-
消息内容的容器
cloudCustomData
String
''
消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
payload 的描述如下:
参数
类型
描述
file
Object
录音后得到的文件信息
返回值
Message
示例
// 示例:使用微信官方的 RecorderManager 进行录音
// 参考 https://developers.weixin.qq.com/minigame/dev/api/media/recorder/RecorderManager.start.html
// 1. 获取全局唯一的录音管理器 RecorderManager
const recorderManager = wx.getRecorderManager();

// 录音部分参数
const recordOptions = {
duration: 60000, // 录音的时长,单位 ms,最大值 600000(10 分钟)
sampleRate: 44100, // 采样率
numberOfChannels: 1, // 录音通道数
encodeBitRate: 192000, // 编码码率
format: 'aac' // 音频格式,选择此格式创建的音频消息,可以在即时通信 IM 全平台(Android、iOS、微信小程序和Web)互通
};

// 2.1 监听录音错误事件
recorderManager.onError(function(errMsg) {
console.warn('recorder error:', errMsg);
});
// 2.2 监听录音结束事件,录音结束后,调用 createAudioMessage 创建音频消息实例
recorderManager.onStop(function(res) {
console.log('recorder stop', res);

// 4. 创建消息实例,接口返回的实例可以上屏
const message = chat.createAudioMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
// 消息优先级,用于群聊
// priority: TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL,
payload: {
file: res
},
// 消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
// cloudCustomData: 'your cloud custom data'
});

// 5. 发送消息
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});
});

// 3. 开始录音
recorderManager.start(recordOptions);
// 在 Web 端创建语音消息并发送
// 示例:使用第三方库 js-audio-recorder 录制音频
// 1. 开始录制
let recorder = new Recorder({
// 采样位数,支持 8 或 16,默认是16
sampleBits: 16,
// 采样率,支持 11025、16000、22050、24000、44100、48000,根据浏览器默认值,我的chrome是48000
sampleRate: 16000,
// 声道,支持 1 或 2, 默认是1
numChannels: 1,
});
let startTs;
recorder.start().then(() => {
// 开始录音,记录起始时间戳
startTs = Date.now();
}, (error) => {
// 出错了
console.log(`${error.name} : ${error.message}`);
});

// 2. 结束录制
recorder.stop();

// 3. 计算录音时长,获取 WAV 数据
let duration = Date.now() - startTs; // 单位:ms
let wavBlob = recorder.getWAVBlob();

// 4. blob 数据转成 File 对象
let audioFile = new File([wavBlob], 'hello.wav', { type: 'wav' });
audioFile.duration = duration;

// 5. 创建音频消息
let message = chat.createAudioMessage({
to: 'user1',
conversationType: 'C2C',
payload: {
file: audioFile
}
});

// 6. 发送消息
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});

创建视频消息

创建视频消息实例的接口,此接口返回一个消息实例,可以在需要发送视频消息时调用 发送消息 接口发送消息。
注意:
在开通 云端审核 功能后,如果您发送的视频消息内容不合规,云端异步审核后 SDK 会触发 MESSAGE_MODIFIED 事件,回调的 message 对象 hasRiskContent 值为 true。
接口
chat.createVideoMessage(options);
参数
参数 options 为 Object 类型,包含的属性值如下:
参数
类型
默认值
描述
to
String
-
消息的接收方
conversationType
String
-
会话类型,取值TencentCloudChat.TYPES.CONV_GROUPTencentCloudChat.TYPES.CONV_GROUP
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
消息优先级
payload
Object
-
消息内容的容器
cloudCustomData
String
''
消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
payload 的描述如下:
参数
类型
描述
file
HTMLInputElement | File | Object
自定义消息的数据字段
返回值
Message
示例
// 小程序端发送视频消息示例:
// 接口详情请查阅 https://developers.weixin.qq.com/miniprogram/dev/api/media/video/wx.chooseVideo.html
// 1. 调用小程序接口选择视频
wx.chooseVideo({
sourceType: ['album', 'camera'], // 来源相册或者拍摄
maxDuration: 60, // 设置最长时间60s
camera: 'back', // 后置摄像头
success (res) {
// 2. 创建消息实例,接口返回的实例可以上屏
let message = chat.createVideoMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
// 消息优先级,用于群聊
// priority: TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL,
payload: {
file: res
},
// 消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
// cloudCustomData: 'your cloud custom data'
onProgress: function(event) { console.log('file uploading:', event) }
})
// 3. 发送消息
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});
}
})
// web 端发送视频消息示例
// 1. 获取视频:传入 DOM 节点
// 2. 创建消息实例
const message = chat.createVideoMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
payload: {
file: document.getElementById('videoPicker') // 或者用event.target
},
onProgress: function(event) { console.log('file uploading:', event) }
});
// 3. 发送消息
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});
// uni-app 发送视频
// 1. 选择视频
uni.chooseVideo({
count: 1,
sourceType: ['camera', 'album'], // album 从相册选视频,camera 使用相机拍摄,默认为:['album', 'camera']
maxDuration: 60, // 设置最长时间60s
camera: 'back', // 后置摄像头
success: function(res) {
let message = chat.createVideoMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
payload: { file: res },
onProgress: function(event) { console.log('file uploading:', event) }
});
// 2. 发送消息
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});
}
})

创建自定义消息

创建自定义消息实例的接口,此接口返回一个消息实例,可以在需要发送自定义消息时调用 发送消息 接口发送消息实例。当 SDK 提供的能力不能满足您的需求时,可以使用自定义消息进行个性化定制。
接口
chat.createCustomMessage(options);
参数
参数 options 为 Object 类型,包含的属性值如下:
参数
类型
默认值
描述
to
String
-
消息接收方的 userID 或 groupID
conversationType
String
-
会话类型,取值TencentCloudChat.TYPES.CONV_GROUPTencentCloudChat.TYPES.CONV_GROUP
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
消息优先级
payload
Object
-
消息内容的容器
cloudCustomData
String
''
消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
payload 的描述如下:
参数
类型
描述
data
String
自定义消息的数据字段
description
String
自定义消息的说明字段
extension
String
自定义消息的扩展字段
返回值
Message
示例
// 示例:利用自定义消息实现投骰子功能
// 1. 定义随机函数
function random(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
// 2. 创建消息实例,接口返回的实例可以上屏
let message = chat.createCustomMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
// 消息优先级,用于群聊
// priority: TencentCloudChat.TYPES.MSG_PRIORITY_HIGH,
payload: {
data: 'dice', // 用于标识该消息是骰子类型消息
description: String(random(1,6)), // 获取骰子点数
extension: ''
}
});
// 3. 发送消息
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});

创建表情消息

创建表情消息实例的接口,此接口返回一个消息实例,可以在需要发送表情消息时调用 发送消息 接口发送消息。
接口
chat.createFaceMessage(options);
参数
参数 options 为 Object 类型,包含的属性值如下:
参数
类型
默认值
描述
to
String
-
消息接收方的 userID 或 groupID
conversationType
String
-
会话类型,取值 TencentCloudChat.TYPES.CONV_C2CTencentCloudChat.TYPES.CONV_GROUP
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
消息优先级
payload
Object
-
消息内容的容器
cloudCustomData
String
''
消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
payload 的描述如下:
参数
类型
描述
index
Number
表情索引,用户自定义
data
String
额外数据
返回值
Message
示例
// 发送表情消息,Web端与小程序端相同。
// 1. 创建消息实例,接口返回的实例可以上屏
let message = chat.createFaceMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
// 消息优先级,用于群聊
// priority: TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL,
payload: {
index: 1, // Number 表情索引,用户自定义
data: 'tt00' // String 额外数据
},
// 消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
// cloudCustomData: 'your cloud custom data'
});
// 2. 发送消息
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});

创建文件消息

创建文件消息的接口,此接口返回一个消息实例,可以在需要发送文件消息时调用 发送消息 接口发送消息实例。
接口
chat.createFileMessage(options);
参数
参数 options 为 Object 类型,包含的属性值如下:
参数
类型
默认值
描述
to
String
-
消息接收方的 userID 或 groupID
conversationType
String
-
会话类型,取值 TencentCloudChat.TYPES.CONV_C2CTencentCloudChat.TYPES.CONV_GROUP
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
消息优先级
payload
Object
-
消息内容的容器
onProgress
function
-
获取上传进度的回调函数
cloudCustomData
String
''
消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
payload 的描述如下:
参数
类型
描述
file
HTMLInputElement | File | Object
用于选择文件的 DOM 节点(Web)或者 File 对象(Web)或者 Object(uni.chooseFile 接口的 success 回调参数),SDK 会读取其中的数据并上传文件
返回值
Message
示例
// Web 端发送文件消息示例1 - 传入 DOM 节点
// 1. 创建文件消息实例,接口返回的实例可以上屏
let message = chat.createFileMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
// 消息优先级,用于群聊
// priority: TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL,
payload: {
file: document.getElementById('filePicker'),
},
// 消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
// cloudCustomData: 'your cloud custom data'
onProgress: function(event) { console.log('file uploading:', event) }
});

// 2. 发送消息
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});
// Web 端发送文件消息示例2- 传入 File 对象
// 先在页面上添加一个 id 为 "testPasteInput" 的消息输入框
// 如 <input type="text" id="testPasteInput" placeholder="截图后粘贴到输入框中" size="30" />
document.getElementById('testPasteInput').addEventListener('paste', 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;
}

// 1. 创建消息实例,接口返回的实例可以上屏
let message = chat.createFileMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
payload: {
file: file
},
onProgress: function(event) { console.log('file uploading:', event) }
});

// 2. 发送消息
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});
});
// uni-app 发送文件
// 1. 选择文件
uni.chooseFile({
count: 1,
extension:['.zip','.doc'],
success: function(res) {
let message = chat.createFileMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
payload: { file: res },
onProgress: function(event) { console.log('file uploading:', event) }
});
// 2. 发送消息
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});
}
});
// 手机端微信小程序或 QQ 小程序法发送文件
// wx.chooseMessageFile 基础库 2.5.0 开始支持,低版本需做兼容处理
// qq.chooseMessageFile 基础库 1.18.0 开始支持,低版本需做兼容处理
// 1. 从客户端会话选择文件
wx.chooseMessageFile({
count: 1,
type: 'all', // 从所有文件选择
success: (res) => {
const message = chat.createFileMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
payload: { file: res },
onProgress: function(event) { console.log('file uploading:', event) }
});
// 2. 发送消息
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});
}
})

创建地理位置消息

创建地理位置消息的接口,此接口返回一个消息实例,可以在需要发送地理位置消息时调用 发送消息 接口发送消息。
接口
chat.createLocationMessage(options);
参数
参数 options 为 Object 类型,包含的属性值如下:
参数
类型
默认值
描述
to
String
-
消息接收方的 userID 或 groupID
conversationType
String
-
会话类型,取值 TencentCloudChat.TYPES.CONV_C2CTencentCloudChat.TYPES.TYPES.CONV_GROUP
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
消息优先级
payload
Object
-
消息内容的容器
cloudCustomData
String
''
消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
payload 的描述如下:
参数
类型
描述
description
String
地理位置描述信息
longitude
Number
经度
latitude
Number
纬度
返回值
Message
示例
// 发送地理位置消息,Web 端与小程序端相同
// 1. 创建消息实例,接口返回的实例可以上屏
let message = chat.createLocationMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
// 消息优先级,用于群聊
// priority: TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL,
payload: {
description: '深圳市深南大道10000号腾讯大厦',
longitude: 113.941079, // 经度
latitude: 22.546103 // 纬度
}
});
// 2. 发送消息
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});

创建合并消息

创建合并消息的接口,此接口返回一个消息实例,可以在需要发送合并消息时调用 发送消息 接口发送消息。
注意
不支持合并已发送失败的消息,如果消息列表内传入了已发送失败的消息,则创建消息接口会报错。
接口
chat.createMergerMessage(options);
参数
参数 options 为 Object 类型,包含的属性值如下:
参数
类型
默认值
描述
to
String
-
消息接收方的 userID 或 groupID
conversationType
String
-
会话类型,取值 TencentCloudChat.TYPES.CONV_C2CTencentCloudChat.TYPES.CONV_GROUP
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
消息优先级
payload
Object
-
消息内容的容器
cloudCustomData
String
''
消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
payload 的描述如下:
参数
类型
描述
messageList
Array
合并的消息列表
title
String
合并的标题,例如:"大湾区前端人才中心的聊天记录"
abstractList
String
摘要列表,不同的消息类型可以设置不同的摘要信息,例如:文本消息可以设置为:sender: text,图片消息可以设置为:sender: [图片],文件消息可以设置为:sender: [文件]
compatibleText
String
兼容文本,低版本 SDK 如果不支持合并消息,默认会收到一条文本消息,文本消息的内容为 ${compatibleText},必填
返回值
Message
示例
// 1. 将群聊消息转发到 c2c 会话
// message1 message2 message3 是群聊消息
let mergerMessage = chat.createMergerMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
payload: {
messageList: [message1, message2, message3],
title: '大湾区前端人才中心的聊天记录',
abstractList: ['allen: 666', 'iris: [图片]', 'linda: [文件]'],
compatibleText: '请升级IMSDK到v2.10.1或更高版本查看此消息'
},
// 消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
// cloudCustomData: 'your cloud custom data'
});

// 2. 发送消息
let promise = chat.sendMessage(mergerMessage);
promise.then(function(imResponse) {
// 发送成功
console.log(imResponse);
}).catch(function(imError) {
// 发送失败
console.warn('sendMessage error:', imError);
});

下载合并消息

如果发送方发送的合并消息较大,SDK 会将此消息存储到云端,消息接收方查看消息时,需要先把消息从云端下载到本地。
接口
chat.downloadMergerMessage(message);
参数
参数
类型
描述
message
Message
消息实例
返回值
Promise
示例
// downloadKey 存在说明收到的合并消息存储在云端,需要先下载
if (message.type === TencentCloudChat.TYPES.MSG_MERGER && message.payload.downloadKey !== '') {
let promise = chat.downloadMergerMessage(message);
promise.then(function(imResponse) {
// 下载成功后,SDK会更新 message.payload.messageList 等信息
console.log(imResponse.data);
}).catch(function(imError) {
// 下载失败
console.warn('downloadMergerMessage error:', imError);
});
}

逐条转发消息

如果您需要转发单条消息,可以先通过 createForwardMessage 接口创建一条和原消息内容完全一样的转发消息,再调用 sendMessage 接口把转发消息发送出去。
注意
1. 支持单条转发和逐条转发。
接口