1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
import asyncio
from nio import AsyncClient, MatrixRoom, RoomMessageText
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 main() -> None:
client = AsyncClient("https://matrix.example.org", "@alice:example.org")
client.add_event_callback(message_callback, RoomMessageText)
print(await client.login("my-secret-password"))
# "Logged in as @alice:example.org device id: RANDOMDID"
# If you made a new room and haven't joined as that user, you can use
# await client.join("your-room-id")
await client.room_send(
# Watch out! If you join an old room you'll see lots of old messages
room_id="!my-fave-room:example.org",
message_type="m.room.message",
content={"msgtype": "m.text", "body": "Hello world!"},
)
await client.sync_forever(timeout=30000) # milliseconds
asyncio.run(main())
|