I would like to have a little Telegram bot in python where a command is issued by the user, the bot asks a question and reacts depending on the answer. The official Telegram API mentions that this is possible using ForceReply(), e.g. for creating polls step by step, see here Official Telegram API # ForceReply.
I wrote the following code:
def test_function(update: Update, context: CallbackContext) -> None:
msg = context.bot.send_message(chat_id=update.message.chat_id,
text="Please enter some text.",
reply_markup=telegram.ForceReply(True))
if __name__ == '__main__':
dispatcher.add_handler(CommandHandler("test", test_function))
updater.start_polling()
updater.idle()
So when /test
is issued by the user, he is asked to enter some text and due to the ForceReply(True)
, the user is forced to reply to the message. But how do I get the result of this reply, i.e. the text replied by the user? There is no hint in the API docs and I also spent some time searching on the internet and on SO but didn’t find anything. Therefore, most likely the answer is simple and clear but I just don’t get it at the moment.
Any kind of help is appreciated.
2
Answers
aiogram framework already solved your task
Every step is a state of user.
It’s called
FSM
(finite state machine
).You don’t even need to do something with
ForceReply
.Example
You receive the answer as a general message (see any demo on echo-bot), and that test if it is a reply to your message. If it is, you can process the answer and proceed further.
Example:
I pinned and unpinned questions as a part of an experiment – just drop it, if not needed.
Dialog will look like