Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 384
ramin kazemi Avatar asked Mar 15 '26 14:03

ramin kazemi


2 Answers

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()
like image 56
Andre0512 Avatar answered Mar 17 '26 02:03

Andre0512


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.

like image 22
Batichico Avatar answered Mar 17 '26 03:03

Batichico



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!