Search code examples
pythontelegramtelegram-botpython-telegram-botpy-telegram-bot-api

Getting Error: "TypeError: object Message can't be used in 'await' expression" when trying to wait and get user input on Telegram bot


On telegram the bot is showing me the "reply keyboard" but nothing further happens as the below error gets raised

async def start (update: Update, context: CallbackContext):
    reply_keyboard = [["A", "B", "C"]]
    await update.message.reply_text(
        "Select any one",
        reply_markup = ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
    )
      
def handle_inputs (update: Update, context: CallbackContext):
    comd = update.message.text
    if (comd == '/start'): 
        asyncio.run(start(update, context))
    else:
        update.message.reply_text("Sorry '%s' is not a valid command" % update.message.text)
        
def main():
    updater = Updater(BOT_TOKEN, use_context=True)
    dispatcher = updater.dispatcher
    
    updater.dispatcher.add_handler(MessageHandler(Filters.text, handle_inputs))
    
    updater.start_polling()

if __name__ == '__main__':
    main()

I am new to Telegram bot and have less knowledge for Async Functions. Any help will be greatly appreciated! Thank you!

I tried to understand more about co-routines but and not quite sure how to implement it here. I am expecting couple of telegram features to implement like:

  • wait for user to input something
  • reply keyboard, inline keyboard, etc.

Solution

  • Please note that python-telegram-bot is not supporting asyncio natively for versions v13.x and earlier. python-telegram-bot is based on asyncio since version v20, which is currently in pre-release mode. You can check which version you have installed by running python -m telegram.

    Please also not that await -ing reply_text has nothing to do with waiting for user input. The await keyword in Python is purely related to the asyncio module.

    If you want to have a conversation-like chat flow with the user, please see this question.


    Disclaimer: I'm currently the maintainer of python-telegram-bot.