Search code examples
pythontelegram-botpython-telegram-bot

Can't receive update messages from user


I am trying to create a bot that receives a response from the user and asks again if needed. The problem is that after:

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

I can't get the new updates. The message text remains /start in the first print, and the second print doesn't work at all.

How can I get correctly the response from the user? Can it be an issue related to 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()

Solution

  • Because you are using a CommandHandler, which is only used to capture a single command at a time.

    What you want to do can be achieved by using a ConversationHandler. Please read the example scripts here and here. Also you can read more details on the handler here.