Guest User

Untitled

a guest
Feb 4th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1.  
  2.  
  3. waitForRequestExt(chatId) {
  4. return new Promise(resolve => {
  5. this._waitingRequests[chatId] = resolve
  6. this._waitForUpdate(chatId)
  7. })
  8. }
  9.  
  10. runMenuExt(chatId, menuData) {
  11. const startMessage = menuData.message
  12.  
  13. const ignoredKeys = [
  14. 'message',
  15. 'layout',
  16. 'options',
  17. 'resizeKeyboard',
  18. 'oneTimeKeyboard',
  19. 'anyMatch'
  20. ]
  21.  
  22. const keys = Object.keys(menuData)
  23. let keyboard = []
  24.  
  25. if (menuData.layout) {
  26. let lineIndex = 0
  27.  
  28. keys.forEach(key => {
  29. if (ignoredKeys.indexOf(key) === -1) {
  30. if (!keyboard[lineIndex])
  31. keyboard[lineIndex] = []
  32.  
  33. keyboard[lineIndex].push(new KeyboardButton(key))
  34.  
  35. if (typeof menuData.layout === 'number') {
  36. if (keyboard[lineIndex].length === menuData.layout) {
  37. lineIndex++
  38. }
  39. } else {
  40. if (keyboard[lineIndex].length === menuData.layout[lineIndex]) {
  41. lineIndex++
  42. }
  43. }
  44.  
  45. }
  46. })
  47. } else {
  48. keys.forEach(key => {
  49. if (ignoredKeys.indexOf(key) === -1) {
  50. keyboard.push([new KeyboardButton(key)])
  51. }
  52. })
  53. }
  54.  
  55. const resizeKeyboard = (menuData.resizeKeyboard && menuData.resizeKeyboard === true)
  56. const oneTimeKeyboard = (menuData.oneTimeKeyboard && menuData.oneTimeKeyboard === true)
  57.  
  58. let replyMarkup = new ReplyKeyboardMarkup(keyboard, resizeKeyboard, oneTimeKeyboard)
  59.  
  60. let options = {
  61. reply_markup: JSON.stringify(replyMarkup)
  62. }
  63.  
  64. if (menuData.options) options = Object.assign(options, menuData.options)
  65.  
  66. return this._api.sendMessage(chatId, startMessage, options)
  67. .then(() =>
  68. this.waitForRequestExt(chatId)
  69. .then($ => {
  70. if (keys.indexOf($.message.text) > -1 &&
  71. ignoredKeys.indexOf($.message.text) === -1) {
  72. if (typeof menuData[$.message.text] === 'object') {
  73. $.runMenu(menuData[$.message.text])
  74. } else {
  75. menuData[$.message.text]($)
  76. }
  77. } else if (menuData.anyMatch) {
  78. menuData.anyMatch($)
  79. } else {
  80. $.runMenu(menuData)
  81. }
  82. }))
  83. }
Advertisement
Add Comment
Please, Sign In to add comment