I want to develop a telegram Bot with multiple options, for example at first I show 3 buttons : button 1 , button 2 , button 3
and when user click on each, again, I show three other buttons : button 1 , button 2 , button 3
and after step 2, I want to show user first and second button he clicked, for example :
he first click on button 3 and then click on button 1, so, I want to show :
31
how can I know which button at he clicked first ? is there anyway to get chat history from bot ? or I should save his clicks on a database ? or what is your solution ?
*I am using c# and telegram.bot , but just give me a solution, language is not important.
3
Answers
You should save the chat_id and buttons clicked in a database. Chat history for bots is short lived and may not be reliable for your purpose.
First you can use inline keyboard that gives you the user updates with
data
. That means you can obtain Ids for any question and when user click you get the question Id.Then when user clicked a button; you get the question key and find it in the tree and ask the next question or find where it comes from (previous questions).
If you want to run any method related to each question you can simply store each question method name in tree and when user arrives fire that method with Reflection.
You can use inline buttons. Solution with JavaScript:
See, you can pass any callback data. So I would save current path in this
callback_data
property. Format is not important, can be array, string, whatever. But it will represent the stack.For example, path
a2.a1.a3
means that you clicked on button number 2, then on number 1, and then on number 3. And data structure can be similar to this:And it’s also easy to go pack. Just pop one value from the stack and you’re on previous menu.