-
Notifications
You must be signed in to change notification settings - Fork 0
/
forwardgram.py
51 lines (40 loc) · 1.84 KB
/
forwardgram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from telethon import TelegramClient, events, sync
from telethon.tl.types import InputChannel, InputGroupCall
import yaml
import sys
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logging.getLogger('telethon').setLevel(level=logging.WARNING)
logger = logging.getLogger(__name__)
def start(config):
client = TelegramClient(config["session_name"],
config["api_id"],
config["api_hash"])
client.start()
input_channels_entities = []
output_channel_entities = []
for d in client.iter_dialogs():
if d.name in config["input_channel_names"]:
input_channels_entities.append(InputChannel(d.entity.id, d.entity.access_hash))
if d.name in config["output_channel_names"]:
output_channel_entities.append(InputChannel(d.entity.id, d.entity.access_hash))
if not output_channel_entities:
logger.error(f"Could not find any output channels in the user's dialogs")
sys.exit(1)
if not input_channels_entities:
logger.error(f"Could not find any input channels in the user's dialogs")
sys.exit(1)
logging.info(
f"Listening on {len(input_channels_entities)} channels. Forwarding 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:
await client.forward_messages(output_channel, event.message)
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)