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?

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);
});
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
Most helpful comment
In addition to @yagop's response, consider this too.
Whenever the user invokes a
Previous, the bot receives atextmessage with its contents beingPrevious. 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:That will give you a general idea!