hakkoso/chat.py

40 lines
1.4 KiB
Python
Raw Normal View History

from nio import AsyncClient # pip3 install matrix-nio
import asyncio
class Chatter():
def __init__(self, homeserver, defaultroom):
self.homeserver = homeserver
self.defaultroom = defaultroom
def login(self, username, device, token):
self.client = AsyncClient(self.homeserver, username)
self.client.restore_login(
user_id=username,
device_id=device,
access_token = token)
async def send(self, message, room=None):
print(f"Sending {message}")
await self.client.room_send(room_id = room or self.defaultroom,
message_type="m.room.message",
content = {
"msgtype": "m.text",
"body": message
}
)
await self.client.close()
def send_sync(self, msg):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
coroutine = chat.send(msg)
loop.run_until_complete(coroutine)
chat = Chatter('https://chat.nixo.xyz', '!zJiIjYrICcdvRPKuen:nixo.xyz')
chat.login(
'@rabbit:nixo.xyz', 'ESJZVKGHVF',
'MDAxNmxvY2F0aW9uIG5peG8ueHl6CjAwMTNpZGVudGlmaWVyIGtleQowMDEwY2lkIGdlbiA9IDEKMDAyM2NpZCB1c2VyX2lkID0gQHJhYmJpdDpuaXhvLnh5egowMDE2Y2lkIHR5cGUgPSBhY2Nlc3MKMDAyMWNpZCBub25jZSA9IDUjbUAjTHdlYjpxeEd3MlYKMDAyZnNpZ25hdHVyZSBoyKyldyQxYejjrznz_M47h6TQFxCv3PxpcESV0OnGago')