Manage group chats
Group chats can have metadata like names, descriptions, and images to help users identify them. You can set metadata when creating a group chat or update it later.
Available metadata fields
group_name: The name of the group chatdescription: A description of the group chatimage_url: A URL pointing to an image for the group chat
Get and update metadata
Node
// Get metadata
const groupName = group.name;
const groupDescription = group.description;
const groupImageUrl = group.imageUrl;
// Update metadata
await group.updateName('New Group Name');
await group.updateDescription('New Group Description');
await group.updateImageUrl('newurl.com');Manage group chat membership
The maximum group chat size is 250 members.
Add members
You can add members directly by their Ethereum addresses or by their inbox IDs
Node
// Add members by Ethereum address
await ctx.addMembersWithAddresses(group, [ address1, address2 ]);
// Add members using inbox IDs
await group.addMembers([inboxId]);Remove members
Remove members from the group by their Ethereum addresses or by their inbox IDs:
Node
// Remove members by address
//needs to send the group to the context
await ctx.removeMembersWithAddresses(group, [ address1, address2 ]);
// Remove members by inbox ID
await group.removeMembers([inboxId]);Get member information
Retrieve and work with member data:
Node
// Sync group data to get latest member information
await group.sync();
// Get all members
const members = await group.members();Manage group chat admins
Node
// Check admin status
const isAdmin = group.isAdmin(inboxId);
const isSuperAdmin = group.isSuperAdmin(inboxId);
// List admins
const admins = group.admins;
const superAdmins = group.superAdmins;
// Add/remove admin status
await group.addAdmin(inboxId);
await group.addSuperAdmin(inboxId);
await group.removeAdmin(inboxId);
await group.removeSuperAdmin(inboxId);Example usage
Get detailed information about group members:
agent.on('text', async (ctx) => {
const members = await group.members();
for (const member of members) {
console.log(`Member inbox ID: ${member.inboxId}`);
console.log(`Permission level: ${member.permissionLevel}`);
console.log(`Consent state: ${member.consentState}`);
// Get Ethereum address
const ethIdentifier = member.accountIdentifiers.find(
(id) => id.identifierKind === IdentifierKind.Ethereum
);
if (ethIdentifier) {
console.log(`Ethereum address: ${ethIdentifier.identifier}`);
}
}
});
