Send a Message

Creating a Message

Creating a text message

This API is used to create a text message. It returns a message instance, which can be sent by calling the sendMessage API when you need to send a text message.
API
chat.createTextMessage(options);
Parameter
The options parameter is of the Object type. It contains the following attribute values:
Name
Type
Default
Description
to
String
-
userID or groupID of the message receiver
conversationType
String
-
Conversation type. Valid values:
TencentCloudChat.TYPES.CONV_C2C (one-to-one conversation)
TencentCloudChat.TYPES.CONV_GROUP (group conversation)
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
Message priority. If messages in a group exceed the frequency limit, the backend will deliver high-priority messages first. Supported enumerated values:
TencentCloudChat.TYPES.MSG_PRIORITY_HIGH
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL (default)
TencentCloudChat.TYPES.MSG_PRIORITY_LOW
TencentCloudChat.TYPES.MSG_PRIORITY_LOWEST
payload
Object
-
Message content container
cloudCustomData
String
''
Custom message data, which is saved in the cloud, will be sent to the receiver, and can still be pulled after the application is uninstalled and reinstalled. This attribute is supported by v2.10.2 or later
receiverList
Array | undefined

The list of group members who can receive targeted messages (not applicable to Community group and Audio-video group)
isSupportExtension
Boolean
false
Whether message extensions are supported: true (supported) or false (not supported) (requires you to purchase the flagship package)
The payload is as described below:
Name
Type
Description
text
String
Message text content
Returned value
Message
Sample
// 1. Create a message instance. The returned instance can be displayed on the screen.
let message = chat.createTextMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
// priority: TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL,
payload: {
text: 'Hello world!'
},
// To use the read receipt feature, you need to purchase the Premium edition
// and set `needReadReceipt` to `true` when creating a message.
needReadReceipt: true
// cloudCustomData: 'your cloud custom data'
});
// 2. Send the message.
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// The message sent successfully
console.log(imResponse);
}).catch(function(imError) {
// Failed to send the message
console.warn('sendMessage error:', imError);
});

Creating an @ message

This API is used to create an @ text message. It returns a message instance, which can be sent by calling the sendMessage API when you need to send a text message.
Note
1. It applies only to group chats, and @all members is not supported for a community and its topic.
API
chat.createTextAtMessage(options);
Parameter
The options parameter is of the Object type. It contains the following attribute values:
Name
Type
Default
Description
to
String
-
userID or groupID of the message receiver
conversationType
String
-
Conversation type. Valid values:
TencentCloudChat.TYPES.CONV_GROUP (group conversation)
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
Message priority. If messages in a group exceed the frequency limit, the backend will deliver high-priority messages first. Supported enumerated values:
TencentCloudChat.TYPES.MSG_PRIORITY_HIGH
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL (default)
TencentCloudChat.TYPES.MSG_PRIORITY_LOW
TencentCloudChat.TYPES.MSG_PRIORITY_LOWEST
payload
Object
-
Message content container
cloudCustomData
String
''
Custom message data, which is saved in the cloud, will be sent to the receiver, and can still be pulled after the application is uninstalled and reinstalled.
The payload is as described below:
Name
Type
Description
text
String
Text content
atUserList
Array
List of users that need to be mentioned (@). To mention (@) all, pass in TencentCloudChat.TYPES.MSG_AT_ALL. For example, to mention (@) denny and lucy as well as all members, pass in ['denny', 'lucy', TencentCloudChat.TYPES.MSG_AT_ALL] for atUserList.
Returned value
Sample
// 1. Create a message instance. The returned instance can be displayed on the screen.
let message = chat.createTextAtMessage({
to: 'group1',
conversationType: TencentCloudChat.TYPES.CONV_GROUP,
// priority: TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL,
payload: {
text: '@denny @lucy @all Dinner tonight. Reply 1 if you receive this message',
// 'denny' and 'lucy' are `userID` values, not nicknames
atUserList: ['denny', 'lucy', TencentCloudChat.TYPES.MSG_AT_ALL]
},
// cloudCustomData: 'your cloud custom data'
});
// 2. Send the message.
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// The message sent successfully
console.log(imResponse);
}).catch(function(imError) {
// Failed to send the message
console.warn('sendMessage error:', imError);
});

