Add references (removed inner git)
This commit is contained in:
32
references/matrix-bot-chat-reference/sample/basic_client.py
Normal file
32
references/matrix-bot-chat-reference/sample/basic_client.py
Normal file
@@ -0,0 +1,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())
|
||||
Reference in New Issue
Block a user