diff --git a/morpheus/core/api.py b/morpheus/core/api.py index 8344b1f..03bd833 100644 --- a/morpheus/core/api.py +++ b/morpheus/core/api.py @@ -154,14 +154,15 @@ class API: self.access_token = None async def room_send(self, room_id: str, event_type: str, content: dict): + txnid = uuid.uuid4() if room_id.startswith("!") and ":" in room_id: - path = self.build_url(f"rooms/{room_id}/send/{event_type}/{uuid.uuid4()}") + path = self.build_url(f"rooms/{room_id}/send/{event_type}/{txnid}") elif room_id.startswith("#") and ":" in room_id: path = self.build_url(f"directory/room/{room_id}") resp = await self.send("GET", path) if resp.get("room_id"): path = self.build_url( - f'rooms/{resp["room_id"]}/send/{event_type}/{uuid.uuid4()}' + f'rooms/{resp["room_id"]}/send/{event_type}/{txnid}' ) else: raise RuntimeWarning(resp) diff --git a/morpheus/core/client.py b/morpheus/core/client.py index 85849ed..a1143a2 100644 --- a/morpheus/core/client.py +++ b/morpheus/core/client.py @@ -19,7 +19,7 @@ class Client: self.rooms: Dict[str, Room] = {} self.api: Optional[API] = None self.running: bool = False - self.sync_timeout: int = 1000 + self.sync_timeout: int = 30000 self.sync_since: Optional[str] = None self.sync_full_state: bool = False self.sync_set_presence: str = "online" @@ -159,3 +159,17 @@ class Client: if not callable(handler): raise TypeError(f'handler must be a callable not {type(handler)}') self.event_dispatchers[event_type] = handler + + async def send_room_message(self, room: Room, content: dict): + await self.api.room_send(room_id=room.id, event_type='m.room.message', content=content) + + async def send_text(self, room: Room, body: str, formatted_body: str = None, format_type: str = None): + content = { + 'msgtype': 'm.text', + 'body': body + } + if formatted_body and format_type: + content['format'] = format_type + content['formatted_body'] = formatted_body + + await self.send_room_message(room=room, content=content) diff --git a/morpheus/core/room.py b/morpheus/core/room.py index 59e8879..30d5f3b 100644 --- a/morpheus/core/room.py +++ b/morpheus/core/room.py @@ -90,5 +90,8 @@ class Room: elif isinstance(content, MRoomPowerLevelsContent): self.power_levels = content + async def send_text(self, body: str, formatted_body: str = None, format_type: str = 'org.matrix.custom.html'): + await self.client.send_text(self, body, formatted_body, format_type) + def __eq__(self, other): return other.__class__ == self.__class__ and other.id == self.id