0
Set room state
One script reply has been approved by the moderators Verified
Created by jaller94 766 days ago Viewed 9290 times
0
Submitted by jaller94 Deno
Verified 766 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