python - 无法接收用户的更新消息

标签 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))

我无法获取新的更新。消息文本在第一个 print 中保留为 /start,而第二个 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 的更多详细信息。 .

关于python - 无法接收用户的更新消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47380254/

相关文章:

python-3.x - 电报机器人 : how to ask for user input on button click

telegram - Telegram user_id 的长度可以是多少?

python - 如何将 Telegram 聊天机器人中的内联键盘设置为 python-telegram-bot 中的其他功能?

多输入法上的Python映射方法

python - 何时使用 matplotlib.pyplot 类以及何时使用绘图对象 (matplotlib.collections.PathCollection)

python - 如何在 python 中获取数学函数作为输出

python - 如何通过 python-telegram-bot 发送照片

python - 得到一个似乎没有多大意义的错误。

go - 如何使用 Telegram bot API 查找消息的文件唯一 ID?

python - 如何在 python telegram bot 中发送消息之间添加暂停?