Creating an image message

This API is used to create an image message. It returns a message instance, which can be sent by calling the sendMessage API when you need to send an image message.
API
chat.createImageMessage(options);
Parameter
The options parameter is of the Object type. It contains the following attribute values:
Name
Type
Default
Description
to
String
-
Message receiver
conversationType
String
-
Conversation type. Valid values: TencentCloudChat.TYPES.CONV_C2C, TencentCloudChat.TYPES.CONV_GROUP.
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
Message priority. If messages in a group exceed the frequency limit, the backend will deliver high-priority messages first. Supported enumerated values:
TencentCloudChat.TYPES.MSG_PRIORITY_HIGH
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL (default)
TencentCloudChat.TYPES.MSG_PRIORITY_LOW
TencentCloudChat.TYPES.MSG_PRIORITY_LOWEST
payload
Object
-
Message content container
onProgress
function
-
Callback function for getting the upload progress
cloudCustomData
String
''
Custom message data, which is saved in the cloud, will be sent to the receiver, and can still be pulled after the application is uninstalled and reinstalled.
The payload is as described below:
Name
Type
Description
file
HTMLInputElement | Object
It is used to select a DOM node (web) or File object (web) of the image. The SDK reads the data contained in this parameter and uploads the image.
Returned value
Message
Sample
// Sample 1: Sending an image message on web - passing in a DOM node
// 1. Create a message instance. The returned instance can be displayed on the screen.
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. Send the message.
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// The message sent successfully
console.log(imResponse);
}).catch(function(imError) {
// Failed to send the message
console.warn('sendMessage error:', imError);
});
// Sample 2: Sending an image message on web - passing in a File object
// Add a message input box with the ID set to `testPasteInput`
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];
// After the image message is sent successfully
// the content pointed to by `file` may be cleared by the browser.
// If you have extra rendering needs, you can copy the data in advance.
fileCopy = file.slice();
}

if (typeof file === 'undefined') {
console.warn('The `file` is `undefined`. Check for the compatibility of the code or browser.');
return;
}

// 1. Create a message instance. The returned instance can be displayed on the screen.
let message = chat.createImageMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
payload: {
file: file
},
onProgress: function(event) { console.log('file uploading:', event) }
});

// 2. Send the message.
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// The message sent successfully
console.log(imResponse);
}).catch(function(imError) {
// Failed to send the message
console.warn('sendMessage error:', imError);
});
});

Creating an audio message

This API is used to create an audio message. It returns a message instance, which can be sent by calling the sendMessage API when you need to send an audio message.
API
chat.createAudioMessage(options);
Parameter
The options parameter is of the Object type. It contains the following attribute values:
Name
Type
Default
Description
to
String
-
Message receiver
conversationType
String
-
Conversation type. Valid values:
TencentCloudChat.TYPES.CONV_C2C
TencentCloudChat.TYPES.CONV_GROUP
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
Message priority. If messages in a group exceed the frequency limit, the backend will deliver high-priority messages first. Supported enumerated values:
TencentCloudChat.TYPES.MSG_PRIORITY_HIGH
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL (default)
TencentCloudChat.TYPES.MSG_PRIORITY_LOW
TencentCloudChat.TYPES.MSG_PRIORITY_LOWEST
payload
Object
-
Message content container
cloudCustomData
String
''
Custom message data, which is saved in the cloud, will be sent to the receiver, and can still be pulled after the application is uninstalled and reinstalled.
The payload is as described below:
Name
Type
Description
file
Object
File information obtained after recording
Returned value
Message

Creating a video message

