talkomatic.py 0.1.2
An easy-to-use Python versatile wrapper for the Talkomatic Bot interface ensuring best practices for making bots, and handling the REST API.
 
Loading...
Searching...
No Matches
rooms.py
Go to the documentation of this file.
1from talkomatic.api.v1.auth import API_AUTH_HEADERS, get_auth_bot_token
2from talkomatic.dataclasses import Room, RoomType, RoomLayoutType
3
4from requests import get as requests_get
5from requests import post as requests_post
6
7from enum import Enum
8
9
10def get_rooms() -> list[Room]:
11 """
12 Get all visible (public/semi-private) rooms from the Talkomatic REST API.
13
14 Returns:
15 list[Room]: A list of all visible rooms.
16 """
17
18 rooms = requests_get(f"https://classic.talkomatic.co/api/v1/rooms?token={get_auth_bot_token()}", headers = API_AUTH_HEADERS).json()
19 return [Room.from_raw_json(room) for room in rooms]
20
21def get_room(room_id: int) -> Room:
22 """
23 Get a room (including private rooms) from the Talkomatic REST API by ID.
24
25 Args:
26 room_id (int): The ID of the room.
27
28 Returns:
29 Room: The room with the given ID.
30 """
31
32 room = requests_get(f"https://classic.talkomatic.co/api/v1/rooms/{room_id}?token={get_auth_bot_token()}", headers = API_AUTH_HEADERS).json()
33 return Room.from_raw_json(room)
34
35def create_room(room_name: str, room_type: RoomType, layout: RoomLayoutType) -> int:
36 """
37 Create a room on the Talkomatic REST API with its name, type and layout.
38
39 Args:
40 room_name (str): The name of the room.
41 room_type (RoomType): The type of the room.
42 layout (RoomLayoutType): The layout of the room.
43
44 Returns:
45 int: The ID of the created room.
46 """
47
48 response = requests_post(f"https://classic.talkomatic.co/api/v1/rooms?token={get_auth_bot_token()}", headers = API_AUTH_HEADERS, json = {
49 "name": room_name,
50 "type": room_type.value,
51 "layout": layout.value
52 })
53 if response.status_code != 200: raise RuntimeError("Failed to create room.")
54 return int(response.json()["roomId"])
55
56class RoomJoinStatus(Enum):
57 """
58 A class representing the error codes for can_join_room
59 API endpoint.
60 """
61
62 SUCCESS = "ok"
63 NOT_FOUND = "NOT_FOUND"
64 ROOM_FULL = "ROOM_FULL"
65 FORBIDDEN = "FORBIDDEN"
66 VALIDATION_ERROR = "VALIDATION_ERROR"
67 SERVER_ERROR = "SERVER_ERROR"
68
69def can_join_room(room_id: int, access_code: int | None = None) -> RoomJoinStatus:
70 """
71 Check if a room can be joined by the user.
72
73 Args:
74 room_id (int): The ID of the room.
75 access_code (int, optional): The access code of the room.
76
77 Returns:
78 bool: Whether the room can be joined.
79 """
80
81 response = requests_post(f"https://classic.talkomatic.co/api/v1/rooms/{room_id}/join?token={get_auth_bot_token()}", headers = API_AUTH_HEADERS, json = {
82 "accessCode": str(access_code) if access_code else None
83 })
84 if response.status_code == 200: return RoomJoinStatus.SUCCESS
85 else: return RoomJoinStatus(response.json()["error"]["code"])
86
87__all__ = ["get_rooms", "get_room", "create_room", "can_join_room", "RoomJoinStatus"]
list[Room] get_rooms()
Definition rooms.py:10
RoomJoinStatus can_join_room(int room_id, int|None access_code=None)
Definition rooms.py:69
Room get_room(int room_id)
Definition rooms.py:21