-
Notifications
You must be signed in to change notification settings - Fork 9
/
relay.py
executable file
·53 lines (41 loc) · 1.55 KB
/
relay.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
52
53
#!/usr/bin/env python
import asyncio
import logging
from telethon import TelegramClient, events
import config
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
client = TelegramClient(config.SESSION_NAME, config.API_ID, config.API_HASH)
client.start()
RELAY_MAP = {}
async def setup():
user = await client.get_me()
logger.info('Started serving as {}'.format(user.first_name))
await client.get_dialogs()
for x in config.RELAY_MAP.split(';'):
if not x:
return
key, values = x.split(':', 1)
values = values.split(',')
RELAY_MAP[int(key)] = [int(x) for x in values]
@client.on(events.NewMessage)
async def my_event_handler(event):
for chat_id, relays in RELAY_MAP.items():
if event.chat and event.chat.id == chat_id:
for relay in relays:
logger.info('Sending message from {} to {}'.format(event.chat.id, relay))
if config.FORWARD:
await client.forward_messages(relay, event.message)
else:
await client.send_message(relay, event.message)
break
else:
for relay in RELAY_MAP.get('default', []):
logger.info('Sending message from {} to {}'.format(event.chat.id, relay))
if config.FORWARD:
await client.forward_messages(relay, event.message)
else:
await client.send_message(relay, event.message)
loop = asyncio.get_event_loop()
loop.run_until_complete(setup())
client.run_until_disconnected()