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('session1_' + 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}1.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: ')) last_message_id = (await self.client.get_messages(source_chat_id, limit=1))[0].id p1_edit="Start1" destination_channel_id2= -1002406030938 ref_msg = "None" while True: print("Checking and forwarding meesage for "+keywords+".......") # 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) for message in reversed(messages): if (message.text!=""): 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) ref_msg=replytexts[x-1].text await self.client.send_message(destination_channel_id, keywords+"\n\n"+message.text+"\n\nMessage_is_reply_of:\n\n"+ref_msg) await self.client.send_message(destination_channel_id2, keywords+"\n\n"+message.text+"\n\nMessage_is_reply_of:\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("\nMessage forwarded") # Update the last message ID ref_msg = "None" last_message_id = max(last_message_id, message.id) for messagep1 in reversed(messagesp1): if (messagep1.edit_date!=None) and (messagep1.edit_date!=p1_edit) and (messagep1.text!=""): print(f"Edited Message contains: {messagep1.text}") 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) ref_msg=replytexts[x-1].text # Forward the message to the destination channel await self.client.send_message(destination_channel_id, keywords+"\n"+messagep1.text+"\n\nMessage_is_reply_of:\n\n"+ref_msg) await self.client.send_message(destination_channel_id2, keywords+"\n"+messagep1.text+"\n\nMessage_is_reply_of:\n\n"+ref_msg) print("\n"+f"Reply of Message: {ref_msg}"+"\n") print("Edited Message forwarded") ref_msg = "None" p1_edit=messagep1.edit_date # Add a delay before checking for new messages again await asyncio.sleep(2) # 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 = "My Own UK/US: " 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())