Search code examples
pythontelethonforwarding

How auto forward messages from a channel to another channel using Python


How do I auto forward messages instantly from a channel to another channel? I created an example but doesn't work.

This is my example:

    `from telethon import TelegramClient, events

    api_id = 99999999
    api_hash = 'xxxxxxxxxxxxxxxxxxxxxxxx'
    client = TelegramClient('session_name', api_id, api_hash)
    client.start()

    @client.on(events.NewMessage)
    async def my_event_handler(event):
        chat = await event.get_chat()
        if chat.id == 'Horse Racing Daily Tips Premium':
            await client.forward_messages('Greyhounds Daily Tips Premium', event.message)

    with client:
        client.run_until_disconnected()`

I tried to send from my source channel "Horse Racing Daily Tips Premium" to my destination channel 'Greyhounds Daily Tips Premium'; I am new of Python and Telegram bot so I am not sure I did the correct way. I already launched the bot from my prompt command line, but doesn't work


Solution

  • you can use event.forward_to() like this:

    @client.on(events.NewMessage())
    async def my_event_handler(event):
        if event.chat_id == 123456:
            await event.forward_to(987654) 
    

    or use client.forward_messages() like this:

    @client.on(events.NewMessage())
    async def my_event_handler(event):
        if event.chat_id == 123456:
            #await client.forward_messages(where you want to forward, messages id, from where)
            await client.forward_messages(98765, event.id, event.chat_id)