1
Join room
One script reply has been approved by the moderators Verified

Joins a Matrix room.

Created by jaller94 624 days ago Viewed 4933 times
0
Submitted by jaller94 Deno
Verified 624 days ago
1
type Matrix = {
2
  baseUrl: string;
3
  token: string;
4
};
5
export async function main(matrix_res: Matrix, room: string) {
6
  if (!matrix_res.token) {
7
    throw Error("Joining a room requires an access token.");
8
  }
9
  const resp = await fetch(
10
    `${matrix_res.baseUrl}/_matrix/client/v3/join/${encodeURIComponent(room)}`,
11
    {
12
      method: "POST",
13
      headers: {
14
        Authorization: `Bearer ${matrix_res.token}`,
15
      },
16
    },
17
  );
18
  if (!resp.ok) {
19
    throw Error(`Failed to join room: Error HTTP${resp.status}`);
20
  }
21
}
22