-1

I'm new in StackOverflow. I'm currently making a telegram bot with the python-telegram-bot library. Unfortunately I'm having a problem that I tried so many times to solve, with no results. I'm wondering how I can make the bot sends a message, waits for an user response and answers it correctly.

My code

def request_help_function(update: Update, context: CallbackContext):

    bot.send_message(

        chat_id=update.effective_chat.id,
        text="You are sending a help request to an admin. Type 'Cancel' if you don't want to do it."
    )
    user_response = update.message.text
    if user_esponse == "Cancel":
        message = "Ok, I cancelled the message"
    else:
        message = "Ok, I sent the message to an admin"
    bot.send_message(
        chat_id=update.effective_chat.id,
        text=message
    )
    
reqhelp_value = CommandHandler("reqhelp", request_help_function)
dispatcher.add_handler(reqhelp_value)

The problem here is that the bot doesn't wait for the user response and it jumps directly to the else statement. Do you know how could I solve it?

  • Does this answer your question? [Telegram bot to wait for user reply](https://stackoverflow.com/questions/44639923/telegram-bot-to-wait-for-user-reply) – 0stone0 Sep 13 '21 at 13:15
  • I've already took a look at it and it doesn't help. By the way thank you. – Christian Cristini Sep 13 '21 at 13:17
  • Does this answer your question? [How to get several inputs in one command in telegram bot api?](https://stackoverflow.com/questions/68142791/how-to-get-several-inputs-in-one-command-in-telegram-bot-api) – CallMeStag Sep 13 '21 at 13:18

1 Answers1

3

I recommend you not to try handel multiple tasks in one single function , follow this steps instead :

1 : firstly , you should make a conversation handler , where user enters the conversation using a command and then step by step communicates with the bot . this is quite useful in tasks like users registering (enter name ? [Behrad] | thanks , enter age ? [21] | thanks , enter phone number ? [+98993...]) etc .

2 : when you fully wrote that conversation handler , you should simply add it to bot !

this is exactly what I mean (in the registering example):

imagine user enters "/register" command ...

# ---------- defining states ---------
ONE , TWO = range(2)

# ---------- functions ---------
def register(update: Update, context: CallbackContext):
     chat_id = update.message.chat_id
     bot.send_message(chat_id , text = "hello , you are registering ! please enter your name | type 'cancel' anytime to cancel process")
     return ONE

def got_name(update: Update, context: CallbackContext):
     chat_id = update.message.chat_id
     name = update.message.text # now we got the name
     context.user_data["name"] = name # to use it later (in next func)
     bot.send_message(chat_id , text = f"thanks {name} ! please enter your phone number")
     return TWO

def got_phone_number(update: Update, context: CallbackContext):
     chat_id = update.message.chat_id
     phone_number = update.message.text # now we got the phone number
     name = context.user_data["name"] # we had the name , remember ?!
     bot.send_message(chat_id , text = f"completed ! your name is {name} and your phone number is {phone_number}")
     return ConversationHandler.END

def cancel(update: Update, context: CallbackContext):
     chat_id = update.message.chat_id
     bot.send_message(chat_id , text = "process canceled !")
     return ConversationHandler.END

# ---------- conversation handler ---------
CH = ConversationHandler (entry_points = [CommandHandler("register", register)],
     states = {ONE : [MessageHandler(Filters.text , got_name)],
     TWO : [MessageHandler(Filters.text , got_phone_number]
     },
     fallbacks = [MessageHandler(Filters.regex('cancel'), cancel)],
     allow-reentry = True)

# -------- add handler ---------
updater.dispatcher.add_handler(CH)

in the end , remember it's way better to write any function to do 1 task , not more !

I hope it helps you , have a nice day .

  • I need it to make an help function that send a message with a text where is written "Type your help request. Type 'Cancel' to Cancel your help request.", and when the user types everything the message will be stored and sent to an admin, but when he types 'Cancel' appears a "Your message was cancelled." message. Thank you for your answer! – Christian Cristini Sep 16 '21 at 18:12