skip to Main Content

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.

2

Answers


  1. 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)

    Login or Signup to reply.
  2. How in Google Script write?
    
    function sendReminderToTelegram() {
      var telegramBotToken = "____";
      var telegramChatId = "-100CHAT_ID";
      
      // ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ
      var currentTime = new Date();
      var targetTime = new Date();
      targetTime.setHours(8); // Установка ΠΆΠ΅Π»Π°Π΅ΠΌΠΎΠ³ΠΎ часа
      targetTime.setMinutes(0); // Установка ΠΆΠ΅Π»Π°Π΅ΠΌΡ‹Ρ… ΠΌΠΈΠ½ΡƒΡ‚
    
       
        // Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ сообщСния
        var message = "ΠŸΡ€ΠΎΡˆΡƒ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΈΡ‚ΡŒ ΠΏΡ€ΠΈΠ½Ρ‚Π΅Ρ€Ρ‹ ΠΈ Π·Π°ΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ ΠΈΡ….";
        
        // ΠžΡ‚ΠΏΡ€Π°Π²ΠΊΠ° сообщСния Π² Telegram
        var telegramUrl = "https://api.telegram.org/bot" + telegramBotToken + "/sendMessage";
        var payload = {
          "chat_id": telegramChatId,
          "text": message,
        };
        
        var options = {
          "method" : "post",
          "payload" : payload
        };
        
        UrlFetchApp.fetch(telegramUrl, options);
      //}
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search