Skip to content

Commit

Permalink
feat(question): allow question to be answered by all kinds of messages
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Sep 10, 2018
1 parent bad1221 commit f811904
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion telegraf-inline-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class TelegrafInlineMenu {

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 => {
this.bot.on('message', 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)
Expand Down
21 changes: 18 additions & 3 deletions telegraf-inline-menu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,11 @@ test('select with prefix', async t => {
}))
})

function exampleQuestionMenu(questionText) {
function exampleQuestionMenu(questionText, expectedAnswer) {
const menu = new TelegrafInlineMenu('a:b', 'some text')

const setFunc = ({t}, answer) => {
t.is(answer, 'correct')
t.is(answer, expectedAnswer)
}

const optionalArgs = {
Expand Down Expand Up @@ -354,7 +354,7 @@ test('question answer', async t => {
t.plan(3)

const questionText = 'wat?'
const menu = exampleQuestionMenu(questionText)
const menu = exampleQuestionMenu(questionText, 'correct')
const bot = new Telegraf()
bot.context.t = t
bot.context.editMessageText = () => t.pass()
Expand All @@ -370,3 +370,18 @@ test('question answer', async t => {
// Will setFunction (+1) and reply menu after that (+1 -> 3)
await bot.handleUpdate({message: {reply_to_message: {text: questionText}, text: 'correct'}})
})

test('question works not only with text', async t => {
const questionText = 'wat?'
const menu = exampleQuestionMenu(questionText)
const bot = new Telegraf()
bot.context.t = t
bot.context.editMessageText = () => {}
bot.context.reply = () => {}
bot.context.answerCbQuery = () => {}
bot.context.deleteMessage = () => {}
bot.use(menu)
bot.use(ctx => t.fail('update not handled: ' + JSON.stringify(ctx.update)))

await bot.handleUpdate({message: {reply_to_message: {text: questionText}, photo: {}, caption: '42'}})
})

0 comments on commit f811904

Please sign in to comment.