import time import asyncio from telethon.sync import TelegramClient class TelegramForwarder: def __init__(self, api_id, api_hash, phone_number): self.api_id = api_id self.api_hash = api_hash self.phone_number = phone_number self.client = TelegramClient('session_' + phone_number, api_id, api_hash) async def list_chats(self): await self.client.connect() # Ensure you're authorized if not await self.client.is_user_authorized(): await self.client.send_code_request(self.phone_number) await self.client.sign_in(self.phone_number, input('Enter the code: ')) # Get a list of all the dialogs (chats) dialogs = await self.client.get_dialogs() chats_file = open(f"chats_of_{self.phone_number}.txt", "w") # Print information about each chat for dialog in dialogs: print(f"Chat ID: {dialog.id}, Title: {dialog.title}") chats_file.write(f"Chat ID: {dialog.id}, Title: {dialog.title} \n") print("List of groups printed successfully!") async def forward_messages_to_channel(self, source_chat_id, destination_channel_id, keywords): await self.client.connect() # Ensure you're authorized if not await self.client.is_user_authorized(): await self.client.send_code_request(self.phone_number) await self.client.sign_in(self.phone_number, input('Enter the code: ')) replytexts ="None" ref_msg = "None" previous_message_UK ="None" previous_message_Alex ="None" previous_message_GoldPro ="None" previous_message_GoldKiller ="None" previous_message_VIPGold ="None" previous_message_GoldForex ="None" x=0 destination_channel_id_UK= -1002477328454 keywords_UK = "MyOwnUK: " source_chat_id_UK=-1002466214276 keywords_Alex = "Alex: " source_chat_id_Alex= -1001678550622 keywords_GoldPro = "GoldPro: " source_chat_id_GoldPro=-1002349439315 keywords_GoldKiller = "GoldKiller: " source_chat_id_GoldKiller=-1001775386468 keywords_VIPGold = "VIPGold: " source_chat_id_VIPGold=-1001216331979 keywords_GoldForex = "GoldForex: " source_chat_id_GoldForex=-1001478404953 last_message_id_UK = (await self.client.get_messages(source_chat_id_UK, limit=1))[0].id last_message_id_Alex = (await self.client.get_messages(source_chat_id_Alex, limit=1))[0].id last_message_id_GoldPro = (await self.client.get_messages(source_chat_id_GoldPro, limit=1))[0].id last_message_id_GoldKiller = (await self.client.get_messages(source_chat_id_GoldKiller, limit=1))[0].id last_message_id_VIPGold = (await self.client.get_messages(source_chat_id_VIPGold, limit=1))[0].id last_message_id_GoldForex = (await self.client.get_messages(source_chat_id_GoldForex, limit=1))[0].id while True: print("Checking for messages and forwarding them...") # Get new messages since the last checked message messages_UK = await self.client.get_messages(source_chat_id_UK, min_id=last_message_id_UK-1, limit=None) messages_Alex = await self.client.get_messages(source_chat_id_Alex, min_id=last_message_id_Alex-1, limit=None) messages_GoldPro = await self.client.get_messages(source_chat_id_GoldPro, min_id=last_message_id_GoldPro-1, limit=None) messages_GoldKiller = await self.client.get_messages(source_chat_id_GoldKiller, min_id=last_message_id_GoldKiller-1, limit=None) messages_VIPGold = await self.client.get_messages(source_chat_id_VIPGold, min_id=last_message_id_VIPGold-1, limit=None) messages_GoldForex = await self.client.get_messages(source_chat_id_GoldForex, min_id=last_message_id_GoldForex-1, limit=None) print(f"LMID UK: {last_message_id_UK} ,Alex: {last_message_id_Alex} ,GoldPro: {last_message_id_GoldPro} ,GoldKiller: {last_message_id_GoldKiller} ,VIPGold: {last_message_id_VIPGold} ,GoldForex: {last_message_id_GoldForex}") for message_UK in reversed(messages_UK): if message_UK.text != previous_message_UK: previous_message_UK =message_UK.text if message_UK.reply_to_msg_id: replytexts = await self.client.get_messages(source_chat_id_UK, min_id=message_UK.reply_to_msg_id-1, limit=None) x = len(replytexts) if (x>0): ref_msg=replytexts[x-1].text if not message_UK.edit_date: print(f"Message contains: {message_UK.text}") await self.client.send_message(destination_channel_id_UK, keywords_UK+"\n"+str(message_UK.id)+"\n\n"+message_UK.text+"\n\nMessage_is_reply_of:\n"+str(message_UK.reply_to_msg_id)+"\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Message forwarded from UK") await asyncio.sleep(3) if message_UK.edit_date: print(f"Edited Message contains: {message_UK.text}") await self.client.send_message(destination_channel_id_UK, keywords_UK+"\nEdited Message:"+"\n"+str(message_UK.id)+"\n\n"+message_UK.text+"\n\nMessage_is_reply_of:\n"+str(message_UK.reply_to_msg_id)+"\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Edited Message forwarded from UK") await asyncio.sleep(3) replytexts ="None" ref_msg = "None" x=0 last_message_id_UK = max(last_message_id_UK, message_UK.id) # Update the last message ID for message_Alex in reversed(messages_Alex): if message_Alex.text != previous_message_Alex: previous_message_Alex =message_Alex.text if message_Alex.reply_to_msg_id: replytexts = await self.client.get_messages(source_chat_id_Alex, min_id=message_Alex.reply_to_msg_id-1, limit=None) x = len(replytexts) if (x>0): ref_msg=replytexts[x-1].text if not message_Alex.edit_date: print(f"Message contains: {message_Alex.text}") await self.client.send_message(destination_channel_id_UK, keywords_Alex+"\n"+str(message_Alex.id)+"\n\n"+message_Alex.text+"\n\nMessage_is_reply_of:\n"+str(message_Alex.reply_to_msg_id)+"\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Message forwarded from Alex") await asyncio.sleep(3) if message_Alex.edit_date: print(f"Edited Message contains: {message_Alex.text}") await self.client.send_message(destination_channel_id_UK, keywords_Alex+"\nEdited Message:"+"\n"+str(message_Alex.id)+"\n\n"+message_Alex.text+"\n\nMessage_is_reply_of:\n"+str(message_Alex.reply_to_msg_id)+"\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Edited Message forwarded from Alex") await asyncio.sleep(3) replytexts ="None" ref_msg = "None" x=0 last_message_id_Alex = max(last_message_id_Alex, message_Alex.id) # Update the last message ID for message_GoldPro in reversed(messages_GoldPro): if message_GoldPro.text != previous_message_GoldPro: previous_message_GoldPro =message_GoldPro.text if message_GoldPro.reply_to_msg_id: replytexts = await self.client.get_messages(source_chat_id_GoldPro, min_id=message_GoldPro.reply_to_msg_id-1, limit=None) x = len(replytexts) if (x>0): ref_msg=replytexts[x-1].text if not message_GoldPro.edit_date: print(f"Message contains: {message_GoldPro.text}") await self.client.send_message(destination_channel_id_UK, keywords_GoldPro+"\n"+str(message_GoldPro.id)+"\n\n"+message_GoldPro.text+"\n\nMessage_is_reply_of:\n"+str(message_GoldPro.reply_to_msg_id)+"\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Message forwarded from GoldPro") await asyncio.sleep(3) if message_GoldPro.edit_date: print(f"Edited Message contains: {message_GoldPro.text}") await self.client.send_message(destination_channel_id_UK, keywords_GoldPro+"\nEdited Message:"+"\n"+str(message_GoldPro.id)+"\n\n"+message_GoldPro.text+"\n\nMessage_is_reply_of:\n"+str(message_GoldPro.reply_to_msg_id)+"\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Edited Message forwarded from GoldPro") await asyncio.sleep(3) replytexts ="None" ref_msg = "None" x=0 last_message_id_GoldPro = max(last_message_id_GoldPro, message_GoldPro.id) # Update the last message ID for message_GoldKiller in reversed(messages_GoldKiller): if message_GoldKiller.text != previous_message_GoldKiller: previous_message_GoldKiller =message_GoldKiller.text if message_GoldKiller.reply_to_msg_id: replytexts = await self.client.get_messages(source_chat_id_GoldKiller, min_id=message_GoldKiller.reply_to_msg_id-1, limit=None) x = len(replytexts) if (x>0): ref_msg=replytexts[x-1].text if not message_GoldKiller.edit_date: print(f"Message contains: {message_GoldKiller.text}") await self.client.send_message(destination_channel_id_UK, keywords_GoldKiller+"\n"+str(message_GoldKiller.id)+"\n\n"+message_GoldKiller.text+"\n\nMessage_is_reply_of:\n"+str(message_GoldKiller.reply_to_msg_id)+"\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Message forwarded from GoldKiller") await asyncio.sleep(3) if message_GoldKiller.edit_date: print(f"Edited Message contains: {message_GoldKiller.text}") await self.client.send_message(destination_channel_id_UK, keywords_GoldKiller+"\nEdited Message:"+"\n"+str(message_GoldKiller.id)+"\n\n"+message_GoldKiller.text+"\n\nMessage_is_reply_of:\n"+str(message_GoldKiller.reply_to_msg_id)+"\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Edited Message forwarded from GoldKiller") await asyncio.sleep(3) replytexts ="None" ref_msg = "None" x=0 last_message_id_GoldKiller = max(last_message_id_GoldKiller, message_GoldKiller.id) # Update the last message ID for message_VIPGold in reversed(messages_VIPGold): if message_VIPGold.text != previous_message_VIPGold: previous_message_VIPGold =message_VIPGold.text if message_VIPGold.reply_to_msg_id: replytexts = await self.client.get_messages(source_chat_id_VIPGold, min_id=message_VIPGold.reply_to_msg_id-1, limit=None) x = len(replytexts) if (x>0): ref_msg=replytexts[x-1].text if not message_VIPGold.edit_date: print(f"Message contains: {message_VIPGold.text}") await self.client.send_message(destination_channel_id_UK, keywords_VIPGold+"\n"+str(message_VIPGold.id)+"\n\n"+message_VIPGold.text+"\n\nMessage_is_reply_of:\n"+str(message_VIPGold.reply_to_msg_id)+"\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Message forwarded from VIPGold") await asyncio.sleep(3) if message_VIPGold.edit_date: print(f"Edited Message contains: {message_VIPGold.text}") await self.client.send_message(destination_channel_id_UK, keywords_VIPGold+"\nEdited Message:"+"\n"+str(message_VIPGold.id)+"\n\n"+message_VIPGold.text+"\n\nMessage_is_reply_of:\n"+str(message_VIPGold.reply_to_msg_id)+"\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Edited Message forwarded from VIPGold") await asyncio.sleep(3) replytexts ="None" ref_msg = "None" x=0 last_message_id_VIPGold = max(last_message_id_VIPGold, message_VIPGold.id) # Update the last message ID for message_GoldForex in reversed(messages_GoldForex): if message_GoldForex.text != previous_message_GoldForex: previous_message_GoldForex =message_GoldForex.text if message_GoldForex.reply_to_msg_id: replytexts = await self.client.get_messages(source_chat_id_GoldForex, min_id=message_GoldForex.reply_to_msg_id-1, limit=None) x = len(replytexts) if (x>0): ref_msg=replytexts[x-1].text if not message_GoldForex.edit_date: print(f"Message contains: {message_GoldForex.text}") await self.client.send_message(destination_channel_id_UK, keywords_GoldForex+"\n"+str(message_GoldForex.id)+"\n\n"+message_GoldForex.text+"\n\nMessage_is_reply_of:\n"+str(message_GoldForex.reply_to_msg_id)+"\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Message forwarded from GoldForex") await asyncio.sleep(3) if message_GoldForex.edit_date: print(f"Edited Message contains: {message_GoldForex.text}") await self.client.send_message(destination_channel_id_UK, keywords_GoldForex+"\nEdited Message:"+"\n"+str(message_GoldForex.id)+"\n\n"+message_GoldForex.text+"\n\nMessage_is_reply_of:\n"+str(message_GoldForex.reply_to_msg_id)+"\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Edited Message forwarded from GoldForex") await asyncio.sleep(3) replytexts ="None" ref_msg = "None" x=0 last_message_id_GoldForex = max(last_message_id_GoldForex, message_GoldForex.id) # Update the last message ID # Add a delay before checking for new messages again await asyncio.sleep(3) # Adjust the delay time as needed # Function to read credentials from file def read_credentials(): try: with open("credentials.txt", "r") as file: lines = file.readlines() api_id = lines[0].strip() api_hash = lines[1].strip() phone_number = lines[2].strip() return api_id, api_hash, phone_number except FileNotFoundError: print("Credentials file not found.") return None, None, None # Function to write credentials to file def write_credentials(api_id, api_hash, phone_number): with open("credentials.txt", "w") as file: file.write(api_id + "\n") file.write(api_hash + "\n") file.write(phone_number + "\n") async def main(): # Attempt to read credentials from file api_id, api_hash, phone_number = read_credentials() # If credentials not found in file, prompt the user to input them if api_id is None or api_hash is None or phone_number is None: api_id = input("Enter your API ID: ") api_hash = input("Enter your API Hash: ") phone_number = input("Enter your phone number: ") # Write credentials to file for future use write_credentials(api_id, api_hash, phone_number) forwarder = TelegramForwarder(api_id, api_hash, phone_number) destination_channel_id= -1002477328454 keywords = "MyOwnUK: " source_chat_id=-1002466214276 await forwarder.forward_messages_to_channel(source_chat_id, destination_channel_id, keywords) # Start the event loop and run the main function if __name__ == "__main__": asyncio.run(main())