外观
发送消息
约 342 字大约 1 分钟
2025-03-25
1. 描述
sendMessage()
发送消息,该方法为异步函数
,会返回一个Promise
对象,可以通过该对象获取发送结果。
2. 参数
参数名称 | 是否必选 | 类型 | 说明 |
---|---|---|---|
message | 是 | Message | 必须是通过createTextMessage() 、createImageMessage() 等方法创建的消息对象 |
3. 返回值
返回一个 Promise
对象,群聊和单聊返回的数据结构不一样,具体如下:
3.1 群聊
字段 | 类型 | 说明 |
---|---|---|
id | Number | 消息ID |
groupId | String | 群聊ID |
type | String | 消息类型,详见消息类型 |
senderId | String | 发送者ID |
senderName | String | 发送者名称 |
senderAvatar | String | 发送者头像 |
content | String | 消息对象字符串 |
conversationId | String | 会话ID,即群聊ID |
conversationType | String | 会话类型,固定为group |
3.2 单聊
字段 | 类型 | 说明 |
---|---|---|
id | Number | 消息ID |
senderId | String | 发送者ID |
senderName | String | 发送者名称 |
senderAvatar | String | 发送者头像 |
receiverId | String | 接收者ID |
receiverName | String | 接收者名称 |
receiverAvatar | String | 接收者头像 |
content | String | 消息对象字符串 |
conversationId | String | 会话ID,即群聊ID |
conversationType | String | 会话类型,固定为single |
4. 示例代码
const message = XBCIM.sendImageMessage({
target: 'group-id',
url: 'https://example.com/image.jpg',
thumbUrl: 'https://example.com/image-thumb.jpg',
width: 100,
height: 100,
size: 1024,
name: 'image.jpg',
conversationType: XBCIM.CONVERSATION_TYPE.GROUP,
quote: {
id: 'xxxxxx',
... // 完整的消息体
},
at: [
{
userId: 'user-id',
... // 用户对象
},
]
})
XBCIM.sendMessage(message).then(result => {
console.log('消息发送成功', result)
}).catch(error => {
console.error(error)
})