Invite users to channels
Requires App Context
Enable App Context for your keyset in the Admin Portal.
Invite users to private or group conversations, creating their channel membership. Invitations trigger an invite event that you can listen to and notify invited users.
To send notifications on invitations, implement custom logic to:
- Create and send custom events when invitations are sent
- Let invited users receive these events
Invite one user
Request a user to join a channel with invite().
Method signature
This method takes the following parameters:
1channel.invite(user: User): Promise<Membership>
Input
| Parameter | Description |
|---|---|
user *Type: UserDefault: n/a | User that you want to invite. |
Output
| Type | Description |
|---|---|
Promise<Membership> | Returned (modified) object containing the membership data. |
Sample code
Invite support-agent-15 to join the high-prio-incidents channel.
1// reference "support-agent-15"
2const user = await chat.getUser("support_agent_15")
3
4// reference the "high-prio-incidents" channel
5const channel = await chat.getChannel("high-prio-incidents")
6
7// invite the agent to join the channel
8await channel.invite(user)
Invite multiple users
Request multiple users to join a channel with inviteMultiple(). Maximum 100 users per call.
Method signature
This method takes the following parameters:
1channel.inviteMultiple(users: User[]): Promise<Membership[]>
Input
| Parameter | Description |
|---|---|
users *Type: User[]Default: n/a | List of users you want to invite to the group channel. You can invite up to 100 users in one call. |
Output
| Type | Description |
|---|---|
Promise<Membership[]> | Returned (modified) list of objects containing the membership data. |
Sample code
Invite support-agent-15 and support-agent-16 to join the high-prio-incidents channel.
1// reference both agents
2const user1 = await chat.getUser("support_agent_15")
3const user2 = await chat.getUser("support_agent_16")
4
5// reference the "high-prio-incidents" channel
6const channel = await chat.getChannel("high-prio-incidents")
7
8// invite both agents to join the channel
9await channel.inviteMultiple([
10 user1,
11 user2
12])
Listen to invite events
Monitor invitation events with listenForEvents() and send notifications to invited users.
Events documentation
See Chat events for details on invite events.
Method signature
This method has the following parameters:
1chat.listenForEvents({
2 channel: string;
3 type?: "invite";
4 callback: (event: Event<"invite">) => unknown;
5}): () => void
Input
| Parameter | Description |
|---|---|
channel *Type: stringDefault: n/a | Channel to listen for new invite events. |
typeType: stringDefault: n/a | Type of events. invite is the type defined for all events emitted when an offensive message is flagged/reported. |
callback *Type: n/a Default: n/a | Callback function passed as a parameter. It defines the custom behavior to be executed whenever an invite event type is detected on the specified channel. |
Output
| Type | Description |
|---|---|
() => void | Function you can call to disconnect (unsubscribe) from the channel and stop receiving invite events. |
Sample code
Print a notification for an invitation received on the support channel.
1const chat = {
2 listenForEvents: async (config) => {
3 const simulateEvent = () => {
4 const event = "invite";
5 const eventData = {
6 channel: "support",
7 user: "Bob",
8 message: "Join our chat room!",
9 };
10 if (event === config.type) {
11 config.callback(eventData);
12 }
13 };
14
15 // simulate a single event when listening starts
show all 28 lines