skip to Main Content

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

2

Answers


  1. 1- chat.id is integer, you can’t compare it to a string

    async def my_event_handler(event):
        if not event.is_channel:
            return
        chat = await event.get_chat()
        if chat.title == 'Horse Racing Daily Tips Premium':
            await event.forward_to('Greyhounds Daily Tips Premium')
    

    2- using string chat title to fetch or match is not reliable all the time if more than one chat has the same name, so prefer chat identifiers if it’s private channel or usernames if it’s public channel, but if it works for your case keep it.

    Login or Signup to reply.
  2. 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)
     
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search