Created
March 19, 2021 14:18
-
-
Save Chityanj/1011f4630f23e4ef5be1c3f3d72f6a50 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
''' 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 😂 ''' | |
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 | |
if __name__ == "__main__": | |
assert forwards | |
asyncio.run(forward_job()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment