0

Set room state

by
Published Nov 17, 2022
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
  room_id: string,
10
  type: string,
11
  state_key?: string,
12
  json_content: string,
13
) {
14
  let url = `${matrix_res.baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(
15
    room_id,
16
  )}/state/${encodeURIComponent(type)}/`;
17
  if (state_key) {
18
    url += encodeURIComponent(state_key);
19
  }
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_content,
27
  });
28
  if (!resp.ok) {
29
    throw Error(`Failed to read room state: Error HTTP${resp.status}`);
30
  }
31
  return await resp.json();
32
}
33

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
      room_id: string,
    8
      type: string,
    9
      state_key?: string,
    10
      json_content: string,
    11
    ) {
    12
      let url = `${matrix_res.baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(
    13
        room_id,
    14
      )}/state/${encodeURIComponent(type)}/`;
    15
      if (state_key) {
    16
        url += encodeURIComponent(state_key);
    17
      }
    18
      const resp = await fetch(url, {
    19
        method: "PUT",
    20
        headers: {
    21
          Authorization: `Bearer ${matrix_res.token}`,
    22
          "Content-Type": "application/json",
    23
        },
    24
        body: json_content,
    25
      });
    26
      if (!resp.ok) {
    27
        throw Error(`Failed to read room state: Error HTTP${resp.status}`);
    28
      }
    29
      return await resp.json();
    30
    }
    31