tencent cloud

Feedback

Web, Mini Program, and uni-app

Last updated: 2023-07-17 17:08:38

    Feature Description

    Message extension allows you to configure keys and values for messages to implement polling, group notes, survey and other types of messages.
    For polling, create a custom message using the createCustomMessage API, where data stores the polling title and options. And store the user ID of the voter and selected option(s) in the key and value of the message extension, respectively. With the selected options of users, we can calculate the polling percentage in real time.
    For group notices, create a custom message for group notification using the createCustomMessage API, where data stores the title of the group notice, and then store the user ID and the corresponding info in the key and value of the message extension, respectively.
    For a survey, create a custom message using the createCustomMessage API, where data stores the title and options of the survey, and then store the user ID and the corresponding info in the key and value of the message extension, respectively.
    Note
    1. To use this feature, you need to purchase the Premium edition.
    2. You need to enable this feature via Chat console > Feature Configuration > Login and Message > Set message extension.
    3. This feature is not available for communities and audio/video groups.

    Setting message extension

    Call the setMessageExtensions API to set a message extension. If the extension key already exists, modify its value. Otherwise, add the new key-value extension. After the message extension is set successfully, both yourself and the recipient (one-to-one) or group members (Group) will receive the TencentCloudChat.EVENT.MESSAGE_EXTENSIONS_UPDATED event.
    API
    chat.setMessageExtensions(message, extensions);
    Parameters
    Name
    Type
    Description
    message
    Message
    Message instance
    extensions
    Array
    List of key-value extensions of a message. If an extension key already exists, modify its value. Otherwise, add the new key-value extension.
    Note
    1. The message must meet the following conditions:
    The isSupportExtension attribute of the message is set to true.
    The message is sent successfully.
    The message is not a message of a community group (Community) or audio-video group (AVChatRoom).
    2. The key and value of an extension can contain up to 100 B and 1 KB, respectively. You can set up to 20 extensions each time and 300 extensions for a message.
    3. If multiple users set or delete the key of the same extension simultaneously, only the first user can operate successfully, and other users will receive the error code 23001 and the latest extension info in the setting response packet, who can set it again if necessary.
    4. We recommend setting unique keys of extensions by different users to avoid conflicts in most cases. For example, the userID can be set as the key of the extension in polling, group notices and survey.
    Return values
    Promise
    Sample
    let promise = chat.setMessageExtensions(message, [{ key: 'a', value: '1' }, { key: 'b', value: '2' }]);
    promise.then(function(imResponse) {
    // Set message extensions successfully
    const { extensions } = imResponse.data;
    extensions.forEach((item) => {
    const { code, key, value } = item;
    if (code === 23001) {
    // `key` conflict. Try again according to the latest extension information returned as needed.
    }
    });
    }).catch(function(imError){
    // Failed to set message extensions
    console.warn('setMessageExtensions error:', imError);
    });

    Getting message extensions

    Call the getMessageExtensions API to get the list of message extensions.
    API
    chat.getMessageExtensions(message);
    Parameters
    Name
    Type
    Description
    message
    Message
    Message instance
    Note
    The message must meet the following conditions:
    1. The isSupportExtension attribute of the message is set to true.
    2. The message is sent successfully.
    3. The message is not a message of a community group (Community) or audio-video group (AVChatRoom).
    Return values
    Promise
    Sample
    let promise = chat.getMessageExtensions(message);
    promise.then(function(imResponse) {
    // Got message extensions successfully
    const { extensions } = imResponse.data;
    extensions.forEach((item) => {
    const { key, value } = item;
    // `key` - key in the message extension
    // `value` - value of the key in the message extension
    });
    }).catch(function(imError){
    // Failed to get message extensions
    console.warn('getMessageExtensions error:', imError);
    });

    Deleting message extensions

    Call the deleteMessageExtensions API to delete message extensions. If keyList is not passed in, all message extensions will be cleared. After message extensions are successfully deleted, both the user and the recipient (one-to-one) or group members (Group) will receive the TencentCloudChat.EVENT.MESSAGE_EXTENSIONS_DELETED event.
    API
    chat.deleteMessageExtensions(message, keyList);
    Parameters
    Name
    Type
    Description
    message
    Message
    Message instance
    keyList
    Array | undefined
    List of message extension keys
    Note
    1. The message must meet the following conditions:
    The isSupportExtension attribute of the message is set to true.
    The message is sent successfully.
    The message is not a message of a community group (Community) or audio-video group (AVChatRoom).
    2. If multiple users set or delete the key of the same extension simultaneously, only the first user can operate successfully, and other users will receive the error code 23001 and the latest extension info, who can initiate the operation again if necessary.
    Return values
    Promise
    Sample
    // Delete message extension keys
    let promise = chat.deleteMessageExtensions(message, ['a', 'b']);
    promise.then(function(imResponse) {
    // Deleted message extensions successfully
    const { extensions } = imResponse.data;
    extensions.forEach((item) => {
    const { code, key, value } = item;
    if (code === 23001) {
    // `key` conflict. Try again according to the latest extension information returned as needed.
    }
    });
    }).catch(function(imError){
    // Failed to delete message extensions
    console.warn('deleteMessageExtensions error:', imError);
    });
    // Clear all message extension keys
    let promise = chat.deleteMessageExtensions(message);
    promise.then(function(imResponse) {
    // Message extensions cleared successfully
    console.log('deleteMessageExtensions ok:', imResponse)
    }).catch(function(imError){
    // Failed to clear the message extensions
    console.warn('deleteMessageExtensions error:', imError);
    });

    Listening to MESSAGE_EXTENSIONS_UPDATED event

    If you have registered the TencentCloudChat.EVENT.MESSAGE_EXTENSIONS_UPDATED event in advance, and message extensions are added or updated, the SDK distributes the TencentCloudChat.EVENT.MESSAGE_EXTENSIONS_UPDATED event, and you can obtain the updated key-value information in the registered callback.
    Sample
    let onMessageExtensionsUpdated = function(event) {
    const { messageID, extensions } = event.data;
    // `messageID` - Message ID
    // `extensions` - Message extension list
    extensions.forEach((item) => {
    const { key, value } = item;
    // `key` - key in the message extension
    // `value` - value of the key in the message extension
    });
    };
    chat.on(TencentCloudChat.EVENT.MESSAGE_EXTENSIONS_UPDATED, onMessageExtensionsUpdated);

    Listening to MESSAGE_EXTENSIONS_DELETED event

    If you have registered the TencentCloudChat.EVENT.MESSAGE_EXTENSIONS_DELETED event in advance, and message extensions are deleted, the SDK distributes the TencentCloudChat.EVENT.MESSAGE_EXTENSIONS_DELETED event, and you can obtain the deleted key in the registered callback.
    Sample
    let onMessageExtensionsDeleted = function(event) {
    const { messageID, keyList } = event.data;
    // `messageID` - Message ID
    // `keyList` - List of message extension keys deleted
    keyList.forEach((key) => {
    // console.log(key)
    });
    };
    chat.on(TencentCloudChat.EVENT.MESSAGE_EXTENSIONS_DELETED, onMessageExtensionsDeleted);
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support