Skip to content

Instantly share code, notes, and snippets.

@palkx
Last active September 18, 2019 12:38
Show Gist options
  • Save palkx/6a3062ea1647aed7c6a0ff49c5de105c to your computer and use it in GitHub Desktop.
Save palkx/6a3062ea1647aed7c6a0ff49c5de105c to your computer and use it in GitHub Desktop.
This is simple script based on telethon which can forward messages from multiple channels to your one channel, and you can create multiple rules in config.yml. Requires pyyaml and telethon install from pip3.
api_id: 111111 # This has to be an integer.
api_hash: '32symbolsstring' # Long 32 characters hash identifier.
session_name: 'session_name' # Session name.
channels:
- input_channels:
- 'channel'
- 'channel'
- 'channel'
output_channel: 'channel'
- input_channels:
- 'channel'
- 'channel'
output_channel: 'channel'
#!/usr/bin/python3
from telethon.sync import TelegramClient, events
from telethon.tl.types import InputChannel
import yaml
import sys
def start(config):
client = TelegramClient(config["session_name"], config["api_id"], config["api_hash"])
client.start()
# ice = input channels entities
ice = []
# oc = output channels entities
oce = []
# cc - channelsConfig
for index, cc in enumerate(config["channels"]):
ice.append([])
if cc["input_channels"] and cc["output_channel"]:
for d in client.iter_dialogs():
if d.name in cc["input_channels"]:
ice[index].append(d.entity.id)
if d.name == cc["output_channel"]:
oce.append(InputChannel(d.entity.id, d.entity.access_hash))
@client.on(events.NewMessage(chats=ice[index]))
# @client.on(events.NewMessage(chats=cc["input_channels"]))
async def handler(event, i=index):
sender = await event.get_sender()
ec = event
ec.message.message += "\n\n @" + sender.username
print(ec)
await client.send_message(oce[i], ec.message)
client.run_until_disconnected()
with open("config.yml", 'rb') as f:
config = yaml.full_load(f)
start(config)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment