我正在尝试创建一个机器人,该机器人接收用户的响应并再次询问是否需要。问题在于:

update.reply_text("Did you report all you working hour on freshdesk for this week?, ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))


我无法获得新的更新。消息文本在第一个/start中保留为print,而第二个print则完全不起作用。

如何正确获得用户的响应?可能是与ReplyMarkup有关的问题吗?

def check_the_week(bot, update):
    agent_username = update.message.from_user['username']
    parameters = {"username": agent_username}
    url = "{}/weekly_hours/".format(API_URL)
    report = get_request_forwarder(url=url, method="GET", parameters=parameters)["messages"]
    reply_keyboard = [['YES', 'NO']]
    bot.send_message(
       chat_id=update.message.chat_id,
       text=report,
       reply_markup=ReplyKeyboardMarkup(reply_keyboard,  one_time_keyboard=True))  # sends the total nr of hours
    print update.message.text
    update.reply_text("Did you report all you working hour on freshdesk for this week?",
                  ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))
    print update.message.text
    if update.message.text == "YES":
        update.message.reply_text(text="Are you sure?",                            reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))

    # Asks confirmation
        if update.message.text == "YES":
            update.message.reply_text(text="Thank you for reporting your working hours in time!")

        elif update.message.text == "NO":
            update.message.reply_text(text="Please, check you time reports and add missing")

    elif update.message.text == "NO":
        update.message.reply_text(text="Please, check you time reports and add missing")


def main():
    # Create the EventHandler and pass it your bot's token.
    updater = Updater(TELEGRAM_TOKEN)
    j = updater.job_queue
    # # Get the dispatcher to register handlers
    dp = updater.dispatcher
    # # Start the Bot

    dp.add_handler(CommandHandler("start", check_the_week))
    # Send information to manager by command

    updater.start_polling()
    updater.idle()
    print("bot started")

if __name__ == '__main__':
    main()

最佳答案

因为您使用的是CommandHandler,所以只能一次捕获一个命令。

您可以通过使用ConversationHandler来实现。请阅读示例脚本herehere。您还可以阅读有关处理程序here的更多详细信息。

08-17 17:41