add main.py with basic matrix client implementation
This commit is contained in:
parent
762ee0f921
commit
d5c596a8b1
1 changed files with 38 additions and 0 deletions
38
main.py
Executable file
38
main.py
Executable file
|
|
@ -0,0 +1,38 @@
|
|||
import asyncio
|
||||
import tomllib
|
||||
|
||||
from nio import AsyncClient, MatrixRoom, RoomMessageText, RoomMemberEvent
|
||||
|
||||
|
||||
async def message_callback(room: MatrixRoom, event: RoomMessageText) -> None:
|
||||
print(
|
||||
f"Message received in room {room.display_name}\n"
|
||||
f"{room.user_name(event.sender)} | {event.body}"
|
||||
)
|
||||
|
||||
|
||||
async def membership_callback(room: MatrixRoom, event: RoomMemberEvent) -> None:
|
||||
print(f"Room member event on room '{repr(room)}': {repr(event)}\n")
|
||||
|
||||
if event.content["membership"] == "join":
|
||||
print(f"{event.content['displayname']} joined room")
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
with open("credentials.toml", "rb") as f:
|
||||
config = tomllib.load(f)
|
||||
|
||||
server = config["credentials"]["server"]
|
||||
username = config["credentials"]["username"]
|
||||
password = config["credentials"]["password"]
|
||||
|
||||
client = AsyncClient(server, username)
|
||||
client.add_event_callback(message_callback, RoomMessageText)
|
||||
client.add_event_callback(membership_callback, RoomMemberEvent)
|
||||
|
||||
print(await client.login(password))
|
||||
|
||||
await client.sync_forever(timeout=30000) # milliseconds
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
Loading…
Add table
Add a link
Reference in a new issue