This API is used to create a video message. It returns a message instance, which can be sent by calling the sendMessage API when you need to send a video message.
API
chat.createVideoMessage(options);
Parameter
The options parameter is of the Object type. It contains the following attribute values:
Name
Type
Default
Description
to
String
-
Message receiver
conversationType
String
-
Conversation type. Valid values:
TencentCloudChat.TYPES.CONV_C2C
TencentCloudChat.TYPES.CONV_GROUP
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
Message priority. If messages in a group exceed the frequency limit, the backend will deliver high-priority messages first. Supported enumerated values:
TencentCloudChat.TYPES.MSG_PRIORITY_HIGH
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL (default)
TencentCloudChat.TYPES.MSG_PRIORITY_LOW
TencentCloudChat.TYPES.MSG_PRIORITY_LOWEST
payload
Object
-
Message content container
cloudCustomData
String
''
Custom message data, which is saved in the cloud, will be sent to the receiver, and can still be pulled after the application is uninstalled and reinstalled.
The payload is as described below:
Name
Type
Description
file
HTMLInputElement | File | Object
Data field of the custom message
Returned value
Message
Sample
// Sample: Sending a video message on web
// 1. Get the video file by passing in the DOM node.
// 2. Create a message instance.
const message = chat.createVideoMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
payload: {
file: document.getElementById('videoPicker') // Alternatively, use `event.target`
},
onProgress: function(event) { console.log('file uploading:', event) }
});
// 3. Send the message.
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// The message sent successfully
console.log(imResponse);
}).catch(function(imError) {
// Failed to send the message
console.warn('sendMessage error:', imError);
});

Creating a custom message

This API is used to create a custom message. It returns a message instance, which can be sent by calling the sendMessage API when you need to send a custom message. When the current SDK capabilities cannot meet your needs, you can customize messages.
API
chat.createCustomMessage(options);
Parameter
The options parameter is of the Object type. It contains the following attribute values:
Name
Type
Default
Description
to
String
-
userID or groupID of the message receiver
conversationType
String
-
Conversation type. Valid values:
TencentCloudChat.TYPES.CONV_C2C (one-to-one conversation)
TencentCloudChat.TYPES.CONV_GROUP (group conversation)
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
Message priority. If messages in a group exceed the frequency limit, the backend will deliver high-priority messages first. Supported enumerated values:
TencentCloudChat.TYPES.MSG_PRIORITY_HIGH
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL (default)
TencentCloudChat.TYPES.MSG_PRIORITY_LOW
TencentCloudChat.TYPES.MSG_PRIORITY_LOWEST
payload
Object
-
Message content container
cloudCustomData
String
''
Custom message data, which is saved in the cloud, will be sent to the receiver, and can still be pulled after the application is uninstalled and reinstalled.
The payload is as described below:
Name
Type
Description
data
String
Data of the custom message
description
String
Description of the custom message
extension
String
Extension of the custom message
Returned value
Message
Sample
// Sample: Using a custom message to implement dice rolling
// 1. Define the random function.
function random(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
// 2. Create a message instance. The returned instance can be displayed on the screen.
let message = chat.createCustomMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
payload: {
data: 'dice', // Used to identify the message as a dice message
description: String(random(1,6)), // Get the outcome
extension: ''
}
});
// 3. Send the message.
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// The message sent successfully
console.log(imResponse);
}).catch(function(imError) {
// Failed to send the message
console.warn('sendMessage error:', imError);
});

Creating an emoji message

