1

How Can I Back from 3th Level node to Secend level node after That to First Level node? My Problem is Back Name, Because That is Same,,, This is my Code:

    bot.onText(/\/start/, function onLoveText(msg) {
      const opts = {
        reply_to_message_id: msg.message_id,
        reply_markup: JSON.stringify({
          keyboard: StartKeyboard,
          resize_keyboard:true,
          one_time_keyboard: true
        })
      };
      bot.sendMessage(msg.chat.id, 'Hello', opts);
    });
 bot.onText(/\Back/, function onLoveText(msg) {
      const opts = {
        reply_to_message_id: msg.message_id,
        reply_markup: JSON.stringify({
          keyboard: StartKeyboard,
          resize_keyboard:true,
          one_time_keyboard: true
        })
      };
      bot.sendMessage(msg.chat.id, 'Hello', opts);
    });
 bot.onText(/\Back/, function onLoveText(msg) {  //  I Can't Use Backkk
      const opts = {
        reply_to_message_id: msg.message_id,
        reply_markup: JSON.stringify({
          keyboard: BackKeyboard,
          resize_keyboard:true,
          one_time_keyboard: true
        })
      };
      bot.sendMessage(msg.chat.id, 'Hello', opts);
    });

enter image description here

Saeed Heidarizarei
  • 8,406
  • 21
  • 60
  • 103

2 Answers2

2

What you probably should do is save the state, the user is currently in using his unique id.

I don't have experience using nodejs for telegram bots, so the following code is kind of pseudo.

userStateMap = {};

switch ($chat.text)
{
    case "/start":
          sendWelcomeMessage($chat);
          userStateMap[$chat.id] = 0; // save the state
          break;
    case "1":
          $chat.text = "you clicked 1";
          $chat.reply_markup = getCustomKeyboard();
          return sendMessage($chat);
          userStateMap[$chat.id] = 1; // save the state
          break;
    case "about":
          $chat.text = "you clicked about";
          $chat.reply_markup = getCustomKeyboard();
          return sendMessage($chat);
          userStateMap[$chat.id] = 4; // save the state
          break;
    case "2":
          $chat.text = "you clicked 2";
          $chat.reply_markup = getCustomKeyboard2();
          return sendMessage($chat);
          userStateMap[$chat.id] = 2; // save the state
          break;
    case "previous":
          $chat.text = "you clicked previous";
          $chat.reply_markup = getCustomKeyboard(userStateMap[$chat.id]); // use the saved state
          return sendMessage($chat);
          break;
    case "3":
          $chat.text = "you clicked 3";
          $chat.reply_markup = getCustomKeyboard3();
          return sendMessage($chat);
          userStateMap[$chat.id] = 3; // save the state
          break;
}

//customize getCustomKeyboard to receive the state and use it to calculate the next keyboard

So you would update the state after each request, and for requests like previous you would calculate the needed keyboard depending on the last saved state.

Loki
  • 4,065
  • 4
  • 29
  • 51
  • What RegExp? Do you still need help with this one? Just update your question with the newest code and problem. – Loki Jan 31 '17 at 09:10
  • i updated my question code, someone said me use this code, with this modules. `node-telegram-bot-api`, but i couldn't understand very well – Saeed Heidarizarei Jan 31 '17 at 09:17
1

Solved With callback_query and Inline Keyboards

const helpKeyboard = [[{
    text: `back`,
    callback_data: `back3`
}]]



bot.on('callback_query', msg => {
 if (msg.data == `back3`) {
}
}
Saeed Heidarizarei
  • 8,406
  • 21
  • 60
  • 103