Add ability to send text messages

master
DustyP 6 years ago
parent 2eee29e5e8
commit 60da462315

@ -154,14 +154,15 @@ class API:
self.access_token = None self.access_token = None
async def room_send(self, room_id: str, event_type: str, content: dict): async def room_send(self, room_id: str, event_type: str, content: dict):
txnid = uuid.uuid4()
if room_id.startswith("!") and ":" in room_id: 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: elif room_id.startswith("#") and ":" in room_id:
path = self.build_url(f"directory/room/{room_id}") path = self.build_url(f"directory/room/{room_id}")
resp = await self.send("GET", path) resp = await self.send("GET", path)
if resp.get("room_id"): if resp.get("room_id"):
path = self.build_url( path = self.build_url(
f'rooms/{resp["room_id"]}/send/{event_type}/{uuid.uuid4()}' f'rooms/{resp["room_id"]}/send/{event_type}/{txnid}'
) )
else: else:
raise RuntimeWarning(resp) raise RuntimeWarning(resp)

@ -19,7 +19,7 @@ class Client:
self.rooms: Dict[str, Room] = {} self.rooms: Dict[str, Room] = {}
self.api: Optional[API] = None self.api: Optional[API] = None
self.running: bool = False self.running: bool = False
self.sync_timeout: int = 1000 self.sync_timeout: int = 30000
self.sync_since: Optional[str] = None self.sync_since: Optional[str] = None
self.sync_full_state: bool = False self.sync_full_state: bool = False
self.sync_set_presence: str = "online" self.sync_set_presence: str = "online"
@ -159,3 +159,17 @@ class Client:
if not callable(handler): if not callable(handler):
raise TypeError(f'handler must be a callable not {type(handler)}') raise TypeError(f'handler must be a callable not {type(handler)}')
self.event_dispatchers[event_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)

@ -90,5 +90,8 @@ class Room:
elif isinstance(content, MRoomPowerLevelsContent): elif isinstance(content, MRoomPowerLevelsContent):
self.power_levels = content 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): def __eq__(self, other):
return other.__class__ == self.__class__ and other.id == self.id return other.__class__ == self.__class__ and other.id == self.id

Loading…
Cancel
Save