无法从用户接收更新消息

时间:2017-11-19 18:10:15

标签: python telegram-bot python-telegram-bot

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

之后
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()

1 个答案:

答案 0 :(得分:2)

因为您使用的是CommandHandler,它仅用于一次捕获一个命令。

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