skip to Main Content

Ok, I know this is a newbie question but I’ve been stuck for a while now.

I just started using pythonTelegarmapi but I have no idea how to get message_id of a message. How do i do it?

import os
import telebot

API_KEY =  "API_KEY"

# Getting the bot instance
bot = telebot.TeleBot(API_KEY)

# Forward mesasage
# From https://core.telegram.org5/bots/api#forwardmessage
@bot.message_handler(commands=['forward'])
def forward(message):
    bot.forward_message('@send_to','@from', 1, False)
                                           ^^^
#                                          Here is supposed to be the message_id but I don't know how to get that. 

"""

So, how to I retrieve the ID of a particular message in a chat/channel using Python Telegram Bot Api?

2

Answers


  1. It’s in the message parameter that your function is receiving. message.message_id should do the trick, see also https://github.com/eternnoir/pyTelegramBotAPI#types. Note that instead of '@from' you should probably rather pass message.chat.id because IISC your code snippet doesn’t guarantee that message will be a message sent in the chat identified by the username '@from'.

    Login or Signup to reply.
  2. You’ll need:

    bot.forward_message(mydebugid, message.chat.id, message.message_id, False)
    

    Where mydebugid is the ID you’re forwarding to.


    import os
    import telebot
    
    API_KEY = "--"
    
    # Getting the bot instance
    bot = telebot.TeleBot(API_KEY)
    
    mydebugid = 123456
    
    bot.send_message(mydebugid, "Wake")
    
    # Forward mesasage
    @bot.message_handler(commands=['forward'])
    def forward(message):
        bot.forward_message(mydebugid, message.chat.id, message.message_id, False)
    
    
    bot.polling()
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search