我想與內嵌鍵盤聊天更新消息,但無法理解如何接收inline_message_id或者如果它僅適用於嵌入式查詢我怎麼能確定chat_id和MESSAGE_ID在editMessageText(*args, **kwargs)上使用telegram.bot.Bot?使用回調內嵌鍵盤更新信息查詢
我的代碼示例(的一部分):
#!/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()
當我運行它,我有一個錯誤:
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'"
我查無功update.callback_query.inline_message_id,它是空的。當我嘗試bot.editMessageText與硬編碼的變裝chat_id和message_id它運作良好。
我需要保存在DB(對所有用戶)瓦爾chat_id和MESSAGE_ID當它們運行命令/光,然後,當他們按內嵌按鈕,我需要從數據庫讀取這個值還是可以用一些簡單的方法用於編輯消息?
我覺得你並不需要嘗試接受'inline_message_id'。 從內聯鍵盤迴調中收到'message_id'。 你能列出你的update.callback_query嗎?我認爲'update.callback_query.message.message_id'將在那裏 – Dmitry
@Dmitry是的,'message_id'那裏,但我也需要'chat_id'非內聯消息。 – Alex