This API is used to create an emoji message. It returns a message instance, which can be sent by calling the sendMessage API when you need to send an emoji message.
API
chat.createFaceMessage(options);
Parameter
The options parameter is of the Object type. It contains the following attribute values:
Name
Type
Default
Description
to
String
-
userID or groupID of the message receiver
conversationType
String
-
Conversation type. Valid values:
TencentCloudChat.TYPES.CONV_C2C (one-to-one conversation)
TencentCloudChat.TYPES.CONV_GROUP (group conversation)
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
Message priority. If messages in a group exceed the frequency limit, the backend will deliver high-priority messages first. Supported enumerated values:
TencentCloudChat.TYPES.MSG_PRIORITY_HIGH
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL (default)
TencentCloudChat.TYPES.MSG_PRIORITY_LOW
TencentCloudChat.TYPES.MSG_PRIORITY_LOWEST
payload
Object
-
Message content container
cloudCustomData
String
''
Custom message data, which is saved in the cloud, will be sent to the receiver, and can still be pulled after the application is uninstalled and reinstalled.
The payload is as described below:
Name
Type
Description
index
Number
Emoji index, which is customized by the user
data
String
Extra data
Returned value
Message
Sample
// Send an emoji message on web
// 1. Create a message instance. The returned instance can be displayed on the screen.
let message = chat.createFaceMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
payload: {
index: 1, // Number. Emoji index, which is customized by the user
data: 'tt00' // String. Extra data
},
// cloudCustomData: 'your cloud custom data'
});
// 2. Send the message.
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// The message sent successfully
console.log(imResponse);
}).catch(function(imError) {
// Failed to send the message
console.warn('sendMessage error:', imError);
});

Creating a file message

This API is used to create a file message. It returns a message instance, which can be sent by calling the sendMessage API when you need to send a file message.
API
chat.createFileMessage(options);
Parameter
The options parameter is of the Object type. It contains the following attribute values:
Name
Type
Default
Description
to
String
-
userID or groupID of the message receiver
conversationType
String
-
Conversation type. Valid values:
TencentCloudChat.TYPES.CONV_C2C (client to client conversation)
TencentCloudChat.TYPES.CONV_GROUP (group conversation)
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
Message priority. If messages in a group exceed the frequency limit, the backend will deliver high-priority messages first. Supported enumerated values:
TencentCloudChat.TYPES.MSG_PRIORITY_HIGH
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL (default)
TencentCloudChat.TYPES.MSG_PRIORITY_LOW
TencentCloudChat.TYPES.MSG_PRIORITY_LOWEST
payload
Object
-
Message content container
onProgress
function
-
Callback function for getting the upload progress
cloudCustomData
String
''
Custom message data, which is saved in the cloud, will be sent to the receiver, and can still be pulled after the application is uninstalled and reinstalled.
The payload is as described below:
Name
Type
Description
file
HTMLInputElement | File | Object
It is used to select a DOM node or File object of the file on web, or the success callback parameter of the uni.chooseFile API. The SDK reads the data contained in this parameter and uploads the file.
Returned value
Sample
// Sample 1: Sending a file message on web - passing in a DOM node
// 1. Create a file message instance. The returned instance can be displayed on the screen.
let message = chat.createFileMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
payload: {
file: document.getElementById('filePicker'),
},
// cloudCustomData: 'your cloud custom data'
onProgress: function(event) { console.log('file uploading:', event) }
});

// 2. Send the message.
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// The message sent successfully
console.log(imResponse);
}).catch(function(imError) {
// Failed to send the message
console.warn('sendMessage error:', imError);
});
// Sample 2: Sending a file message on web - passing in a File object
// Add a message input box with the ID set to `testPasteInput`,
let clipboardData = e.clipboardData;
let file;
let fileCopy;
if (clipboardData && clipboardData.files && clipboardData.files.length > 0) {
file = clipboardData.files[0];
// After the image message is sent successfully
// the content pointed to by `file` may be cleared by the browser.
// If you have extra rendering needs, you can copy the data in advance.
fileCopy = file.slice();
}

if (typeof file === 'undefined') {
console.warn('The `file` is `undefined`. Check for the compatibility of the code or browser.');
return;
}

// 1. Create a message instance. The returned instance can be displayed on the screen.
let message = chat.createFileMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
payload: {
file: file
},
onProgress: function(event) { console.log('file uploading:', event) }
});

