1

I'm currently developing a telegram bot with python-telegram-bot. I want to be able to get the message, that was replied to ForceReply. The expected flow is this:

  1. User sends /start command
  2. Bot sends a message with some information. The message is linked with ForceReply
  3. User replies to the message
  4. Handle and operate the message.

How could I achieve the expected result? Thanks

initial_message = "Hi... Please reply to this message to proceed to the next step..."
def start (update: Update, context: CallbackContext):
 chat_id = update.message.chat_id
 print(update.message.from_user.username)
 context.bot.send_message(chat_id=chat_id, text=initial_message, reply_markup=ForceReply())
asked Feb 20, 2021 at 17:03

1 Answer 1

1

To be able to handle the ForceReply() you must implement a ConversationHandler. Once the user reply to ForceReply, with the ConversationHandler, you'll be able to deal with his/her reply. That's how it's done:

END = ConversationHandler.END
NEXTSTEP = range (1)
initial_message = "Hi... Please reply to this message to proceed to the next step..."
def start (update, context):
 chat_id = update.message.chat_id
 print(update.message.from_user.username)
 context.bot.send_message(chat_id=chat_id,
 text=initial_message,
 reply_markup=ForceReply())
 return NEXTSTEP
def nextstep (update, context):
 chat_id = update.message.chat_id
 ##user reply will be assigned to replied variable below
 replied = update.message.text
 print(replied)
 return END
def main():
 ##handler start
 start_handler = ConversationHandler(
 entry_points = [CommandHandler('start', start)],
 states = {
 NEXTSTEP: [MessageHandler(Filters.text & ~Filters.command, nextstep)]},
 fallbacks = [CommandHandler('cancel', callback = functions.cancel)])
 dp.add_handler(start_handler)
answered Feb 24, 2021 at 14:48
Sign up to request clarification or add additional context in comments.

1 Comment

Good example, works well

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.