How forward message to other contact with telethon
Asked Answered
R

1

8

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()
Resultant answered 7/1, 2020 at 9:31 Comment(0)
S
17
@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

Soiree answered 7/1, 2020 at 13:34 Comment(1)
Unfourtenately, if message is medaGroup it splits to multipleNutritive

© 2022 - 2024 — McMap. All rights reserved.