I want my Telegram bot to process user inputs based on the last question the bot asked. Basically, this is the flow:
- User calls /authenticate command
- Bot asks for email
- User sends his email
- Bot answers with a message about sending a code to the user’s email for confirmation and asks for the user to type the code on the chat
- User types the code
- Bot validates the user code and user is authenticated and start receiving notifications
The problem is: how do i know that the user is answering a specific bot question in this flow?
I thought about two ways to do so:
-
Send the message with force reply option so the user has to reply to the bot question. This would send me back the message to which the user is responding so i could compare the bot message string to see what was the answer about.
-
Store the last bot message somewhere, then when a message arrives, check what was the last bot message and assume that the user message is a response.
Is there a better way? I am using Java with telegrambots library.
3
Answers
Since it was quite hard to find ideas that could lead me to a solution (in Java), I am going to share mine here for the sake of future Java googlers. I am using telegrambots library together with Spring Boot/Data.
The best way to implement this flow is saving states on your database. To do so, use the messages unique chat ids to distinguish a chat from another.
Here is the relevant part of the Java implementation (the logic pretty much applies to any language):
The entity that holds Telegram chat information related to a system user.
The enum that represents all possible bot responses (states).
The service that receives messages and respond accordingly.
The implemented flow is:
User sends /authenticate.
System knows nothing about the device, so store the chat id and the last state. The last state will be the response to the user. System asks for the user's e-mail.
User sends his e-mail.
The text is not recognized as a command, so system checks if there is a last state relative to this chat id. If a previous state exists, use the incoming text as a parameter to this state's method. System sends a code to the user's e-mail and asks for it.
User sends the code.
System checks the previous state again and authenticates the user, if the code is correct.
That's it! Hope it helps someone.
Usually to be specific on what are you sending to the telegram bot, you would communicate with the bot using commands, (telegram commands starts with /, and each command have a specific command handler on the server side), unfortunately, so far there is no way to send extra params along with the command to the telegram bot, you can use one of the workarounds mentioned in the conversation below :
How do I have my Bot respond with arguments?
if you are not familiar with bot commands using java, please refer to the following examples:
https://www.programcreek.com/java-api-examples/?api=org.telegram.telegrambots.bots.AbsSender
I hope this helps.
One of the most popular tasks when developing telegram bots.
There is a solution, you need to install the shapoapps/multibot_driver package (composer require shapoapps/multibot_driver) for Laravel.
This package has a session manager, an analogue of sessions for web page users. After each user message, you record user input into the session on server side. When each new request(message) received, you read the data from the session and build the logic.
Here’s the documentation – https://github.com/shapoapps/multibot_driver