Created
July 9, 2018 04:11
-
-
Save misaka4e21/cc28274875954fe255dd28c9ecd60475 to your computer and use it in GitHub Desktop.
telegram-amsbot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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