tencent cloud

Feedback

JavaScript

Last updated: 2024-10-30 12:01:43

    Feature Description

    The Chat SDK supports friend management, and users can add and delete friends and set to send messages only to friends.

    Getting contacts

    Contacts cached in the SDK can be obtained. When the contacts are updated, the SDK will deliver the TencentCloudChat.EVENT.FRIEND_LIST_UPDATEDevent.
    API
    chat.getFriendList();
    Returned value
    Promise
    Sample
    let promise = chat.getFriendList();
    promise.then(function(imResponse) {
    const friendList = imResponse.data; // Contacts
    }).catch(function(imError) {
    console.warn('getFriendList error:', imError); // Failed to obtain contacts
    });

    Adding a friend

    API
    chat.addFriend(options);
    Parameter
    The options parameter is of the Object type. It contains the following attribute values:
    Name
    Type
    Description
    to
    String
    User ID
    source
    String
    Friend source. It consists of two parts: the prefix and the keyword. The former is AddSource_Type_, and the latter must be a string of up to 8 bytes. We recommend you use an English word or its abbreviation as the keyword. For example, if the keyword is Android, this field will be AddSource_Type_Android.
    wording
    String | undefined
    Friending remarks, which can contain up to 256 bytes.
    type
    String | undefined
    Friending method (two-way friending by default). Valid values:
    TencentCloudChat.TYPES.SNS_ADD_TYPE_SINGLE (one-way friending, where user B is in the friend list of user A but not vice versa)
    TencentCloudChat.TYPES.SNS_ADD_TYPE_BOTH (two-way friending, where user B is in the friend list of user A and vice versa)
    remark
    String | undefined
    Friend remarks, which can contain up to 96 bytes.
    groupName
    String | undefined
    List name, which can contain up to 30 bytes.
    Returned value
    Promise
    Sample
    // Add a friend
    let promise = chat.addFriend({
    to: 'user1',
    source: 'AddSource_Type_Web',
    remark: 'Jane',
    groupName: 'Friend',
    wording: 'I'm user0',
    type: TencentCloudChat.TYPES.SNS_ADD_TYPE_BOTH,
    });
    promise.then(function(imResponse) {
    // Sent the friend request successfully
    const { code } = imResponse.data;
    if (code === 30539) {
    // 30539 indicates that user1 has set the friend request processing method
    // to manually accept friend requests received.
    // The SDK will trigger the TencentCloudChat.EVENT.FRIEND_APPLICATION_LIST_UPDATED event.
    } else if (code === 0) {
    // 0 indicates that user1 has set the friend request processing method
    // to automatically accept all friend requests received.
    // The SDK will trigger the TencentCloudChat.EVENT.FRIEND_LIST_UPDATED event.
    }
    }).catch(function(imError) {
    console.warn('addFriend error:', imError); // Failed to add the friend
    });

    Deleting a friend

    Delete friends. Both one-way deletion and two-way deletion are supported.
    Caution
    We recommend you include up to 100 userID values in the userIDList at a time, as a large data packet may be rejected by the backend, which requires that a data packet not exceed 1 MB.
    API
    chat.deleteFriend(options);
    Parameter
    The options parameter is of the Object type. It contains the following attribute values:
    Name
    Type
    Description
    userIDList
    Array
    List of userID values of the friends to be deleted. The number of userID values cannot exceed 100 per request.
    type
    String | undefined
    Deletion mode (two-way deletion by default). Valid values:
    TencentCloudChat.TYPES.SNS_DELETE_TYPE_SINGLE (one-way deletion, where user B is deleted from the friend list of user A but not vice versa)
    TencentCloudChat.TYPES.SNS_DELETE_TYPE_BOTH (two-way deletion, where user B is deleted from the friend list of user A and vice versa)
    Returned value
    Promise
    Sample
    let promise = chat.deleteFriend({
    userIDList: ['user1','user2'],
    type: TencentCloudChat.TYPES.SNS_DELETE_TYPE_BOTH
    });
    promise.then(function(imResponse) {
    const { successUserIDList, failureUserIDList } = imResponse.data;
    // List of successfully deleted `userID` values
    successUserIDList.forEach((item) => {
    const { userID } = item;
    });
    // List of `userID` values failed to be deleted
    failureUserIDList.forEach((item) => {
    const { userID, code, message } = item;
    });
    // If the contacts are updated,
    // the SDK will trigger the TencentCloudChat.EVENT.FRIEND_LIST_UPDATED event.
    }).catch(function(imError) {
    console.warn('removeFromFriendList error:', imError);
    });

    Checking the friend relationship

    API
    chat.checkFriend(options);
    Parameter
    The options parameter is of the Object type. It contains the following attribute values:
    Name
    Type
    Description
    userIDList
    Array
    List of the userID values to be verified. The number of userID values cannot exceed 1,000 per request.
    type
    String | undefined
    Verification mode (two-way verification by default). Valid values:
    TencentCloudChat.TYPES.SNS_CHECK_TYPE_SINGLE (one-way verification, where the friend list of user A is checked for user B but not vice versa)
    TencentCloudChat.TYPES.SNS_CHECK_TYPE_BOTH (two-way verification, where the friend list of user A is checked for user B and vice versa)
    Returned value
    Promise
    Sample
    let promise = chat.checkFriend({
    userIDList: ['user0','user1'],
    type: TencentCloudChat.TYPES.SNS_CHECK_TYPE_BOTH,
    });
    promise.then(function(imResponse) {
    const { successUserIDList, failureUserIDList } = imResponse.data;
    // List of the `userID` values that passed the verification
    successUserIDList.forEach((item) => {
    const { userID, code, relation } = item; // The value of code is always 0.
    // Possible results of one-way friend verification are:
    // - relation: TencentCloudChat.TYPES.SNS_TYPE_NO_RELATION
    // B is not on A's friend list, but it cannot determine whether A is on B's friend list.
    // - relation: TencentCloudChat.TYPES.SNS_TYPE_A_WITH_B:
    // B is on A's friend list, but it cannot determine whether A is on B's friend list.
    // Possible results of two-way friend verification are:
    // - relation: TencentCloudChat.TYPES.SNS_TYPE_NO_RELATION:
    // A and B are not on each other's friend list
    // - relation: TencentCloudChat.TYPES.SNS_TYPE_A_WITH_B:
    // B is on A's friend list, but A is not on B's friend list
    // - relation: TencentCloudChat.TYPES.SNS_TYPE_B_WITH_A:
    // B is not on A's friend list, but A is on B's friend list
    // - relation: TencentCloudChat.TYPES.SNS_TYPE_BOTH_WAY:
    // A and B are on each other's friend list
    });
    // List of the `userID` values that failed the verification
    failureUserIDList.forEach((item) => {
    const { userID, code, message } = item;
    });
    }).catch(function(imError) {
    console.warn('checkFriend error:', imError);
    });

    Setting to send messages to friends only

    By default, Chat SDK does not check the relationship when sending one-to-one messages. This default setting is generally applied in customer service scenarios, where having to friend a customer service agent before chatting is inefficient. To implement "adding a friend before sending a message", you can log in to the Chat Console, select Feature Configuration > Login and Message > Relationship Check, and enable Check Relationship for One-to-One Messages. After it is enabled, a user can send messages only to friends. If the user sends a message to a non-friend user, the SDK will report error code 20009.
    
    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