skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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.

    1. Using InlineKeyboardMarkup with question key as data.
    2. I suggest you to save a question tree to the database and save current state of each user.

    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.

    Login or Signup to reply.
  3. You can use inline buttons. Solution with JavaScript:

    var options = {
      reply_markup: JSON.stringify({
        inline_keyboard: [
          [
            {text: 'button 1', callback_data: '1'},
            {text: 'button 2', callback_data: '2'},
            {text: 'button 3', callback_data: '3'},
          ],
        ]
      })
    };
    
    bot.sendMessage(chatId, 'Pick action:', options);
    

    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:

    {
      q: 'Which path you wanna go?',
      a1: { ... },
      a2: {
        value: 'right',
        q: 'You're going right. You see the wall, what will you do?',
        a1: {
           a1: { ... },
           a2: { ... }
           a3: {
             value: 'go back',
             q: 'You're going back and you see the tiger, what will you do?',
             a1: { ... },
             a2: { ... }
           }
        },
        a2: { ... },
        a3: { ... }
      },
      a3: { ... }
    }
    

    And it’s also easy to go pack. Just pop one value from the stack and you’re on previous menu.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search