Python 电报机器人无法在应答键盘后发送消息

Python 电报机器人无法在应答键盘后发送消息,python,telegram,chatbot,telegram-bot,python-telegram-bot,Python,Telegram,Chatbot,Telegram Bot,Python Telegram Bot,我是电报机器人api的新手,正在尝试制作一个带有内联键盘的简单机器人。我的机器人工作正常,但在选择最终的内联键盘选项后无法发回消息。这是我的密码: import telegram from telegram.ext import Updater,CommandHandler,MessageHandler,Filters,CallbackQueryHandler from telegram import InlineKeyboardButton,InlineKeyboardMarkup,Keybo

我是电报机器人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而不是此。您只需将用户返回到特定状态,等待命令、消息或回调,然后获取更新,并在检测到用户操作后执行您的工作

  • 当你初始化一个更新程序时,你不必也初始化一个机器人。更新程序对象传递两个bot,在获得用户输入时更新到您的函数
我建议,使用此代码并使用ConversationHandler代替此代码。您只需将用户返回到特定状态,等待命令、消息或回调,然后获取更新,并在检测到用户操作后执行您的工作

  • 当你初始化一个更新程序时,你不必也初始化一个机器人。更新程序对象传递两个bot,在获得用户输入时更新到您的函数

如何在代码中使用错误处理程序以及bot和update参数?代码使用
use\u context
,它通过
update
context
,但是我也需要
bot
参数。我更喜欢使用不需要使用\u context的11.1.0版,这里更新是update,context是代替bot实例的。context参数的方法与bot相同吗?文档显示了不同的方法。我需要不在
上下文中的
bot.edit\u message\u text
,如何在代码中使用错误处理程序以及bot和更新参数?代码使用
use\u context
,它通过
update
context
,但是我也需要
bot
参数。我更喜欢使用不需要使用\u context的11.1.0版,这里更新是update,context是代替bot实例的。context参数的方法与bot相同吗?文档显示了不同的方法。我需要
bot.edit\u message\u text
,它不在
context