-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforwarder.py
85 lines (62 loc) · 3.13 KB
/
forwarder.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
''' A script to send all messages from one chat to another. '''
import asyncio
import logging
from telethon.tl.patched import MessageService
from telethon.errors.rpcerrorlist import FloodWaitError
from telethon import TelegramClient
from telethon.sessions import StringSession
from settings import API_ID, API_HASH, REPLACEMENTS, forwards, get_forward, update_offset, STRING_SESSION
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
SENT_VIA = f'\n__Sent via__ `{str(__file__)}`'
def intify(string):
try:
return int(string)
except:
return string
def replace(message):
for old, new in REPLACEMENTS.items():
message.text = str(message.text).replace(old, new)
return message
async def forward_job():
''' the function that does the job 😂 '''
loop = asyncio.get_running_loop()
while True:
if STRING_SESSION:
session = StringSession(STRING_SESSION)
else:
session = 'forwarder'
async with TelegramClient(session, API_ID, API_HASH) as client:
error_occured = False
for forward in forwards:
from_chat, to_chat, offset = get_forward(forward)
if not offset:
offset = 0
last_id = 0
async for message in client.iter_messages(intify(from_chat), reverse=True, offset_id=offset):
if isinstance(message, MessageService):
continue
try:
await client.send_message(intify(to_chat), replace(message))
last_id = str(message.id)
logging.info('forwarding message with id = %s', last_id)
update_offset(forward, last_id)
except FloodWaitError as fwe:
print(f'{fwe}')
await asyncio.sleep(delay=fwe.seconds)
except Exception as err:
logging.exception(err)
error_occured = True
break
logging.info('Completed working with %s', forward)
#await client.send_file('me', 'config.ini', caption='This is your config file for telegram-chat-forward.')
message = 'Your forward job has completed.' if not error_occured else 'Some errors occured. Please see the output on terminal. Contact Developer.'
await client.send_message('me', f'''Hi !
\n**{message}**
\n**Telegram Chat Forward** is developed by @AahnikDaw.
\nPlease star 🌟 on [GitHub](https://github.com/aahnik/telegram-chat-forward).
{SENT_VIA}''', link_preview=False)
await asyncio.sleep(5)
if __name__ == "__main__":
# assert forwards
asyncio.run(forward_job())