I have started to write bot logic for telegram by using this module
I can create simple question and answer logic like this:
bot.onText(//start/, function(msg, match){
bot.sendMessage(msg.chat.id, "Hello this is great bot");
});
When user types /start
he will receive this message.
I want to create something like chained conversation between bot and user. Like when user types /buy
bot will show options to buy, after that user types what he wants to buy then bot will show types of selected product and so on.
How is it possible to create chained conversation between user and bot? How to make bot remember previous selected commands and reset them when it is time? Do I need to keep in my own database in order to do that?
2
Answers
You can do it in different ways.
ForceReply
of the Bot APIAlright. So for
1.
I’d say you have some benefits. You can actually guide the user and restrict access to some commands, when he is not in the proper state. So let’s say he wants to buy Popcorn but he’s in the shoestore you’d disallow the command by checking the saved user state.For
2.
you would always allow the user to use/buy
and/buy_popcorn
and/buy_shoe
. But depending on your answers you just give him a specific amount of possible selections.This would be allowed, but the user would have to manually write
/buy_shoe
The
3.
possible way is using the ForceReply. The user will automatically get ananswer to
message. So when he uses/buy_shoe
he will answer to the last message the bot sent. You will also get the message the user answered to in the message from the api. You can check if the message the user answered to is the proper precondition / proper message for the command and then restrict or allow the command.It comes down to personal preference, I guess. But all of it has pros and cons and you have to decide if you want to allow specific commands without a precondition.
This List may not be complete. It could be that there are other ways, I didn’t think about. But these 3 are ways I know of.
I also had this problem where i needed my bot to respond questions based on his last answer to the user, and 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.
As Loki pointed out, 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:
1 – User sends /authenticate.
2 – 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.
3 – User sends his e-mail.
4 – 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.
5 – User sends the code.
6 – System checks the previous state again and authenticates the user, if the code is correct.
That’s it! Hope it helps someone.