From 47a42320225a00746e8b70416508de8c71e69cde Mon Sep 17 00:00:00 2001 From: Edgar To Date: Sun, 9 Sep 2018 06:42:14 +0200 Subject: [PATCH] feat(question): add question --- README.md | 12 ++++++++++- example.js | 47 +++++++++++++++++++++++++++++++++++++++++ telegraf-inline-menu.js | 30 +++++++++++++++++++++++++- 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a5f34d2..1b8899e 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,6 @@ Add a Button for a manual (or legacy) bot.action `text` can be a `string` or a `function(ctx)` that will be set as the Button text. `hide(ctx)` (optional) can hide the button when return is true. - ### `menu.submenu(text, menu, {hide})` Creates a Button in the menu to a submenu @@ -125,3 +124,14 @@ Can only be used when `isSetFunc` is not used. This is an alias for `menu.select` The wording makes more sense with list that are not exclusive selections. + +### `menu.question(actionCode, buttonText, setFunc, {questionText, hide})` + +When the user presses the button, he will be asked a question. +The answer he gives will be given via `setFunc(ctx, answer)` + +`actionCode` has to be unique in this menu. +`buttonText` can be a `string` or a `function(ctx)` that will be set as the Button text. +`setFunc(ctx, answer)` will be called when the user answers the question. +`questionText` (optional) can be a string. This has to be globally unique! If this is not unique it will collide with the other question with the same text and probably not work as intended. +`hide(ctx)` (optional) can hide the button when return is true. diff --git a/example.js b/example.js index e51e9f6..0aff884 100644 --- a/example.js +++ b/example.js @@ -17,6 +17,53 @@ eventMenu.toggle('t', 'toggle me', () => { someValue = !someValue }, {isSetFunc: () => someValue}) +const allEvents = [ + 'AA', + 'AD', + 'AF', + 'CE', + 'DT', + 'VS' +] + +function selectEvent(ctx, selected) { + return ctx.answerCbQuery(selected + ' was added') +} + +const addMenu = new TelegrafInlineMenu('e:a', 'Welche Events möchtest du hinzufügen?') +function filterText(ctx) { + let text = '🔎 Filter' + if (ctx.session.eventfilter !== '.+') { + text += ': ' + ctx.session.eventfilter + } + return text +} +addMenu.question('filter', filterText, + (ctx, answer) => { + ctx.session.eventfilter = answer + }, { + questionText: 'Wonach möchtest du filtern?' + } +) + +addMenu.toggle('clearfilter', 'Filter aufheben', ctx => { + ctx.session.eventfilter = '.+' +}, { + hide: ctx => ctx.session.eventfilter === '.+' +}) + +addMenu.list('add', () => allEvents, selectEvent, { + hide: (ctx, selectedEvent) => { + console.log('addMenu list hide', ctx.session.eventfilter) + const filter = ctx.session.eventfilter || '.+' + const regex = new RegExp(filter, 'i') + return !regex.test(selectedEvent) + }, + columns: 3 +}) + +eventMenu.submenu('Hinzufügen…', addMenu) + mainMenu.submenu('Events', eventMenu) const settingsMenu = new TelegrafInlineMenu('s', '*Settings*') diff --git a/telegraf-inline-menu.js b/telegraf-inline-menu.js index 65f921f..8bfaa25 100644 --- a/telegraf-inline-menu.js +++ b/telegraf-inline-menu.js @@ -1,4 +1,4 @@ -const {Composer, Extra} = require('telegraf') +const {Composer, Extra, Markup} = require('telegraf') const {getRowsOfButtons} = require('./align-buttons') const {buildKeyboard} = require('./build-keyboard') @@ -178,6 +178,34 @@ class TelegrafInlineMenu { result.forEach(o => this.buttons.push(o)) } } + + question(action, buttonText, setFunc, {hide, questionText} = {}) { + if (!questionText) { + questionText = buttonText + } + + const actionCode = this.prefix + ':' + action + + this.bot.on('text', Composer.optional(ctx => ctx.message && ctx.message.reply_to_message && ctx.message.reply_to_message.text === questionText, async ctx => { + const answer = ctx.message.text + await setFunc(ctx, answer) + return this.replyMenuNow(ctx) + })) + + this.bot.action(actionCode, this.hideMiddleware(hide, ctx => { + const extra = Extra.markup(Markup.forceReply()) + return Promise.all([ + ctx.reply(questionText, extra), + ctx.deleteMessage() + ]) + })) + + this.buttons.push([{ + text: buttonText, + actionCode, + hide + }]) + } } function generateSelectButtons(actionCodePrefix, options, {isSetFunc, prefixFunc, hide, columns}) {