tencent cloud

Feedback

JavaScript

Last updated: 2024-10-30 11:43:57

    Feature Description

    If a message sender wants to know who has or has not read the message, the sender needs to enable the message read receipt feature. After this feature is enabled, the sender can set whether a message requires a read receipt when sending the message; if yes, the sender will receive a receipt after the message is read by the receiver.
    

    Message Read Receipt

    Specifying a group type for which to support message read receipts

    Specifying that a message requires a read receipt (by the sender)

    The sender creates a message, sets needReadReceipt to true, and sends the message.
    Sample
    // Create a one-to-one message
    let message = chat.createTextMessage({
    to: 'user1',
    conversationType: TencentCloudChat.TYPES.CONV_C2C,
    payload: {
    text: 'Hello world!'
    },
    // To use it, purchase the Premium edition and set `needReadReceipt` to `true` when creating a message.
    needReadReceipt: true
    });
    // 2. Send the message.
    let promise = chat.sendMessage(message);
    promise.then(function(imResponse) {
    // Message sent successfully
    console.log(imResponse);
    }).catch(function(imError) {
    // Failed to send the message
    console.warn('sendMessage error:', imError);
    });
    // Create a group message
    let message = chat.createTextMessage({
    to: 'test',
    conversationType: TencentCloudChat.TYPES.CONV_GROUP,
    payload: {
    text: 'Hello world!'
    },
    // To use it, purchase the Premium edition and set `needReadReceipt` to `true` when creating a message.
    needReadReceipt: true
    });
    // Send the message
    let promise = chat.sendMessage(message);
    promise.then(function(imResponse) {
    // Message sent successfully
    console.log(imResponse);
    }).catch(function(imError) {
    // Failed to send the message
    console.warn('sendMessage error:', imError);
    });

    Sending a message read receipt (by the receiver)

    After receiving the message, the receiver determines whether the message requires a read receipt based on the needReadReceipt field in Message. If yes, after the user reads the message, the receiver calls the sendMessageReadReceipt API to send a read receipt.
    Note
    Messages in the messageList must be from the same one-to-one or group conversation.
    After this API is called successfully, the unread count of the conversation will not change, and the sender will receive the TencentCloudChat.TYPES.MESSAGE_READ_RECEIPT_RECEIVED callback which contains the latest read information of the message.
    API
    chat.sendMessageReadReceipt(messageList);
    Parameter
    Name
    Type
    Description
    messageList
    Array
    List of messages (up to 30) in the same conversation
    Returned value
    Promise
    Sample
    // Pull the group message list
    let messageList = null;
    chat.getMessageList({conversationID: 'GROUPtest'}).then(function(imResponse) {
    messageList = imResponse.data.messageList; // Message list
    chat.sendMessageReadReceipt(messageList).then(function() {
    // Read receipt for the group message sent successfully
    }).catch(function(imError) {
    // Failed to send a read receipt for the group message
    });
    });
    // Pull the one-to-one message list
    let messageList = null;
    chat.getMessageList({conversationID: 'C2Ctest'}).then(function(imResponse) {
    messageList = imResponse.data.messageList; // Message list
    chat.sendMessageReadReceipt(messageList).then(function() {
    // Read receipt for the one-to-one message sent successfully
    }).catch(function(imError) {
    // Failed to send a read receipt for the one-to-one message
    });
    });

    Listening for a message read receipt notification (by the sender)

    After the receiver sends a message read receipt, the sender can listen for a receipt notification and update the UI based on the notification to display the message as, for example, "Read by two members".
    Sample
    let onMessageReadReceiptReceived = function(event) {
    // event.data - An array that stores message read receipt information
    const readReceiptInfoList = event.data;
    readReceiptInfoList.forEach((item) => {
    const { groupID, userID, messageID, readCount, unreadCount, isPeerRead } = item;
    // messageID - Message ID
    // userID - one-to-one message receiver
    // isPeerRead - Whether the one-to-one message is read by the receiver
    // groupID - Group ID
    // readCount - Number of members who have read the group message
    // unreadCount - Number of members who have not read the group message
    const message = chat.findMessage(messageID);
    if (message) {
    if (message.conversationType === TencentCloudChat.TYPES.CONV_C2C) {
    if (message.isPeerRead === true) {
    // Read by the receiver
    }
    } else if (message.conversationType === TencentCloudChat.TYPES.CONV_GROUP) {
    if (message.readReceiptInfo.unreadCount === 0) {
    // Read by all
    } else {
    // message.readReceiptInfo.readCount - Latest read count of the message
    // To query which group members have read the message
    // call the [getGroupMessageReadMemberList] API.
    }
    }
    }
    });
    }
    chat.on(TencentCloudChat.EVENT.MESSAGE_READ_RECEIPT_RECEIVED, onMessageReadReceiptReceived);

    Pulling message read receipt information (by the sender)

    After entering the message list, the sender pulls historical messages first, and then calls the getMessageReadReceipt API to pull the message read receipt information.
    Note
    1. Messages in the messageList must be from the same one-to-one or group conversation.
    API
    chat.getMessageReadReceiptList(messageList);
    Parameter
    Name
    Type
    Description
    messageList
    Array
    List of messages in the same conversation
    Returned value
    Promise
    Sample
    // Pull the group message list
    let messageList = null;
    chat.getMessageList({conversationID: 'GROUPtest'}).then(function(imResponse) {
    messageList = imResponse.data.messageList; // Message list
    chat.getMessageReadReceiptList(messageList).then(function(imResponse) {
    messageList = imResponse.data.messageList; // Message list
    // `getMessageReadReceiptList` is called successfully,
    // `Message.readReceiptInfo` will contain the message read receipt information.
    // Message.readReceiptInfo.readCount - Read count of a message.
    // To query which group members have read the message, call the [getGroupMessageReadMemberList] API.
    // Message.readReceiptInfo.unreadCount - Unread count of a message.
    // `0` indicates that all members have read the message.
    }).catch(function(imError) {
    // Failed to pull the read receipt list
    });
    });
    // Pull the one-to-one message list
    let messageList = null;
    chat.getMessageList({conversationID: 'C2Ctest'}).then(function(imResponse) {
    messageList = imResponse.data.messageList; // Message list
    chat.getMessageReadReceiptList(messageList).then(function(imResponse) {
    messageList = imResponse.data.messageList; // Message list
    // After the message list is pulled successfully
    // `Message.readReceiptInfo` will contain the message read receipt information.
    // Message.readReceiptInfo.isPeerRead - Whether the receiver has sent a read receipt
    }).catch(function(imError) {
    // Failed to pull the read receipt list
    });
    });

    Pulling the list of members who have or have not read a group message (by the sender)

    To view the list of members who have or have not read a group message, the sender can call the getGroupMessageReadMemberList API to pull the member list by page.
    API
    chat.getGroupMessageReadMemberList(options);
    Parameter
    The options parameter is of the Object type. It contains the following attribute values:
    Name
    Type
    Description
    message
    Message
    Message instance
    cursor
    String
    Cursor for the paged pull. Pass in '' for the first pull.
    filter
    Number
    Specifies to pull the list of members who have or have not read the message. Valid values:
    0 - pull the list of members who have read the message
    1 - pull the list of members who have not read the message
    count
    Number
    Number of members to be pulled per page. Maximum value: 100.
    Returned value
    Promise
    Sample
    // Pull the list of members who have read the group message
    let promise = chat.getGroupMessageReadMemberList({
    message,
    filter: 0,
    cursor: '', // Pass in `''` for the first pull
    count: 30,
    });
    promise.then(function(imResponse) {
    const { isCompleted, cursor, messageID, readUserIDList } = imResponse.data;
    // isCompleted - true: completed; false: not completed
    // cursor - Used for the subsequent pull when `isCompleted` is `false`
    // messageID - Group message ID
    // readUserIDList - List of `userID` values of members who have read the message
    }).catch(function(imError) {
    // Failed to pull the list of members who have read the group message
    });
    // Pull the list of members who have not read the group message
    let promise = chat.getGroupMessageReadMemberList({
    message,
    filter: 1,
    cursor: '', // Pass in `''` for the first pull
    count: 30,
    });
    promise.then(function(imResponse) {
    const { isCompleted, cursor, messageID, readUserIDList } = imResponse.data;
    // isCompleted - true: completed; false: not completed
    // cursor - Used for the subsequent pull when `isCompleted` is `false`
    // messageID - Group message ID
    // unreadUserIDList - List of `userID` values of members who have not read the group message
    }).catch(function(imError) {
    // Failed to pull the list of members who have not read the group message
    // 10062 - The read receipt information for the group message was not found.
    });
    
    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