// 2. Send the message.
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// The message sent successfully
console.log(imResponse);
}).catch(function(imError) {
// Failed to send the message
console.warn('sendMessage error:', imError);
});
});

Creating a geographical location message

This API is used to create a geographical location message. It returns a message instance, which can be sent by calling the sendMessage API when you need to send a geographical location message.
API
chat.createLocationMessage(options);
Parameter
The options parameter is of the Object type. It contains the following attribute values:
Name
Type
Default
Description
to
String
-
userID or groupID of the message receiver
conversationType
String
-
Conversation type. Valid values:
TencentCloudChat.TYPES.CONV_C2C (one-to-one conversation)
TencentCloudChat.TYPES.CONV_GROUP (group conversation)
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
Message priority. If messages in a group exceed the frequency limit, the backend will deliver high-priority messages first. Supported enumerated values:
TencentCloudChat.TYPES.MSG_PRIORITY_HIGH
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL (default)
TencentCloudChat.TYPES.MSG_PRIORITY_LOW
TencentCloudChat.TYPES.MSG_PRIORITY_LOWEST
payload
Object
-
Message content container
cloudCustomData
String
''
Custom message data, which is saved in the cloud, will be sent to the receiver, and can still be pulled after the application is uninstalled and reinstalled.
The payload is as described below:
Name
Type
Description
description
String
Description of the geographical location
longitude
Number
Longitude
latitude
Number
Latitude
Returned value
Message
Sample
// Send a geographical location message on web
// 1. Create a message instance. The returned instance can be displayed on the screen.
let message = chat.createLocationMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
payload: {
description: 'Tencent Building, No. 10000, Shennan Boulevard, Shenzhen',
longitude: 113.941079, // Longitude
latitude: 22.546103 // Latitude
}
});
// 2. Send the message.
let promise = chat.sendMessage(message);
promise.then(function(imResponse) {
// The message sent successfully
console.log(imResponse);
}).catch(function(imError) {
// Failed to send the message
console.warn('sendMessage error:', imError);
});

Creating a merged message

This API is used to create a merged message. It returns a message instance, which can be sent by calling the sendMessage API when you need to send a merged message.
Note
1. This API does not support merging messages that failed to be sent. If the message list contains a message that failed to be sent, the API will report an error.
API
chat.createMergerMessage(options);
Parameter
The options parameter is of the Object type. It contains the following attribute values:
Name
Type
Default
Description
to
String
-
userID or groupID of the message receiver
conversationType
String
-
Conversation type. Valid values:
TencentCloudChat.TYPES.CONV_C2C (one-to-one conversation)
TencentCloudChat.TYPES.CONV_GROUP (group conversation)
priority
String
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL
Message priority. If messages in a group exceed the frequency limit, the backend will deliver high-priority messages first. Supported enumerated values:
TencentCloudChat.TYPES.MSG_PRIORITY_HIGH
TencentCloudChat.TYPES.MSG_PRIORITY_NORMAL (default)
TencentCloudChat.TYPES.MSG_PRIORITY_LOW
TencentCloudChat.TYPES.MSG_PRIORITY_LOWEST
payload
Object
-
Message content container
cloudCustomData
String
''
Custom message data, which is saved in the cloud, will be sent to the receiver, and can still be pulled after the application is uninstalled and reinstalled.
The payload is as described below:
Name
Type
Description
messageList
Array
Merged message list
title
String
Title of merged messages, for example, "Chat History of the Talent Center in the Greater Bay Area"
abstractList
String
Abstract list. You can set abstract information in different formats for different message types, for example, in the sender:text format for a text message, in the sender:[image] format for an image message, or in the sender:[file] format for a file message.
compatibleText
String
Compatibility text. If the SDK on an early version does not support the merged message, the user will receive a text message with the content ${compatibleText} by default. This field is required.
Returned value
Message
Sample
// 1. Forward group messages to a one-to-one conversation.
// `message1`, `message2`, and `message3` are group messages.
let mergerMessage = chat.createMergerMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
payload: {
messageList: [message1, message2, message3],
title: 'Chat History of the Talent Center in the Greater Bay Area',
abstractList: ['allen: 666', 'iris: [Image]', 'linda: [File]'],
compatibleText: 'Upgrade your Chat SDK to v2.10.1 or later to view this message.'
},
// cloudCustomData: 'your cloud custom data'
});

