skip to Main Content

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?

2

Answers


  1. I recommend pyTelegramBotAPI, not python-telegram-bot.
    For me it is easier.

    And you should use this:

    @bot.message_handler(func=lambda message: True)
    def echo_message(message):
      cid = message.chat.id
      mid = message.message_id 
      message_text = message.text 
      user_id = message.from_user.id 
      user_name = message.from_user.first_name 
    

    You can save this information in JSON if you want.

    Login or Signup to reply.
  2. 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()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search