Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send telegram message to a topic thread?

Recently Telegram added support for Topics in Groups in Bot API version 6.3 and this support added into python-telegram-bot version 13.15 (please find changelog https://docs.python-telegram-bot.org/en/stable/changelog.html)

Im sending messages like this:

{
  "@type": "sendMessage",
  "chat_id": "-1001580394181",
  "input_message_content": {
      "@type": "inputMessageText","text": {
          "@type": "formattedText","text": "test", "entities": [] 
       }
  }
}

and it works. They added message_thread_id parameter to define to which topic message should be sent. So i added the parameter:

{
  "@type": "sendMessage",
  "chat_id": "-1001580394181",
  "message_thread_id": "20451426304",
  "input_message_content": {
      "@type": "inputMessageText","text": {
          "@type": "formattedText","text": "test", "entities": [] 
       }
}

but message goes to main topic, there's no error or anything unless i'll provide incorrect message_thread_id, if so i'm getting message: Code: 400, message: Invalid message thread ID specified

What i'm doing wrong? The id's are correct i got them from catching the response from sending test message to topic channel.

like image 638
Navi Avatar asked Jan 29 '26 03:01

Navi


1 Answers

To send messages to Telegram Forum Topic you need to reply to a message saying

💬 "Topic" was created

So basically every message you send in telegram topic is just a reply to this original message. You can check this if you view your Forum as Messages.

I, for example, have deleted that message so I:

  • added my bot to the Forum
  • created new topic
  • opened link https://api.telegram.org/bot{token}/getUpdates
  • looked for message_id of topic created message

Here is how my message looked like:

{
    "ok": true,
    "result":
    [
        {
            "update_id": 1234567890,
            "message":
            {
                "message_id": 12345,
                "from":
                {
                    ...
                },
                "chat":
                {
                    "id": -12345678,
                    "title": "bruh",
                    "is_forum": true,
                    "type": "supergroup"
                },
                "date": 0987654321,
                "message_thread_id": 12345,
                "forum_topic_created":
                {
                    ...
                },
                "is_topic_message": true
            }
        }
    ]
}

So you can take either message_id or message_thread_id. In my case they were the same but I am not sure if there is a difference.

And now paste this message_id as a reply_to_message_id parameter in bot.send_message() method.

bot.send_message(-12345678, 'Message for topic', reply_to_message_id=12345)

like image 101
redwideweb Avatar answered Jan 31 '26 22:01

redwideweb