Skip to content

Instantly share code, notes, and snippets.

@misaka4e21
Created July 9, 2018 04:11
Show Gist options
  • Save misaka4e21/cc28274875954fe255dd28c9ecd60475 to your computer and use it in GitHub Desktop.
Save misaka4e21/cc28274875954fe255dd28c9ecd60475 to your computer and use it in GitHub Desktop.
telegram-amsbot
const fs = require('fs')
function getRandomArrayElements(arr, count) {
var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
while (i-- > min) {
index = Math.floor((i + 1) * Math.random());
temp = shuffled[index];
shuffled[index] = shuffled[i];
shuffled[i] = temp;
}
return shuffled.slice(min);
}
const Telegraf = require('telegraf')
const Markup = require('telegraf/markup')
const bot = new Telegraf(process.env.BOT_TOKEN)
const tk = JSON.parse(fs.readFileSync('wxdtk.json'))
bot.command('getquestion', (ctx)=>{
const question = getRandomArrayElements(tk, 1)[0]
const answers = question.answers.map((a)=>{
return [Markup.callbackButton(a.text, (a.correct?'Correct':'Wrong'))]
});
return ctx.reply(question.question, Markup.inlineKeyboard(answers).extra())
})
bot.action('Correct', (ctx)=>ctx.answerCbQuery('你答对了!'))
bot.action('Wrong', (ctx)=>ctx.answerCbQuery('你答错了!'))
bot.catch((err) => {
console.log('Ooops', err)
})
bot.startPolling()
{
"name": "telegram-amsbot",
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"telegraf": "^3.21.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment