1

Get room state by type and state_key

by
Published Aug 10, 2022

Requests the content of a room state event. This does not return the entire room state. Parameters are the room (alias or id), the event type and an optional state key.

Script matrix Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 7 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
) {
13
  let url = `${matrix_res.baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(
14
    room_id,
15
  )}/state/${encodeURIComponent(type)}/`;
16
  if (state_key) {
17
    url += encodeURIComponent(state_key);
18
  }
19
  const resp = await fetch(url, {
20
    headers: {
21
      Authorization: `Bearer ${matrix_res.token}`,
22
    },
23
  });
24
  if (!resp.ok) {
25
    throw Error(`Failed to read room state: Error HTTP${resp.status}`);
26
  }
27
  return {
28
    content: await resp.json(),
29
  };
30
}
31

Other submissions
  • Submitted by jaller94 Deno
    Created 399 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
    ) {
    11
      let url = `${matrix_res.baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(
    12
        room_id,
    13
      )}/state/${encodeURIComponent(type)}/`;
    14
      if (state_key) {
    15
        url += encodeURIComponent(state_key);
    16
      }
    17
      const resp = await fetch(url, {
    18
        headers: {
    19
          Authorization: `Bearer ${matrix_res.token}`,
    20
        },
    21
      });
    22
      if (!resp.ok) {
    23
        throw Error(`Failed to read room state: Error HTTP${resp.status}`);
    24
      }
    25
      return {
    26
        content: await resp.json(),
    27
      };
    28
    }
    29