skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. 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 
    botn'
                              '/help - get all 
    available commandsn')
    
    # 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search