2023-01-07 20:11:46 +00:00
|
|
|
import asyncio
|
2023-01-08 13:01:33 +00:00
|
|
|
import os
|
2023-01-08 12:55:14 +00:00
|
|
|
import sys
|
2023-01-07 21:13:27 +00:00
|
|
|
|
|
|
|
import aiogram as telegram
|
|
|
|
import nio as matrix
|
2023-01-07 20:11:46 +00:00
|
|
|
|
2023-01-08 13:01:33 +00:00
|
|
|
MATRIX_HOMESERVER_URL = os.environ['MATRIX_HOMESERVER_URL']
|
|
|
|
MATRIX_FULL_USER_ID = os.environ['MATRIX_FULL_USER_ID']
|
|
|
|
MATRIX_PASSWORD = os.environ['MATRIX_PASSWORD']
|
|
|
|
TELEGRAM_BOT_TOKEN = os.environ['TELEGRAM_BOT_TOKEN']
|
2023-01-07 21:42:02 +00:00
|
|
|
|
2023-01-07 21:29:12 +00:00
|
|
|
async def main():
|
2023-01-07 21:42:02 +00:00
|
|
|
try:
|
|
|
|
matrix_client = \
|
|
|
|
matrix.AsyncClient(MATRIX_HOMESERVER_URL, MATRIX_FULL_USER_ID)
|
2023-01-08 12:46:08 +00:00
|
|
|
matrix_client.add_event_callback(matrix_on_message,
|
|
|
|
matrix.RoomMessage)
|
2023-01-07 21:42:02 +00:00
|
|
|
await matrix_client.login(MATRIX_PASSWORD)
|
|
|
|
|
2023-01-08 13:54:22 +00:00
|
|
|
telegram_bot = telegram.Bot(token=TELEGRAM_BOT_TOKEN)
|
|
|
|
telegram_dispatcher = telegram.Dispatcher(bot=telegram_bot)
|
|
|
|
telegram_dispatcher.register_message_handler(telegram_on_message)
|
2023-01-07 21:42:02 +00:00
|
|
|
|
2023-01-08 13:54:22 +00:00
|
|
|
await asyncio.gather(
|
|
|
|
matrix_loop(matrix_client),
|
|
|
|
telegram_loop(),
|
|
|
|
)
|
2023-01-07 21:42:02 +00:00
|
|
|
finally:
|
|
|
|
if matrix_client:
|
|
|
|
await matrix_client.close()
|
2023-01-07 21:05:21 +00:00
|
|
|
|
2023-01-08 12:46:08 +00:00
|
|
|
async def matrix_loop(client):
|
|
|
|
await client.sync_forever(timeout=30000)
|
2023-01-07 20:11:46 +00:00
|
|
|
|
2023-01-07 21:29:12 +00:00
|
|
|
async def telegram_loop():
|
2023-01-08 12:55:14 +00:00
|
|
|
print(456, file=sys.stderr)
|
2023-01-07 21:29:12 +00:00
|
|
|
|
2023-01-08 12:46:08 +00:00
|
|
|
async def matrix_on_message(room, event):
|
2023-01-08 12:55:14 +00:00
|
|
|
print(room, event, file=sys.stderr)
|
2023-01-08 12:46:08 +00:00
|
|
|
|
2023-01-08 13:54:22 +00:00
|
|
|
async def telegram_on_message(msg):
|
|
|
|
print(msg, file=sys.stderr)
|
|
|
|
|
2023-01-07 20:11:46 +00:00
|
|
|
if __name__ == '__main__':
|
2023-01-07 21:29:12 +00:00
|
|
|
asyncio.run(main())
|