add send_message
This commit is contained in:
parent
46dc303d9a
commit
6e4becd3df
2 changed files with 34 additions and 6 deletions
|
@ -8,6 +8,7 @@ import logging
|
||||||
import nio
|
import nio
|
||||||
from aiopath import AsyncPath
|
from aiopath import AsyncPath
|
||||||
from typing import (
|
from typing import (
|
||||||
|
Any,
|
||||||
Optional,
|
Optional,
|
||||||
NoReturn,
|
NoReturn,
|
||||||
Union
|
Union
|
||||||
|
@ -39,7 +40,8 @@ class Client(Aobject):
|
||||||
__sync_token_file: AsyncPath
|
__sync_token_file: AsyncPath
|
||||||
__sync_token:Optional[str]
|
__sync_token:Optional[str]
|
||||||
__sync_token_queue: asyncio.Queue[str]
|
__sync_token_queue: asyncio.Queue[str]
|
||||||
__invites_queue: asyncio.Queue[tuple[RoomId, nio.responses.InviteInfo]]
|
__invite_queue: asyncio.Queue[tuple[RoomId, nio.responses.InviteInfo]]
|
||||||
|
__message_queue: asyncio.Queue[tuple[Union[RoomAlias, RoomId], dict[Any,Any]]]
|
||||||
whitelist_rooms: Optional[dict[RoomId, Room]]
|
whitelist_rooms: Optional[dict[RoomId, Room]]
|
||||||
|
|
||||||
async def __init__(
|
async def __init__(
|
||||||
|
@ -75,7 +77,8 @@ class Client(Aobject):
|
||||||
else:
|
else:
|
||||||
self.__sync_token = None
|
self.__sync_token = None
|
||||||
self.__sync_token_queue = asyncio.Queue()
|
self.__sync_token_queue = asyncio.Queue()
|
||||||
self.__invites_queue = asyncio.Queue()
|
self.__invite_queue = asyncio.Queue()
|
||||||
|
self.__message_queue = asyncio.Queue()
|
||||||
resp = await self.__client.login(password)
|
resp = await self.__client.login(password)
|
||||||
if isinstance(resp, nio.responses.LoginError):
|
if isinstance(resp, nio.responses.LoginError):
|
||||||
raise RuntimeError(f"Fail to connect: {resp.message}")
|
raise RuntimeError(f"Fail to connect: {resp.message}")
|
||||||
|
@ -130,6 +133,17 @@ class Client(Aobject):
|
||||||
self.__rooms_by_aliases[room_name] = room
|
self.__rooms_by_aliases[room_name] = room
|
||||||
return room
|
return room
|
||||||
|
|
||||||
|
async def send_message(
|
||||||
|
self,
|
||||||
|
room: Union[RoomAlias, RoomId],
|
||||||
|
message: str,
|
||||||
|
):
|
||||||
|
msg = {
|
||||||
|
"body": message,
|
||||||
|
"msgtype": "m.text"
|
||||||
|
}
|
||||||
|
await self.__message_queue.put((room, msg))
|
||||||
|
|
||||||
async def __save_sync_tocken(self)->NoReturn:
|
async def __save_sync_tocken(self)->NoReturn:
|
||||||
"""
|
"""
|
||||||
Save the sync token.
|
Save the sync token.
|
||||||
|
@ -145,7 +159,7 @@ class Client(Aobject):
|
||||||
Process invites to rooms.
|
Process invites to rooms.
|
||||||
"""
|
"""
|
||||||
while True:
|
while True:
|
||||||
room_id, invite_info = await self.__invites_queue.get()
|
room_id, invite_info = await self.__invite_queue.get()
|
||||||
accept_invite = False
|
accept_invite = False
|
||||||
|
|
||||||
if self.whitelist_rooms is not None:
|
if self.whitelist_rooms is not None:
|
||||||
|
@ -171,7 +185,18 @@ class Client(Aobject):
|
||||||
else:
|
else:
|
||||||
log.info(f"{room_id} left")
|
log.info(f"{room_id} left")
|
||||||
|
|
||||||
self.__invites_queue.task_done()
|
self.__invite_queue.task_done()
|
||||||
|
|
||||||
|
async def __send_messages(self)->NoReturn:
|
||||||
|
"""
|
||||||
|
Send messages from the queue.
|
||||||
|
"""
|
||||||
|
while True:
|
||||||
|
room_name, msg = await self.__message_queue.get()
|
||||||
|
room = await self.resolve_room(room_name)
|
||||||
|
await self.__client.room_send(room.id, "m.room.message", msg)
|
||||||
|
log.debug(f"message sent to room {room.id}")
|
||||||
|
self.__message_queue.task_done()
|
||||||
|
|
||||||
async def __sync(
|
async def __sync(
|
||||||
self,
|
self,
|
||||||
|
@ -195,7 +220,7 @@ class Client(Aobject):
|
||||||
|
|
||||||
invites = sync_resp.rooms.invite
|
invites = sync_resp.rooms.invite
|
||||||
for room_id in invites:
|
for room_id in invites:
|
||||||
await self.__invites_queue.put((room_id, invites[room_id]))
|
await self.__invite_queue.put((room_id, invites[room_id]))
|
||||||
|
|
||||||
async def run(
|
async def run(
|
||||||
self,
|
self,
|
||||||
|
@ -208,7 +233,8 @@ class Client(Aobject):
|
||||||
await asyncio.gather(
|
await asyncio.gather(
|
||||||
self.__sync(sync_timeout=sync_timeout),
|
self.__sync(sync_timeout=sync_timeout),
|
||||||
self.__save_sync_tocken(),
|
self.__save_sync_tocken(),
|
||||||
self.__process_invites()
|
self.__process_invites(),
|
||||||
|
self.__send_messages(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,8 @@ async def main():
|
||||||
os.environ["PASSWD"],
|
os.environ["PASSWD"],
|
||||||
os.environ["ROOMS"].split(",")
|
os.environ["ROOMS"].split(",")
|
||||||
)
|
)
|
||||||
|
room_name = os.environ["ROOMS"].split(",")[0]
|
||||||
|
await client.send_message(room_name, "Beware of Greeks bearing gifts")
|
||||||
await client.run()
|
await client.run()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue