0
Set room topic
One script reply has been approved by the moderators Verified
Created by jaller94 702 days ago Viewed 9314 times
0
Submitted by jaller94 Deno
Verified 702 days ago
1
/**
2
 * Set the topic of a Matrix room. A topic is a short message detailing what is currently being discussed in the 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/#mroomtopic
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
  topic: string = "",
16
) {
17
  const url = `${
18
    matrix_res.baseUrl
19
  }/_matrix/client/v3/rooms/${encodeURIComponent(room_id)}/state/m.room.topic/`;
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
      topic,
28
    }),
29
  });
30
  if (!resp.ok) {
31
    throw Error(`Failed to set room topic: Error HTTP${resp.status}`);
32
  }
33
  return await resp.json();
34
}
35