From 4ecb426958462773fb9b820c0d7c5a9f1b26cccd Mon Sep 17 00:00:00 2001 From: cheetah Date: Tue, 11 May 2021 14:51:47 +0000 Subject: [PATCH] upated menu api, now with a seperator key, if needed --- index.js | 2 +- types/MessageManager.js | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index ac8afaa..d781c3c 100644 --- a/index.js +++ b/index.js @@ -53,7 +53,7 @@ app.post('/api/message/advanced/menu', async (req, res) => { let id = await types.MessageManager.New('duplex', req.body.routing, 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') ) types.MessageManager.attachMenudata(id, { diff --git a/types/MessageManager.js b/types/MessageManager.js index 4e0a672..9a6305a 100644 --- a/types/MessageManager.js +++ b/types/MessageManager.js @@ -5,7 +5,9 @@ const md5 = require('md5') const axios = require('axios') 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 { constructor() { @@ -76,14 +78,16 @@ class MessageManager { respondToMessage(msgId, response) { this.messages[ msgId ]._routerData.response = response this.events.emit('event', 'response', [msgId, response]) - if (!!this.messages[ msgId ]._routerData.menu) { - const menuOptions = this.messages[ msgId ]._routerData.menu + if (!!this.messages[ msgId ]._routerData.menu && !!this.messages[ msgId ]._routerData.menu.options) { + + 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)[ - 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]) - if (!!menuOptions.url) { - axios.get(menuOptions.url).then( () => false ) + if (!!selectedMenu && !!selectedMenu.url) { + axios.get(selectedMenu.url).then( () => false ) } } }