Constant
Constant

Reputation: 604

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?

Upvotes: 0

Views: 3954

Answers (2)

0stone0
0stone0

Reputation: 43884

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

Upvotes: 1

CallMeStag
CallMeStag

Reputation: 7020

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'.

Upvotes: 0

Related Questions