Telegram bot嵌套前一个按钮,node.js

时间:2017-01-17 10:44:37

标签: node.js telegram telegram-bot node-telegram-bot-api

如何在第一级节点之后从第3级节点返回到第二级节点?我的问题是Back名称,因为那是相同的,,, 这是我的代码:

    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

2 个答案:

答案 0 :(得分:2)

您可能应该做的是保存状态,用户当前正在使用他的唯一ID。

我没有将nodejs用于电报机器人的经验,因此以下代码是伪的。

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

因此,您将在每次请求后更新状态,对于previous之类的请求,您将根据上次保存的状态计算所需的键盘。

答案 1 :(得分:1)

解决callback_queryInline Keyboards

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



bot.on('callback_query', msg => {
 if (msg.data == `back3`) {
}
}