Search code examples
pythonpython-3.xtelegramtelegram-botpython-telegram-bot

Getting input from users in Bot


I have a question about building a telegram bot with python. How can I get input from the user in my python-telegram-bot?

For example, a dictionary bot, how can I get a word from the user?


Solution

  • I recommend to have a look at the examples on GitHub.

    With python-telegram-bot v12.0.0b1 you can do it like this:

    def do_something(user_input):
        answer = "You have wrote me " + user_input
        return answer
    
    def reply(update, context):
        user_input = update.message.text
        update.message.reply_text(do_something(user_input))
    
    def main():
        updater = Updater("TOKEN", use_context=True)
        dp = updater.dispatcher
        dp.add_handler(MessageHandler(Filters.text, reply))
        updater.start_polling()
        updater.idle()