1
Invite user to a room
One script reply has been approved by the moderators Verified

Invites a user to a Matrix room.

Created by jaller94 618 days ago Viewed 4366 times
0
Submitted by jaller94 Deno
Verified 617 days ago
1
type Matrix = {
2
  baseUrl: string;
3
  token: string;
4
};
5
export async function main(
6
  matrix_res: Matrix,
7
  room_id: string,
8
  user_id: string,
9
  reason = "",
10
) {
11
  if (!matrix_res.token) {
12
    throw Error("Inviting a user requires an access token.");
13
  }
14
  const resp = await fetch(
15
    `${matrix_res.baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(
16
      room_id,
17
    )}/invite`,
18
    {
19
      method: "POST",
20
      headers: {
21
        Authorization: `Bearer ${matrix_res.token}`,
22
        "Content-Type": "application/json",
23
      },
24
      body: JSON.stringify({
25
        user_id,
26
        ...(reason && { reason }),
27
      }),
28
    },
29
  );
30
  if (!resp.ok) {
31
    throw Error(`Failed to invite: Error HTTP${resp.status}`);
32
  }
33
}
34