36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
import nio as matrix
|
|
import sys
|
|
|
|
|
|
class MatrixLoop:
|
|
def __init__(self, app):
|
|
self.app = app
|
|
self.client = matrix.AsyncClient(
|
|
app.config.matrix_homeserver_url,
|
|
app.config.matrix_bot_id,
|
|
)
|
|
self.client.add_event_callback(self.on_message, matrix.RoomMessage)
|
|
|
|
async def prepare(self):
|
|
await self.client.login(self.app.config.matrix_password)
|
|
|
|
async def finish(self):
|
|
await self.client.close()
|
|
|
|
async def run(self):
|
|
await self.client.sync_forever(timeout=30000)
|
|
|
|
async def on_message(self, room, event):
|
|
print(room, event, file=sys.stderr)
|
|
|
|
def upgrade_room(self, room, telegram_nickname):
|
|
event_dict = matrix.event_builders.event_builder.EventBuilder(
|
|
name=telegram_nickname
|
|
).as_dict()
|
|
client.room_send(
|
|
room_id=room,
|
|
message_type=event_dict["type"],
|
|
content=event_dict["content"],
|
|
) # предположу что оно так работает
|
|
# https://matrix-nio.readthedocs.io/en/latest/nio.html#module-nio.event_builders.state_events
|