1

Set room name

by
Published Jan 20, 2023

HTTP endpoint: https://spec.matrix.org/v1.5/client-server-api/#put_matrixclientv3roomsroomidstateeventtypestatekey State event description: https://spec.matrix.org/v1.5/client-server-api/#mroomname

Script matrix Verified

The script

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

3
/**
4
 * Set the name of a Matrix room.
5
 *
6
 * HTTP endpoint: https://spec.matrix.org/v1.5/client-server-api/#put_matrixclientv3roomsroomidstateeventtypestatekey
7
 *
8
 * State event description: https://spec.matrix.org/v1.5/client-server-api/#mroomname
9
 */
10
type Matrix = {
11
  baseUrl: string;
12
  token: string;
13
};
14
export async function main(
15
  matrix_res: Matrix,
16
  room_id: string,
17
  name: string = "",
18
) {
19
  const url = `${
20
    matrix_res.baseUrl
21
  }/_matrix/client/v3/rooms/${encodeURIComponent(room_id)}/state/m.room.name/`;
22
  const resp = await fetch(url, {
23
    method: "PUT",
24
    headers: {
25
      Authorization: `Bearer ${matrix_res.token}`,
26
      "Content-Type": "application/json",
27
    },
28
    body: JSON.stringify({
29
      name,
30
    }),
31
  });
32
  if (!resp.ok) {
33
    throw Error(`Failed to set room name: Error HTTP${resp.status}`);
34
  }
35
  return await resp.json();
36
}
37

Other submissions
  • Submitted by jaller94 Deno
    Created 398 days ago
    1
    /**
    2
     * Set the name of a Matrix room.
    3
     *
    4
     * HTTP endpoint: https://spec.matrix.org/v1.5/client-server-api/#put_matrixclientv3roomsroomidstateeventtypestatekey
    5
     *
    6
     * State event description: https://spec.matrix.org/v1.5/client-server-api/#mroomname
    7
     */
    8
    type Matrix = {
    9
      baseUrl: string;
    10
      token: string;
    11
    };
    12
    export async function main(
    13
      matrix_res: Matrix,
    14
      room_id: string,
    15
      name: string = "",
    16
    ) {
    17
      const url = `${
    18
        matrix_res.baseUrl
    19
      }/_matrix/client/v3/rooms/${encodeURIComponent(room_id)}/state/m.room.name/`;
    20
      const resp = await fetch(url, {
    21
        method: "PUT",
    22
        headers: {
    23
          Authorization: `Bearer ${matrix_res.token}`,
    24
          "Content-Type": "application/json",
    25
        },
    26
        body: JSON.stringify({
    27
          name,
    28
        }),
    29
      });
    30
      if (!resp.ok) {
    31
        throw Error(`Failed to set room name: Error HTTP${resp.status}`);
    32
      }
    33
      return await resp.json();
    34
    }
    35