From c0a5fb3d3e616fa320a307f313160cfd6d18b81a Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Thu, 16 May 2019 14:58:34 +0530 Subject: [PATCH] Add core logic for event handlers. Add gitignore, sample config. --- .gitignore | 3 +++ bot.py | 23 +++++++++++++++++++++++ sample.config.py | 13 +++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 .gitignore create mode 100644 bot.py create mode 100644 sample.config.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9d669ae --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +config.py +Temec.session +__pycache__/ diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..e6b4349 --- /dev/null +++ b/bot.py @@ -0,0 +1,23 @@ +from telethon import TelegramClient, events +from matrix_client.client import MatrixClient +import config + + +async def send_to_matrix(event, room): + room.send_text(event.raw_text) + + +matrix_bot = MatrixClient(config.matrix_creds['server']) +matrix_bot.login( + username=config.matrix_creds['username'], + password=config.matrix_creds['password'] +) +client = TelegramClient('Temec', config.api_id, config.api_hash).start() +rooms = matrix_bot.get_rooms() + +for chan_id, room_id in config.mappings: + client.add_event_handler(lambda x: send_to_matrix(x, rooms[room_id]), + events.NewMessage(chats=[chan_id])) + + +client.run_until_disconnected() diff --git a/sample.config.py b/sample.config.py new file mode 100644 index 0000000..e611225 --- /dev/null +++ b/sample.config.py @@ -0,0 +1,13 @@ +api_id = 12345 +api_hash = "abcdefghj..." + +matrix_creds = { + "username": "username", + "password": "password", + "server": "server" +} + +mappings = [ + # Each pair is tg channel id, matrix room id + (-123456789, '!asdfghjkl'), +]