Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- waitForRequestExt(chatId) {
- return new Promise(resolve => {
- this._waitingRequests[chatId] = resolve
- this._waitForUpdate(chatId)
- })
- }
- runMenuExt(chatId, menuData) {
- const startMessage = menuData.message
- const ignoredKeys = [
- 'message',
- 'layout',
- 'options',
- 'resizeKeyboard',
- 'oneTimeKeyboard',
- 'anyMatch'
- ]
- const keys = Object.keys(menuData)
- let keyboard = []
- if (menuData.layout) {
- let lineIndex = 0
- keys.forEach(key => {
- if (ignoredKeys.indexOf(key) === -1) {
- if (!keyboard[lineIndex])
- keyboard[lineIndex] = []
- keyboard[lineIndex].push(new KeyboardButton(key))
- if (typeof menuData.layout === 'number') {
- if (keyboard[lineIndex].length === menuData.layout) {
- lineIndex++
- }
- } else {
- if (keyboard[lineIndex].length === menuData.layout[lineIndex]) {
- lineIndex++
- }
- }
- }
- })
- } else {
- keys.forEach(key => {
- if (ignoredKeys.indexOf(key) === -1) {
- keyboard.push([new KeyboardButton(key)])
- }
- })
- }
- const resizeKeyboard = (menuData.resizeKeyboard && menuData.resizeKeyboard === true)
- const oneTimeKeyboard = (menuData.oneTimeKeyboard && menuData.oneTimeKeyboard === true)
- let replyMarkup = new ReplyKeyboardMarkup(keyboard, resizeKeyboard, oneTimeKeyboard)
- let options = {
- reply_markup: JSON.stringify(replyMarkup)
- }
- if (menuData.options) options = Object.assign(options, menuData.options)
- return this._api.sendMessage(chatId, startMessage, options)
- .then(() =>
- this.waitForRequestExt(chatId)
- .then($ => {
- if (keys.indexOf($.message.text) > -1 &&
- ignoredKeys.indexOf($.message.text) === -1) {
- if (typeof menuData[$.message.text] === 'object') {
- $.runMenu(menuData[$.message.text])
- } else {
- menuData[$.message.text]($)
- }
- } else if (menuData.anyMatch) {
- menuData.anyMatch($)
- } else {
- $.runMenu(menuData)
- }
- }))
- }
Advertisement
Add Comment
Please, Sign In to add comment