Node-telegram-bot-api: How can I go back to the last step/level, using a 'Previous' button?

Created on 29 Jan 2017  路  5Comments  路  Source: yagop/node-telegram-bot-api

Hello Mr/Ms
I have 3 Keyboards and 2 Previous buttons, Previous buttons label are same, (not previous one and previous two).
my question is: how can i back from level 3 to level 2, after that from level 2 to level 1?
untitled
and my code writed with your node-telegram-bot-api
I told my question to 3 developers but they chouln't awnser me clear.
Appreciate It.

bot.onText(/\/start/, function (msg) {
  const opts = {
      reply_to_message_id: msg.message_id,
      reply_markup: {
        resize_keyboard: true,
        one_time_keyboard: true,
        keyboard: [
          ['Level 1'],
        ],
      },
    };

    bot.sendMessage(msg.chat.id, 'I\'m a test robot', opts);
});
bot.onText(/\Level 1/, function (msg) {
  const opts = {
      reply_to_message_id: msg.message_id,
      reply_markup: {
        resize_keyboard: true,
        one_time_keyboard: true,
        keyboard: [
          ['Level 2','Previous'],
        ],
      },
    };

    bot.sendMessage(msg.chat.id, 'I\'m a test robot', opts);
});
bot.onText(/\Level 2/, function (msg) {
  const opts = {
      reply_to_message_id: msg.message_id,
      reply_markup: {
        resize_keyboard: true,
        one_time_keyboard: true,
        keyboard: [
          ['END','Previous'],
        ],
      },
    };

    bot.sendMessage(msg.chat.id, 'I\'m a test robot', opts);
});
question

Most helpful comment

In addition to @yagop's response, consider this too.

Whenever the user invokes a Previous, the bot receives a text message with its contents being Previous. With that only, you can not determine the right level to back into. A possible solution would be maintain a session for the user, tracking the level the user is currently in. Imagine this:

const session = getSessionSomehow();

// handles Level 1
function LevelOneHandler(msg) {
    session.save(msg.from.id, "current_level", 1);
    // ... do sth ...
}

// improve your regexps please!
bot.onText(/^Level 1$/, LevelOneHandler);

bot.onText(/^Previous$/, function (msg) {
    const currentLevel = session.get(msg.from.id, "current_level");
    switch (currentLevel) {
        case "1": return LevelOneHandler(msg);
        // ... more ...
    }
});

That will give you a general idea!

All 5 comments

bot.onText(/\Level 1/, function (msg) {

Seems L its being scaped

In addition to @yagop's response, consider this too.

Whenever the user invokes a Previous, the bot receives a text message with its contents being Previous. With that only, you can not determine the right level to back into. A possible solution would be maintain a session for the user, tracking the level the user is currently in. Imagine this:

const session = getSessionSomehow();

// handles Level 1
function LevelOneHandler(msg) {
    session.save(msg.from.id, "current_level", 1);
    // ... do sth ...
}

// improve your regexps please!
bot.onText(/^Level 1$/, LevelOneHandler);

bot.onText(/^Previous$/, function (msg) {
    const currentLevel = session.get(msg.from.id, "current_level");
    switch (currentLevel) {
        case "1": return LevelOneHandler(msg);
        // ... more ...
    }
});

That will give you a general idea!

Hope the responses answer your question. Closing this now. Thanks for using and supporting this library.

Hi, @GochoMugo, can You show how i can get "getSessionSomehow()" and use "session.save()".

@serfer89 as @GochoMugo said, you can track the level, so you have to save the level somewhere, like database, you can have a table in database with two columns (userId, current_level) and whenever user send a message, you have to update that level in DB. so session.save just is a method that update the user record in db

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alikhil picture alikhil  路  3Comments

Panthro picture Panthro  路  3Comments

jacopocappelli1989 picture jacopocappelli1989  路  4Comments

mbrammer picture mbrammer  路  3Comments

kenanchristian picture kenanchristian  路  4Comments