Search code examples
pythontelethon

With telethon is it possible to forward a message to a group topic?


There is nothing in the documentation about forwarding or sending messages to topic.

I have tried using client.forward_messages but, since there's nothing in the documentation about topics, I was left flailing and guessing what might work. E.g.:

await client.send_message(
    destination__id,
    message,
    reply_to=destination_topic_id,
)
    if (0x10000 <= ord(x) <= 0x10FFFF) else x for x in text
TypeError: ord() expected a character, but string of length 2 found

Solution

  • The friendly client.forward_messages method does not yet expose a way to forward messages to a specific topic. However, you can use ForwardMessagesRequest directly like so:

    from telethon import TelegramClient, functions
    
    client = TelegramClient(...)
    
    client(functions.messages.ForwardMessagesRequest(
        from_peer=source_chat,     # <- chat where the messages exist
        id=[123, 456],             # <- message ids to forward from source_chat
        to_peer=destination_chat,  # <- where to forward the messages
        top_msg_id=topic_id,       # <- topic id
    ))