Getting input from users in Bot

11,077

Solution 1

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()

Solution 2

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.

Share:
11,077
ramin kazemi
Author by

ramin kazemi

Updated on June 22, 2022

Comments

  • ramin kazemi
    ramin kazemi almost 2 years

    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?

  • curiousguy
    curiousguy over 2 years
    Can I know how to save the information in JSON?