From 80511e6bb702ac9f68ca2dc96ce0d45121962d43 Mon Sep 17 00:00:00 2001 From: likewhoa Date: Sun, 17 Dec 2023 12:08:29 -0500 Subject: [PATCH] Initial commit of bot.py --- bot.py | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 bot.py diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..e96f5b2 --- /dev/null +++ b/bot.py @@ -0,0 +1,96 @@ +import logging +logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s', + level=logging.INFO) + +from telethon import TelegramClient, events +from telethon.tl.types import InputChannel + +import asyncio, json, time, sys, yaml, os + +loop = asyncio.get_event_loop() + +async def _wait_until(somepredicate, timeout, period=0.25): + mustend = time.time() + timeout + while time.time() < mustend: + if somepredicate: return True + await asyncio.sleep(period) + return False + +def start(config): + client = TelegramClient('anon', + config["api_id"], + config["api_hash"]) + client.start() + + input_channels_entities = [] + output_channel_entities = [] + + # loop through user dialogs + for dialog in client.iter_dialogs(): + if dialog.name in config["input_channel_names"]: + logging.info(f"Adding {dialog.name} to input channels.") + input_channels_entities.append(dialog.entity.id) + elif dialog.name in config["output_channel_names"]: + logging.info(f"Adding {dialog.name} to output channels.") + output_channel_entities.append(dialog.entity.id) + + if not output_channel_entities: + logging.error("Could not find any output channels in the user's dialogs") + sys.exit(1) + elif not input_channels_entities: + logging.error("Could not find any input channels in the user's dialogs") + sys.exit(1) + + logging.info(f"Listening on {len(input_channels_entities)} channels. Mirroring messages to {len(output_channel_entities)} channels.") + + @client.on(events.NewMessage(chats=input_channels_entities)) + async def handler(event): + for output_channel in output_channel_entities: + if event.media is not None: + os.makedirs('usermedia', exist_ok=True) + + try: + path = await client.download_media(event.media, file='usermedia/') + + if path is not None: + logging.info(f'Sending file to {output_channel}.') + await client.send_file( + output_channel, + path, + caption=event.message.message + ) + + # Remove file after upload + os.remove(path) + else: + logging.info(f'Sending message to {output_channel}..') + await client.send_message( + output_channel, + event.message + ) + except Exception as e: + print(f'Error:\n{e}') + else: + logging.info(f'Sending message to {output_channel}.') + await client.send_message( + output_channel, + event.message + ) + + @client.on(events.NewMessage(outgoing=True, pattern=r'/test')) + async def _(event): + sender = await event.get_sender() + chat = await event.get_chat() + me = await client.get_me() + + print(f'chat:{chat.id}') + + client.run_until_disconnected() + +if __name__ == "__main__": + if len(sys.argv) < 2: + print(f"Usage: {sys.argv[0]} {{CONFIG_PATH}}") + sys.exit(1) + with open(sys.argv[1], 'rb') as f: + config = yaml.safe_load(f) + start(config) \ No newline at end of file