4

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?

Alex
  • 5,728
  • 5
  • 20
  • 20
  • 1
    I think you don't need try receive `inline_message_id`. From inline keyboard callback you receive `message_id`. Can you list your update.callback_query? I think `update.callback_query.message.message_id` will be there – Dmitry Aug 24 '16 at 15:16
  • @Dmitry Yes, `message_id` there but I need `chat_id` too for non inline messages. – Alex Aug 24 '16 at 16:18

2 Answers2

9

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"
)
Nazar
  • 1,769
  • 1
  • 15
  • 31
  • Thanks, message_id is OK, but error sad that `Bad Request: Wrong inline message identifier specified` when I tried to use `message_id` instead of `inline_message_id` I have an error about empty `chat_id`. How can determine `chat_id`? I'm using bot not in inline mode. – Alex Aug 24 '16 at 16:11
  • @Alex btw [RTFM](https://core.telegram.org/bots/api#available-types) :) Refer to [callbackquery](https://core.telegram.org/bots/api#callbackquery) and [message](https://core.telegram.org/bots/api#message), all the info is there. – Nazar Aug 24 '16 at 17:40
  • Yes, sorry, I'm new to OOP in python and didn't know how I can find available types. I tried to found it in library documentation and with python dir(). Now it's clear thank you. – Alex Aug 24 '16 at 18:21
0

You can always do this

    try:
        query.edit_message_reply_markup(reply_markup=InlineKeyboardMarkup(reply_keyboard))
    except:
        return
Henadzi Rabkin
  • 6,834
  • 3
  • 32
  • 39