1
Get room state by type and state_key
One script reply has been approved by the moderators Verified

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.

Created by jaller94 598 days ago Viewed 2449 times
0
Submitted by jaller94 Deno
Verified 547 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