• UIKit
  • SDK
  • 서버 API
Chat/
SDK/
Web/
사용자 정보 및 관계망/
SDK
  • Chat SDK 설치
  • Chat SDK 초기화
  • 로그인 및 로그아웃
  • 고객 인터페이스
  • Changelog
  • 메시지
    • 개요
    • 메시지 보내기
    • 메시지 받기
    • 과거 메시지
    • 메시지 전달
    • 메시지 수정
    • 메시지 삭제
    • 과거 메시지 비우기
    • 메시지 철회
    • 온라인 메시지 보내기
    • 메시지 읽음 확인
    • 메시지 조회
    • 지향적 그룹 메시지
    • 알림 차단
    • Key-Value 확장
    • 번역
  • 대화
    • 개요
    • 대화 목록
    • 대화 획득
    • 읽지 않은 메시지 수
    • 대화 최상단 고정
    • 대화 삭제
    • 마크
    • 대화 그룹
  • 그룹
    • 개요
    • 그룹 관리
    • 그룹 정보
    • 그룹 멤버 관리
    • 그룹 멤버 정보
    • 그룹 속성 사용자 정의
    • 그룹 카운터
  • 커뮤니티 화제
    • 커뮤니티 관리
  • 사용자 정보 및 관계망
    • 사용자 정보
    • 사용자 상태
    • 친구 관리
    • 친구 목록
    • 블록리스트
  • Guideline for Beginners
  • 콘솔 안내
    • 애플리케이션 생성 및 업그레이드
    • 기본 구성
    • 기능 구성
    • 계정 관리
    • 그룹 관리
    • 콜백 구성
  • 제품 소개
    • 메시지 관리
      • 1대1 메시지
      • 메시지 저장
      • 오프라인 푸시
      • 그룹 메시지
      • 메시지 포맷
    • 계정 시스템
      • 로그인 인증
      • 온라인 상태 관리
    • 그룹 관련
      • 그룹 시스템
      • 그룹 관리
    • 사용자 정보 및 관계망
      • 정보 관리
      • 관계망 관리
  • 구매 가이드
    • 과금 개요
    • 가격
  • 에러코드
이 페이지는 현재 영어로만 제공되며 한국어 버전은 곧 제공될 예정입니다. 기다려 주셔서 감사드립니다.

친구 관리

Feature Description

The IM 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.