Manage chat group member attributes

Updated

Introduces how to use the Agora Chat SDK to manage the attributes of the members of a chat group in your app.

Chat groups enable real-time messaging among multiple users.

This page shows how to use the Agora Chat SDK to manage the attributes of the members of a chat group in your app.

Understand the tech

The Chat SDK provides the Group, IGroupManager, and IGroupManagerDelegate classes for chat group management, which allows you to implement the following features:

  • Set custom attributes of a group member via key and value items.
  • Fetch group member custom attributes.
  • Listen for attribute changes of a group member.

Prerequisites

Before proceeding, ensure that you meet the following requirements:

  • You have initialized the Chat SDK. For details, SDK quickstart.
  • You understand the call frequency limits of the Chat APIs supported by different pricing plans as described in Limitations.
  • You understand the number of chat groups and chat group members supported by different pricing plans as described in Pricing Plan Details.

Implementation

This section describes how to call the APIs provided by the Chat SDK to implement chat group features.

Set custom attributes of a group member via key and value items

Each chat group member can set their own attributes. Chat group admins/owners can also modify all members' attributes. Each custom attribute should be in key-value format.

Refer to the following sample code to set a custom attribute of a group member:

Dictionary dict = new Dictionary();
dict.Add("key", "value");

SDKClient.Instance.GroupManager.SetMemberAttributes(groupId, userId, dict, new CallBack(
    onSuccess: () =>
    {
        Console.WriteLine($"SetMemberAttributes success.");
    },
    onError: (code, desc) =>
    {
        Console.WriteLine($"SetMemberAttributes failed, code:{code}, desc:{desc}");
    }
));

Fetch group member custom attributes

Chat group members and group admins/owners can retrieve custom attributes of multiple group members by attribute key.

Refer to the following sample code to use the attribute key to fetch custom attributes of multiple group members:

List userList = new List();
userList.Add("user");

// keyList: The array of keys for custom attributes of group members. If you pass in no value or an empty array, this method retrieves all custom attributes of these group members.
List keyList = new List();
keyList.Add("key");

SDKClient.Instance.GroupManager.FetchMemberAttributes(groupId, userList, keyList, new ValueCallBack>>(
    onSuccess: (dict) =>
    {

    },
    onError: (code, desc) =>
    {

    }
));

Listen for attribute changes of a group member

IGroupManagerDelegate class holds callbacks that can be used to monitor the change of any key-value items. When such a change occurs, an OnUpdateMemberAttributesFromGroup callback will notify the Client SDK by returning chat group ID, UID, and key-value pairs of the changes.

// Inherit and implement `IGroupManagerDelegate`.
public class GroupManagerDelegate : IGroupManagerDelegate {
    public void OnUpdateMemberAttributesFromGroup(string groupId, string userId, Dictionary attributes, string from)
    {

    }
}

// Add a delegate.
GroupManagerDelegate adelegate = new GroupManagerDelegate();
SDKClient.Instance.GroupManager.AddGroupManagerDelegate(adelegate);

// Remove the delegate when it is unnecessary.
SDKClient.Instance.GroupManager.RemoveGroupManagerDelegate(adelegate);