// 2. Send the message.
let promise = chat.sendMessage(mergerMessage);
promise.then(function(imResponse) {
// The message sent successfully
console.log(imResponse);
}).catch(function(imError) {
// Failed to send the message
console.warn('sendMessage error:', imError);
});

Downloading a Merged Message

This API is used to download a merged message. When the merged message sent by the sender is large in size, the SDK will store it in the cloud, and the message receiver needs to download it from the cloud before viewing it.
API
chat.downloadMergerMessage(message);
Parameter
Name
Type
Description
message
Message
Message instance
Returned value
Promise
Sample
// If `downloadKey` exists, the received merged message is stored in the cloud
// and needs to be downloaded first.
if (message.type === TencentCloudChat.TYPES.MSG_MERGER && message.payload.downloadKey !== '') {
let promise = chat.downloadMergerMessage(message);
promise.then(function(imResponse) {
// After the download is successful
// the SDK will update information such as `message.payload.messageList`.
console.log(imResponse.data);
}).catch(function(imError) {
// Download failed
console.warn('downloadMergerMessage error:', imError);
});
}

Forwarding Messages One by One

To forward a single message, create a message identical to the original message through the createForwardMessage API first, and then call the sendMessage API to send the message.
API
chat.createForwardMessage(message);
Parameter
Name
Type
Description
message
Message
Message instance
Sample
let forwardMessage = chat.createForwardMessage({
to: 'user1',
conversationType: TencentCloudChat.TYPES.CONV_C2C,
payload: message, // The received or sent message instance
});
// 2. Send the message.
let promise = chat.sendMessage(forwardMessage);
promise.then(function(imResponse) {
// The message sent successfully
console.log(imResponse);
}).catch(function(imError) {
// Failed to send the message
console.warn('sendMessage error:', imError);
});

Sending a message

