您可以使用内联按钮。 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);
看,你可以传递任何回调数据。所以我会在这个callback_data 属性中保存当前路径。格式不重要,可以是数组、字符串等。但它将代表堆栈。
例如,路径a2.a1.a3 表示您点击了2号按钮,然后点击了1号,然后点击了3号。数据结构可以类似于:
{
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: { ... }
}
而且打包也很容易。只需从堆栈中弹出一个值即可进入上一个菜单。