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: ')) p1_edit="Start1" p1_edit_Alex="Start2" p1_edit_GoldPro="Start3" p1_edit_GoldKiller="Start4" p1_edit_US="Start5" ref_msg = "None" x=0 destination_channel_id_US= -1002406030938 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_US = "MyOwnUS: " source_chat_id_US=-4787530036 last_message_id = (await self.client.get_messages(source_chat_id, 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_US = (await self.client.get_messages(source_chat_id_US, limit=1))[0].id while True: print("Checking for messages and forwarding them...") # Get new messages since the last checked message messages = await self.client.get_messages(source_chat_id, min_id=last_message_id, limit=None) messagesp1 = await self.client.get_messages(source_chat_id, min_id=last_message_id-1, limit=None) messages_Alex = await self.client.get_messages(source_chat_id_Alex, min_id=last_message_id_Alex, limit=None) messages_Alexp1 = 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, limit=None) messages_GoldProp1 = 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, limit=None) messages_GoldKillerp1 = await self.client.get_messages(source_chat_id_GoldKiller, min_id=last_message_id_GoldKiller-1, limit=None) messages_US = await self.client.get_messages(source_chat_id_US, min_id=last_message_id_US, limit=None) messages_USp1 = await self.client.get_messages(source_chat_id_US, min_id=last_message_id_US-1, limit=None) for message in reversed(messages): print(f"Message contains: {message.text}") # Forward the message to the destination channel if message.reply_to_msg_id: replytexts = await self.client.get_messages(source_chat_id, min_id=message.reply_to_msg_id-1, limit=None) x = len(replytexts) if (x>0): ref_msg=replytexts[x-1].text await self.client.send_message(destination_channel_id, keywords+"\n"+str(last_message_id)+"\n\n"+message.text+"\n\nMessage_is_reply_of:\n"+str(message.reply_to_msg_id)+"\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Message forwarded from UK") ref_msg = "None" last_message_id = max(last_message_id, message.id) # Update the last message ID for messagep1 in reversed(messagesp1): # Check if the message text includes any of the keywords if messagep1.edit_date!=None and messagep1.edit_date!=p1_edit: print(f"Edited Message contains: {messagep1.text}") # Forward the message to the destination channel if messagep1.reply_to_msg_id: replytexts = await self.client.get_messages(source_chat_id, min_id=messagep1.reply_to_msg_id-1, limit=None) x = len(replytexts) if (x>0): ref_msg=replytexts[x-1].text await self.client.send_message(destination_channel_id, keywords+"\nEdited Message:"+"\n"+str(last_message_id)+"\n\n"+messagep1.text+"\n\nMessage_is_reply_of:\n"+str(messagep1.reply_to_msg_id)+"\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Edited Message forwarded from UK") ref_msg = "None" p1_edit=messagep1.edit_date for message_Alex in reversed(messages_Alex): print(f"Message contains: {message_Alex.text}") # Forward the message to the destination channel if message_Alex.reply_to_msg_id: replytexts_Alex = await self.client.get_messages(source_chat_id_Alex, min_id=message_Alex.reply_to_msg_id-1, limit=None) x = len(replytexts_Alex) if (x>0): ref_msg=replytexts_Alex[x-1].text await self.client.send_message(destination_channel_id, keywords_Alex+"\n"+str(last_message_id_Alex)+"\n\n"+message_Alex.text+"\n\nMessage_is_reply_of:\n"+str(message_Alex.reply_to_msg_id)+"\n\n"+ref_msg) await self.client.send_message(destination_channel_id_US, keywords_Alex+"\n"+str(last_message_id_Alex)+"\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") ref_msg = "None" last_message_id_Alex = max(last_message_id_Alex, message_Alex.id) # Update the last message ID for message_Alexp1 in reversed(messages_Alexp1): # Check if the message text includes any of the keywords if message_Alexp1.edit_date!=None and message_Alexp1.edit_date!=p1_edit_Alex: print(f"Edited Message contains: {message_Alexp1.text}") # Forward the message to the destination channel if message_Alexp1.reply_to_msg_id: replytexts_Alexp1 = await self.client.get_messages(source_chat_id_Alex, min_id=message_Alexp1.reply_to_msg_id, limit=None) x = len(replytexts_Alexp1) if (x>0): ref_msg=replytexts_Alexp1[x-1].text await self.client.send_message(destination_channel_id, keywords_Alex+"\nEdited Message:"+"\n"+str(last_message_id_Alex)+"\n\n"+message_Alexp1.text+"\n\nMessage_is_reply_of:\n"+str(x)+"\n"+str(message_Alexp1.reply_to_msg_id)+"\n\n"+ref_msg) await self.client.send_message(destination_channel_id_US, keywords_Alex+"\nEdited Message:"+"\n"+str(last_message_id_Alex)+"\n\n"+message_Alexp1.text+"\n\nMessage_is_reply_of:\n"+str(message_Alexp1.reply_to_msg_id)+"\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Edited Message forwarded from Alex") ref_msg = "None" p1_edit_Alex = message_Alexp1.edit_date for message_GoldPro in reversed(messages_GoldPro): print(f"Message contains: {message_GoldPro.text}") # Forward the message to the destination channel 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 await self.client.send_message(destination_channel_id, keywords_GoldPro+"\n"+str(last_message_id_GoldPro)+"\n\n"+message_GoldPro.text+"\n\nMessage_is_reply_of:\n"+str(message_GoldPro.reply_to_msg_id)+"\n\n"+ref_msg) await self.client.send_message(destination_channel_id_US, keywords_GoldPro+"\n"+str(last_message_id_GoldPro)+"\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 Gold Pro") ref_msg = "None" last_message_id_GoldPro = max(last_message_id_GoldPro, message_GoldPro.id) # Update the last message ID for message_GoldProp1 in reversed(messages_GoldProp1): # Check if the message text includes any of the keywords if message_GoldProp1.edit_date!=None and message_GoldProp1.edit_date!=p1_edit_GoldPro: print(f"Edited Message contains: {message_GoldProp1.text}") # Forward the message to the destination channel if message_GoldProp1.reply_to_msg_id: replytexts = await self.client.get_messages(source_chat_id_GoldPro, min_id=message_GoldProp1.reply_to_msg_id-1, limit=None) x = len(replytexts) if (x>0): ref_msg=replytexts[x-1].text await self.client.send_message(destination_channel_id, keywords_GoldPro+"\nEdited Message:"+"\n"+str(last_message_id_GoldPro)+"\n\n"+message_GoldProp1.text+"\n\nMessage_is_reply_of:\n"+str(message_GoldProp1.reply_to_msg_id)+"\n\n"+ref_msg) await self.client.send_message(destination_channel_id_US, keywords_GoldPro+"\nEdited Message:"+"\n"+str(last_message_id_GoldPro)+"\n\n"+message_GoldProp1.text+"\n\nMessage_is_reply_of:\n"+str(message_GoldProp1.reply_to_msg_id)+"\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Edited Message forwarded from Gold Pro") ref_msg = "None" p1_edit_GoldPro = message_GoldProp1.edit_date for message_GoldKiller in reversed(messages_GoldKiller): print(f"Message contains: {message_GoldKiller.text}") # Forward the message to the destination channel 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 await self.client.send_message(destination_channel_id, keywords_GoldKiller+"\n"+str(last_message_id_GoldKiller)+"\n\n"+message_GoldKiller.text+"\n\nMessage_is_reply_of:\n"+str(message_GoldKiller.reply_to_msg_id)+"\n\n"+ref_msg) await self.client.send_message(destination_channel_id_US, keywords_GoldKiller+"\n"+str(last_message_id_GoldKiller)+"\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 Gold Killer") ref_msg = "None" last_message_id_GoldKiller = max(last_message_id_GoldKiller, message_GoldKiller.id) # Update the last message ID for message_GoldKillerp1 in reversed(messages_GoldKillerp1): # Check if the message text includes any of the keywords if message_GoldKillerp1.edit_date!=None and message_GoldKillerp1.edit_date!=p1_edit_GoldKiller: print(f"Edited Message contains: {message_GoldKillerp1.text}") # Forward the message to the destination channel if message_GoldKillerp1.reply_to_msg_id: replytexts = await self.client.get_messages(source_chat_id_GoldKiller, min_id=message_GoldKillerp1.reply_to_msg_id-1, limit=None) x = len(replytexts) if (x>0): ref_msg=replytexts[x-1].text await self.client.send_message(destination_channel_id, keywords_GoldKiller+"\nEdited Message:"+"\n"+str(last_message_id_GoldKiller)+"\n\n"+message_GoldKillerp1.text+"\n\nMessage_is_reply_of:\n"+str(message_GoldKillerp1.reply_to_msg_id)+"\n\n"+ref_msg) await self.client.send_message(destination_channel_id_US, keywords_GoldKiller+"\nEdited Message:"+"\n"+str(last_message_id_GoldKiller)+"\n\n"+message_GoldKillerp1.text+"\n\nMessage_is_reply_of:\n"+str(message_GoldKillerp1.reply_to_msg_id)+"\n\n"+ref_msg) await asyncio.sleep(5) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Edited Message forwarded from Gold Killer") ref_msg = "None" p1_edit_GoldKiller = message_GoldKillerp1.edit_date for message_US in reversed(messages_US): print(f"Message contains: {message_US.text}") # Forward the message to the destination channel if message_US.reply_to_msg_id: replytexts = await self.client.get_messages(source_chat_id_US, min_id=message_US.reply_to_msg_id-1, limit=None) x = len(replytexts) if (x>0): ref_msg=replytexts[x-1].text await self.client.send_message(destination_channel_id_US, keywords_US+"\n"+str(last_message_id_US)+"\n\n"+message_US.text+"\n\nMessage_is_reply_of:\n"+str(message_US.reply_to_msg_id)+"\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Message forwarded from US") ref_msg = "None" last_message_id_US = max(last_message_id_US, message_US.id) # Update the last message ID for message_USp1 in reversed(messages_USp1): # Check if the message text includes any of the keywords if message_USp1.edit_date!=None and message_USp1.edit_date!=p1_edit_US: print(f"Edited Message contains: {message_USp1.text}") # Forward the message to the destination channel if message_USp1.reply_to_msg_id: replytexts = await self.client.get_messages(source_chat_id_US, min_id=message_USp1.reply_to_msg_id-1, limit=None) x = len(replytexts) if (x>0): ref_msg=replytexts[x-1].text await self.client.send_message(destination_channel_id_US, keywords_US+"\nEdited Message:"+"\n"+str(last_message_id_US)+"\n\n"+message_USp1.text+"\n\nMessage_is_reply_of:\n"+str(message_USp1.reply_to_msg_id)+"\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Edited Message forwarded from US") ref_msg = "None" p1_edit_US = message_USp1.edit_date # Add a delay before checking for new messages again await asyncio.sleep(1) # 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=-1002306133101 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())