1

Create room

by
Published Aug 10, 2022

Creates a Matrix room.

Script matrix Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 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
  is_direct = false,
10
  name = "",
11
  room_alias_name = "",
12
  room_version = "",
13
  topic = "",
14
  visibility: "public" | "private" = "private",
15
) {
16
  if (!matrix_res.token) {
17
    throw Error("Creating a room requires an access token.");
18
  }
19
  const resp = await fetch(
20
    `${matrix_res.baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(
21
      room_id,
22
    )}/invite`,
23
    {
24
      method: "POST",
25
      headers: {
26
        Authorization: `Bearer ${matrix_res.token}`,
27
        "Content-Type": "application/json",
28
      },
29
      body: JSON.stringify({
30
        is_direct,
31
        ...(name && { name }),
32
        ...(room_alias_name && { room_alias_name }),
33
        ...(room_version && { room_version }),
34
        ...(topic && { topic }),
35
        visibility,
36
      }),
37
    },
38
  );
39
  if (!resp.ok) {
40
    throw Error(`Failed to create room: Error HTTP${resp.status}`);
41
  }
42
}
43

Other submissions
  • Submitted by jaller94 Deno
    Created 398 days ago
    1
    type Matrix = {
    2
      baseUrl: string;
    3
      token: string;
    4
    };
    5
    export async function main(
    6
      matrix_res: Matrix,
    7
      is_direct = false,
    8
      name = "",
    9
      room_alias_name = "",
    10
      room_version = "",
    11
      topic = "",
    12
      visibility: "public" | "private" = "private",
    13
    ) {
    14
      if (!matrix_res.token) {
    15
        throw Error("Creating a room requires an access token.");
    16
      }
    17
      const resp = await fetch(
    18
        `${matrix_res.baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(
    19
          room_id,
    20
        )}/invite`,
    21
        {
    22
          method: "POST",
    23
          headers: {
    24
            Authorization: `Bearer ${matrix_res.token}`,
    25
            "Content-Type": "application/json",
    26
          },
    27
          body: JSON.stringify({
    28
            is_direct,
    29
            ...(name && { name }),
    30
            ...(room_alias_name && { room_alias_name }),
    31
            ...(room_version && { room_version }),
    32
            ...(topic && { topic }),
    33
            visibility,
    34
          }),
    35
        },
    36
      );
    37
      if (!resp.ok) {
    38
        throw Error(`Failed to create room: Error HTTP${resp.status}`);
    39
      }
    40
    }
    41