Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How forward message to other contact with telethon

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()
like image 463
Rafael Rodrigues Pereira Avatar asked Jan 07 '20 09:01

Rafael Rodrigues Pereira


People also ask

How do I forward telethon messages?

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? )

How do I forward a line message?

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.


1 Answers

@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

like image 175
Gagan T K Avatar answered Oct 17 '22 21:10

Gagan T K