Search code examples
pythontelegram-botpython-telegram-botpyrogram

I need a forward function code which forward the original message (pyrogram)


@bot.on_message(filters.chat(INPUT_CHANNELS) & filters.text)
def forward_message(client, message: Message):
    text = message.text
    client.send_message(chat_id=DESTINATION_CHANNEL, text=text)

I got this code here, but it just forwards text from the message although there is an image. I need it to forward the original message, keeping all the content such as images, videos, text, etc.


Solution

  • You're grabbing the text of a message and then use send_message to send a new message. If you want to actually forward a message, you'll have to either use client.forward_messages, or the bound method Message.forward(). This will actually forward a message including any media (images, videos, etc).

    @app.on_message(filters.chat(CHANNELS))
    def forward_messages(app, message):
    
        # Either via forward_messages
        app.forward_messages(
            chat_id=TARGET,
            from_chat_id=message.chat.id,
            message_ids=message.id,
        )
    
        # or via bound method:
        message.forward(TARGET)