How do I forward a message to another chat as soon as I receive it from a contact? I created this example just to test routing, but it doesn't work.
#!/usr/local/bin/python3
from telethon import TelegramClient, events
api_id = 9999900
api_hash = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
client = TelegramClient('session_name', api_id, api_hash)
client.start()
@client.on(events.NewMessage)
async def main(event):
await client.send_message('Other People', 'Hello!') #Don't work. Keeps waiting forever
with client:
client.run_until_disconnected()
Forwarding messages forward_messages , and can be used like shown below: # If you only have the message IDs client. forward_messages( entity, # to which entity you are forwarding the messages message_ids, # the IDs of the messages (or message) to forward from_entity # who sent the messages? )
Tap and hold the message you want to forward to from the message room. Tap 'Forward'. 'Forward' is displayed only to messages that can be forwarded. Select whether to forward messages one by one or by binding them.
@client.on(events.NewMessage)
async def main(event):
await client.forward_messages(entity, event.message)
This code will work for forwarding new messages.
You can simply put chat_id
of target user in place of entity
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With