Search code examples
pythontelegram-botpy-telegram-bot-api

How to get message id of a message through telegram python api


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?


Solution

  • 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