upated menu api, now with a seperator key, if needed

master
cheetah 4 years ago
parent ad82e778ed
commit 4ecb426958

@ -53,7 +53,7 @@ app.post('/api/message/advanced/menu', async (req, res) => {
let id = await types.MessageManager.New('duplex', req.body.routing, let id = await types.MessageManager.New('duplex', req.body.routing,
req.body.payload + '\n\n' + Object.keys(req.body.options).map((key, index) => { req.body.payload + '\n\n' + Object.keys(req.body.options).map((key, index) => {
return `[${ index + 1 }] ${ req.body.options[key].txt }` return `[${ index + 1 }]${ req.body.options[key].sep || ' ' }${ req.body.options[key].txt }`
}).join('\n') }).join('\n')
) )
types.MessageManager.attachMenudata(id, { types.MessageManager.attachMenudata(id, {

@ -5,7 +5,9 @@ const md5 = require('md5')
const axios = require('axios') const axios = require('axios')
function clamp(v,min,max) { function clamp(v,min,max) {
return Math.max(Math.min(v,min),max) if (v < min) return min
if (v > max) return max
return v
} }
class MessageManager { class MessageManager {
constructor() { constructor() {
@ -76,14 +78,16 @@ class MessageManager {
respondToMessage(msgId, response) { respondToMessage(msgId, response) {
this.messages[ msgId ]._routerData.response = response this.messages[ msgId ]._routerData.response = response
this.events.emit('event', 'response', [msgId, response]) this.events.emit('event', 'response', [msgId, response])
if (!!this.messages[ msgId ]._routerData.menu) { if (!!this.messages[ msgId ]._routerData.menu && !!this.messages[ msgId ]._routerData.menu.options) {
const menuOptions = this.messages[ msgId ]._routerData.menu
const menuOptions = this.messages[ msgId ]._routerData.menu.options
console.log(this.messages[ msgId ]._routerData.menu, menuOptions, parseInt(response) - 1, clamp(-1+response, 0, Object.keys(menuOptions).length))
const selectedMenu = menuOptions[ Object.keys(menuOptions)[ const selectedMenu = menuOptions[ Object.keys(menuOptions)[
clamp(+response-1, 0, Object.keys(menuOptions).length) clamp(parseInt(response) - 1, 0, Object.keys(menuOptions).length)
] ] ] ]
this.events.emit('event', 'menu', [msgId, response, selectedMenu]) this.events.emit('event', 'menu', [msgId, response, selectedMenu])
if (!!menuOptions.url) { if (!!selectedMenu && !!selectedMenu.url) {
axios.get(menuOptions.url).then( () => false ) axios.get(selectedMenu.url).then( () => false )
} }
} }
} }

Loading…
Cancel
Save