Skip to content

Commit

Permalink
feat(question): add question
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Sep 9, 2018
1 parent ad04310 commit 47a4232
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
47 changes: 47 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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*')
Expand Down
30 changes: 29 additions & 1 deletion telegraf-inline-menu.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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}) {
Expand Down

0 comments on commit 47a4232

Please sign in to comment.