Skip to content

Instantly share code, notes, and snippets.

@fonov
Created November 24, 2016 17:07
Show Gist options
  • Save fonov/0ae8511fe63370c6f41c1d3fce594688 to your computer and use it in GitHub Desktop.
Save fonov/0ae8511fe63370c6f41c1d3fce594688 to your computer and use it in GitHub Desktop.
protorunMenu
protorunMenu(menuData, stop){
if(typeof menuData.title == 'undefined')
throw Error('titleis not set')
if(typeof menuData.menu == 'undefined')
throw Error('menu is not set')
const message = '<b>'+menuData.title+'</b>'+((typeof menuData.message == 'undefined') ? '' : '\n'+menuData.message)
var keyboard = [],
action = {}
for (var i = 0; i < menuData.menu.length; i++){
var array = []
for (var q = 0; q < menuData.menu[i].length; q++){
let random = Math.random().toString(36).substring(7)
array.push({
text: menuData.menu[i][q].text,
callback_data: random
})
action[random] = menuData.menu[i][q].action
}
keyboard.push(array)
if(menuData.menu.length-1 == i){
if(typeof menuData.back != 'undefined'){
let random = Math.random().toString(36).substring(7)
keyboard.push([{
text: lg.back,
callback_data: random
}])
action[random] = menuData.back
}
keyboard = JSON.stringify({inline_keyboard: keyboard})
}
}
this.SessionSetMessage(message, keyboard, () => {
this.waitForRequest
.then($ => {
$.SessionGetMessage((messageId) => {
if(typeof action[$.message.text] != 'undefined')
action[$.message.text]($)
}, (messageId) => {
$.SessionClose(messageId, null, () => {
$.protorunMenu(menuData, true)
})
}, (messageId) => {
$.SessionClose(messageId, null, () => {
$.protorunMenu(menuData)
})
})
})
}, stop)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment