[英]Telegram bot not able to send message after reply keyboard
我是 Telegram 機器人 api 的新手,我正在嘗試用內聯鍵盤制作一個簡單的機器人。 我的機器人工作正常,但在選擇最終內聯鍵盤選項后無法發回消息。 這是我的代碼:
import telegram
from telegram.ext import Updater,CommandHandler,MessageHandler,Filters,CallbackQueryHandler
from telegram import InlineKeyboardButton,InlineKeyboardMarkup,KeyboardButton,ReplyKeyboardMarkup
def start(bot, update):
bot.send_message(chat_id=update.message.chat_id, text='Hi, I am Food Bot')
update.message.reply_text(main_menu_message(),
reply_markup=main_menu_keyboard())
def main_menu(bot, update):
query = update.callback_query
bot.edit_message_text(chat_id=query.message.chat_id,
message_id=query.message.message_id,
text=main_menu_message(),
reply_markup=main_menu_keyboard())
def first_menu(bot, update):
query = update.callback_query
bot.edit_message_text(chat_id=query.message.chat_id,
message_id=query.message.message_id,
text=first_menu_message(),
reply_markup=first_menu_keyboard())
def second_menu(bot, update):
query = update.callback_query
bot.edit_message_text(chat_id=query.message.chat_id,
message_id=query.message.message_id,
text=second_menu_message(),
reply_markup=second_menu_keyboard())
def main_menu_keyboard():
keyboard = [[InlineKeyboardButton('Breakfast', callback_data='op1')],
[InlineKeyboardButton('Lunch or Dinner', callback_data='op2')]]
return InlineKeyboardMarkup(keyboard)
def first_menu_keyboard():
keyboard = [[InlineKeyboardButton('Omelette and 2 Bread slices with coffee',callback_data = 'b1')],
[InlineKeyboardButton('Aloo paratha with curd and tea',callback_data = 'b2')],
[InlineKeyboardButton('Masala dosa with sambar and chutney with coffee',callback_data = 'b3')]]
return InlineKeyboardMarkup(keyboard)
def second_menu_keyboard():
keyboard = [[InlineKeyboardButton('Paneer Makhni with 2 rotis and rice',callback_data = 'l1')],
[InlineKeyboardButton('Pasta in white sauce with 2 pieces of garlic bread',callback_data = 'l2')],
[InlineKeyboardButton('Biryani with raita',callback_data = 'l3')]]
return InlineKeyboardMarkup(keyboard)
def main_menu_message():
return 'What do you want to order?'
def first_menu_message():
return 'Choose the food option:'
def second_menu_message():
return 'Choose the food option:'
def breakfast1(bot,update):
bot.send_message(chat_id=update.message.chat_id,
message_id=update.message.message_id,
text='Your order: Omelette and 2 Bread slices with coffee')
def breakfast2(bot,update):
bot.send_message(chat_id=update.message.chat_id,
message_id=update.message.message_id,
text='Your order: Aloo paratha with curd and tea')
def breakfast3(bot,update):
bot.send_message(chat_id=update.message.chat_id,
message_id=update.message.message_id,
text='Your order: Masala dosa with sambar and chutney with coffee')
def lunchdinner1(bot,update):
bot.send_message(chat_id=update.message.chat_id,
message_id=update.message.message_id,
text='Your order: Paneer Makhni with 2 rotis and rice')
def lunchdinner2(bot,update):
bot.send_message(chat_id=update.message.chat_id,
message_id=update.message.message_id,
text='Your order: Pasta in white sauce with 2 pieces of garlic bread')
def lunchdinner3(bot,update):
bot.send_message(chat_id=update.message.chat_id,
message_id=update.message.message_id,
text='Biryani with raita')
def main():
bot=telegram.Bot(token=TOKEN)
updater = Updater(token=TOKEN)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(main_menu, pattern='main'))
updater.dispatcher.add_handler(CallbackQueryHandler(first_menu, pattern='op1'))
updater.dispatcher.add_handler(CallbackQueryHandler(second_menu, pattern='op2'))
updater.dispatcher.add_handler(CallbackQueryHandler(breakfast1,
pattern='b1'))
updater.dispatcher.add_handler(CallbackQueryHandler(breakfast2,
pattern='b2'))
updater.dispatcher.add_handler(CallbackQueryHandler(breakfast3,
pattern='b3'))
updater.dispatcher.add_handler(CallbackQueryHandler(lunchdinner1,
pattern='l1'))
updater.dispatcher.add_handler(CallbackQueryHandler(lunchdinner2,
pattern='l2'))
updater.dispatcher.add_handler(CallbackQueryHandler(lunchdinner3,
pattern='l3'))
updater.start_polling()
if __name__ == '__main__':
main()
在選擇主菜單的選項之一時,會相應地出現下一個菜單,但是當從第二個菜單中選擇該選項時,即使我為這些選項的回調數據添加了處理程序並且函數必須發送消息,也沒有任何反應。 我究竟做錯了什么?
我建議, 閱讀這段代碼並使用 ConversationHandler 而不是這個。 您只需將用戶返回到特定的 state 並等待命令、消息或回調然后獲取更新並在檢測到用戶操作后執行您的工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.