telegraf-quiz
v2.0.0
Published
Telegraf quiz/survey engine
Downloads
10
Maintainers
Readme
Telegraf quiz engine
Quiz engine for Telegraf.
Based on Kwiz library.
Installation
$ npm install telegraf-quiz
Example
var Telegraf = require('telegraf')
var Quizes = require('telegraf-quiz')
var Quize = require('telegraf-quiz').Quiz
// More info: https://github.com/telegraf/kwiz
var sampleQuizDefinition = {
questions: [...]
}
const telegraf = new Telegraf(process.env.BOT_TOKEN)
const quizes = new Quizes()
// Specify cancel quiz commands, default value: [`/cancel`]
quizes.cancelCommands = ['/cancel', '/stop', 'please stop', 'sudo stop']
// For testing only. Session will be lost on app restart
telegraf.use(Telegraf.memorySession())
// Add middleware
telegraf.use(quizes.middleware())
const beveragePoll = new Quiz('beveragePoll', sampleQuizDefinition)
beveragePoll.onCompleted((ctx) => {
const results = JSON.stringify(ctx.state.quiz, null, 2)
const status = ctx.state.quiz.canceled ? 'canceled' : 'completed'
return ctx.reply(`Quiz ${status} ${results}`)
})
// Register quiz
quizes.register(beveragePoll)
// start quiz on command
telegraf.command('/start', (ctx) => {
return ctx.quiz.start('beveragePoll')
})
telegraf.startPolling()
License
The MIT License (MIT)
Copyright (c) 2016 Vitaly Domnikov
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.