2

I'm looking for a way for a bot to wait for a reply from a user after a command. For example, you first type "/ask", then the bot waits for a plain message (not a command) from the user and after the user replies is stores his/her reply in a variable

I'm sure this is quite simple, but all the tutorials I've seen are in Russian and the documentation for python-telegram-api is very chaotic and I'm not the most advanced If I'm dumb, sorry, just please help a fellow beginner out

1
  • Hi, I wonder if you ever found a way to await for the user input. It seems to be an impossible task with asyncio and python-telegram-bot Commented Nov 21, 2022 at 22:18

2 Answers 2

1

Okay, this was pointless. I thought you couldn't use arguments, but the post I read was 5 years old so... I'm stupid. I just used arguments instead, thanks for the help tho, really appreciate it

Sign up to request clarification or add additional context in comments.

Comments

0

here's the code that takes user's input and stores it in the "store_user_input" variable.

import logging
from telegram.ext import Updater, 
CommandHandler, MessageHandler, Filters, 
CallbackContext
from telegram import Update

logging.basicConfig(
format='%(asctime)s - %(name)s - % 
(levelname)s - %(message)s', 
level=logging.INFO
)
logger = logging.getLogger(__name__)

# function to handle the /start command
def startcommand(update:Update, context: 
CallbackContext) -> None:
first_name = update.message.chat.first_name
update.message.reply_text\
        (
        f'Welcome to the bot, {first_name}, 
what are you interested in?'
        )

# function to handle and store user input
def text(update:Update, context: 
CallbackContext) -> None:
text_received = update.message.text
store_user_input = text_received

# function to handle the /help command
def helpcommand(update:Update, context: 
CallbackContext) -> None:
update.message.reply_text('Here is a list of 
all available commands:\n '
                          '/start - start the 
bot\n'
                          '/help - get all 
available commands\n')

# function to handle errors occured in the 
dispatcher
def errormsg(update:Update, context: 
CallbackContext) -> None:
update.message.reply_text('An error 
occured.')

# main function
def main():
# "bot_data is your .txt file with bot API token"
fo = open("bot_data")
fr = fo.read()
updater = Updater(fr)
dispatcher = updater.dispatcher

# add handlers for start and help commands

dispatcher.add_handler(CommandHandler("start", startcommand))
dispatcher.add_handler(CommandHandler("help", helpcommand))

# add an handler for normal text (not commands)
dispatcher.add_handler(MessageHandler(Filters.text, text))

# add an handler for errors
dispatcher.add_error_handler(errormsg)

# start bot
updater.start_polling()

# run the bot
updater.idle()


if __name__ == '__main__':
    main()

You can check the value by adding:

print("store_user_input")

after store_user_input line.

1 Comment

How do I make it wait for the input? Now it automatically executes code after it, which is annoying

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.