Last active
March 9, 2020 11:31
-
-
Save Karasiq/10b52299b7ee1343ea9498166a499894 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from telethon import TelegramClient, events, sync | |
import socks, logging, asyncio, abc | |
api_id = 17349 | |
api_hash = '344583e45741c457fe1862106095a5eb' | |
proxy = (socks.SOCKS5, 'deimos.public.opennetwork.cc', 1090, True, '577372493', 'Hm2NcWbp') | |
# proxy = (socks.SOCKS5, 'localhost', 9050) | |
phone = "+7телефон" | |
password = "пароль" | |
mappings = [ | |
(["Откуда читаем", "откуда читаем2"], ["Куда отправляем"]) | |
] | |
client = TelegramClient('telebot', api_id, api_hash, proxy=proxy) | |
client.start(phone, password) | |
class ForwardBot(abc.ABC): | |
async def register_forward(self, client, source_names, dest_names): | |
print("Registering forward from ", source_names, " to ", dest_names) | |
dialogs = filter(lambda d: any(s in d.name for s in source_names), await client.get_dialogs()) | |
dialogs_entities = list(map(lambda d: d.entity, dialogs)) | |
print(dialogs_entities) | |
targets = filter(lambda d: any(s in d.name for s in dest_names), await client.get_dialogs()) | |
targets_entities = list(map(lambda d: d.input_entity, targets)) | |
print(targets_entities) | |
async def handle_message(event): | |
if "/joinchat/" in event.message.text: | |
return | |
for target in targets_entities: | |
await client.forward_messages(target, event.message) | |
client.add_event_handler(handle_message, events.NewMessage(chats=dialogs_entities)) | |
async def register_all_forwards(self, client, mappings): | |
print("Registering all forwards: ", mappings) | |
for (source_names, dest_names) in mappings: | |
await self.register_forward(client, source_names, dest_names) | |
logging.basicConfig(level=logging.INFO) | |
sync.syncify(ForwardBot) | |
bot = ForwardBot() | |
bot.register_all_forwards(client, mappings) | |
client.run_until_disconnected() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment