Alex Alex - 3 months ago 80
Python Question

Updating messages with inline keyboards using callback queries

I want to update message in chat with inline keyboard but can't understand how to receive a inline_message_id or if it only for inline queries how I can determine chat_id and message_id for using it on editMessageText(*args, **kwargs) in class telegram.bot.Bot?

my code example (part of it):

#!/usr/bin/python
import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, InlineQueryHandler, CallbackQueryHandler

tokenid = "YOUR_TOKEN_ID"

def inl(bot, update):
if update.callback_query.data == "k_light_on":
#func for turn on light res = k_light.on()
bot.answerCallbackQuery(callback_query_id=update.callback_query.id, text="Turning on light ON!")
bot.editMessageText(inline_message_id=update.callback_query.inline_message_id, text="Do you want to turn On or Off light? Light is ON")
#hardcoded vars variant
#bot.editMessageText(message_id=298, chat_id=174554240, text="Do you want to turn On or Off light? Light is ON")
elif update.callback_query.data == "k_light_off":
#func for turn on light res = k_light.off()
bot.answerCallbackQuery(callback_query_id=update.callback_query.id, text="Turning off light OFF!")
bot.editMessageText(inline_message_id=update.callback_query.inline_message_id, text="Do you want to turn On or Off light? Light is ON")
#hardcoded vars variant
#bot.editMessageText(message_id=298, chat_id=174554240, text="Do you want to turn On or Off light? Light is OFF")
else:
print "Err"

def k_light_h(bot, update):
reply_markup = telegram.InlineKeyboardMarkup([[telegram.InlineKeyboardButton("On", callback_data="k_light_on"), telegram.InlineKeyboardButton("Off", callback_data="k_light_off")]])
ddd = bot.sendMessage(chat_id=update.message.chat_id, text="Do you want to turn On or Off light?", reply_markup=reply_markup)

if __name__ == "__main__":
#
updater = Updater(token=tokenid)
### Handler groups
dispatcher = updater.dispatcher
# light
k_light_handler = CommandHandler('light', k_light_h)
dispatcher.add_handler(k_light_handler)
# errors
updater.dispatcher.add_error_handler(error)
updater.start_polling()
# Run the bot until the user presses Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT
updater.idle()


When I run it I have an error:

telegram.ext.dispatcher - WARNING - A TelegramError was raised while processing the Update.
root - WARNING - Update ...
... caused error "u'Bad Request: message identifier is not specified'"


I checked var update.callback_query.inline_message_id and it was empty. When I tried bot.editMessageText with hardcoded vars chat_id and message_id it worked well.

Do I need save in DB (for all users) vars chat_id and message_id when they run command /light and then when they press inline button I need read from DB this value or I can use some simpler method for editing messages?

Answer

You should pass message_id instead of inline_message_id to editMessageText.

So the solution for you is this:

bot.editMessageText(
    message_id = update.callback_query.message.message_id,
    chat_id = update.callback_query.message.chat.id,
    text = "Do you want to turn On or Off light? Light is ON"
)
Comments