This API is used to send a message. You need to call any of the following message instance creation APIs to create a message instance first before calling this API to send the message instance.
createTextMessage
createTextAtMessage
createImageMessage
createAudioMessage
createVideoMessage
createCustomMessage
createFaceMessage
createFileMessage
createLocationMessage
createMergerMessage
createForwardMessage
Note
1. When this API is called to send a message instance, the SDK must be in the ready status; otherwise, the message instance cannot be sent. The SDK status can be obtained by listening for the TencentCloudChat.EVENT.SDK_READY event, which will be triggered when the SDK is in the ready status.
2. The TencentCloudChat.EVENT.MESSAGE_RECEIVED event must be listened for in order to receive newly pushed one-to-one messages, group messages, group tips, or group system notifications.
3. Messages sent by this API will not trigger the TencentCloudChat.EVENT.MESSAGE_RECEIVED event. Messages sent by the same account from another client (or through the RESTful API) will trigger the TencentCloudChat.EVENT.MESSAGE_RECEIVED event.
4. Offline push applies only to Android or iOS devices but is not supported for web.
5. onlineUserOnly and messageControlInfo cannot be used together.
API
chat.sendMessage(message, options);
Parameter
Name
Type
Description
message
Message
Message instance
options
Object
Message sending option (message content container), which is optional
The options is as described below:
Name
Type
Description
onlineUserOnly
Boolean
It specifies whether the message is sent only to online users and defaults to false. If it is set to true, the message will not be stored on the roaming server, nor counted as an unread message, nor pushed to the receiver offline. It is suitable for sending unimportant tips such as broadcast notifications. This parameter is not supported when a message is sent in an audio-video group (AVChatRoom).
offlinePushInfo
Object
For more information, see Offline Push.
messageControlInfo
Object
It is a message control configuration item.
The offlinePushInfo is as described below:
Name
Type
Description
disablePush
Boolean
true: offline push is disabled; false (default): offline push is enabled.
title
String
Offline push title. This parameter is used by both iOS and Android.
description
String
Offline push content. This parameter will overwrite the offline push display text of the message instance. If the sent message is a custom message, this parameter will overwrite message.payload.description. If both description and message.payload.description are left empty, the receiver cannot receive the offline push notification of the custom message.
extension
String
Content passed through by offline push
ignoreIOSBadge
Boolean
Specifies whether the badge count is ignored (applicable to iOS only). If this parameter is set to true, the unread message count on the application badge will not increase when the message is received by the iOS device.
androidOPPOChannelID
String
Offline push channel ID for OPPO phones that run Android v8.0 or later
The messageControlInfo is as described below:
Name
Type
Description
excludedFromUnreadCount
Boolean
true: unreadCount of the conversation is not updated (the message is stored on the roaming server); false (default)
excludedFromLastMessage
Boolean
true: lastMessage of the conversation is not updated (the message is stored on the roaming server); false (default)
excludedFromContentModeration
Boolean
true: the message is excluded from content moderation. You need to purchase the Premium edition.
Returned Value
Promise
Sample
// If the receiver is offline, the message will be stored on the roaming server
// and pushed offline on the precondition that the receiver's application switches to the background
// or the process is killed.
// The default title and content of offline push are kept.
chat.sendMessage(message);
chat.sendMessage(message, {
// If the receiver is offline, the message will be neither stored on the roaming server
// nor pushed offline.
onlineUserOnly: true
});
chat.sendMessage(message, {
offlinePushInfo: {
// If the receiver is offline, the message will be stored on the roaming server
// but will not be pushed offline.
disablePush: true
}
});
chat.sendMessage(message, {
// If the receiver is offline, the message will be stored on the roaming server
// and pushed offline on the precondition that the receiver's application switches to the background
// or the process is killed.
// The title and content of offline push can be customized at the access side.
offlinePushInfo: {
title: '', // Offline push title
description: '', // Offline push content
// Offline push channel ID for OPPO phones that run Android v8.0 or later for offline push
androidOPPOChannelID: ''
}
});
chat.sendMessage(message, {
messageControlInfo: {
// `unreadCount` of the conversation is not updated (the message is stored on the roaming server).
excludedFromUnreadCount: true,
// `lastMessage` of the conversation is not updated (the message is stored on the roaming server).
excludedFromLastMessage: true
}
});
chat.sendMessage(message, {
messageControlInfo: {
excludedFromContentModeration: true,
}
});
Feature
Limit Item
Description
One-to-one/group message
Content length
A one-to-one or group message must be no longer than 12 KB.
Sending frequency
One-to-one message: No limit for sending one-to-one messages on the client; sending through the RESTful API is subject to the frequency limit. See the corresponding API documentation. Group message: Up to 40 messages can be sent per second per group (regardless of the group type and platform interface, and this limit applies to each group separately).
Receiving frequency
No limit for one-to-one messages or group messages.
Size of a single file
SDKs support a maximum file size of 100 MB for any single file to be sent.
Note:
1. When the number of sent messages exceeds the limit, the backend will first deliver high-priority messages, and messages with the same priority will be delivered randomly. However, if the number of high-priority messages sent in a second exceeds 40, high-priority messages will also be discarded.
2. A message that has been restricted by frequency control is not delivered or stored in the message history, but a success response will be returned to the sender. Before Group Message Is Sent callback is triggered, but After a Group Message Is Sent callback is not triggered.
3. The default frequency limit for calling the RESTful API to send group messages is 200 times per second, which is a different concept from the aforementioned "limit of 40 messages per second for each group." Please distinguish it.
Please refer to Use Limits for more limits.