Invite user to a room

Invites a user to a Matrix room.

Script matrix Verified

by jaller94 ยท 8/10/2022

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 3 days ago
1
//native
2

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

Other submissions
  • Submitted by jaller94 Deno
    Created 395 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