DEV Community

Cover image for JavaSript Bot
Server Serverlesskiy for AWS Community Builders

Posted on • Edited on

JavaSript Bot

With the help of this Telegram bot you can test your knowledge of JavaScript basics.

javascript bot

We use questions from 29 topics of our course on JavaScript basics. More details about us can be found in the previous publication.
As a result of answering all the questions, an assessment of your level of knowledge awaits you.

javascript bot result

Telegraph.js

Our bot is implemented on the framework Telegraph.js

Telegraph.js

OpenSource

The source code of the project is available on GitHub, so you can participate in its development.

open source

require('dotenv').config()
const { Telegraf, session, Stage, BaseScene } = require('telegraf')
const TelegrafI18n = require('telegraf-i18n')
const { level, getSticker, MyContext } = require('./helpers')
const { en, ru } = require('./quiz')
const path = require('path')

const i18n = new TelegrafI18n({
  defaultLanguage: 'en',
  directory: path.resolve(__dirname, 'locales')
})

let BOT_TOKEN

if (process.env.NODE_ENV === 'production') {
  BOT_TOKEN = process.env.BOT_TOKEN
} else {
  BOT_TOKEN = process.env.BOT_TOKEN_TEST
}

const bot = new Telegraf(BOT_TOKEN, { contextType: MyContext })

bot.use(i18n.middleware())

const jsRoom = new BaseScene('js-room')

let getQuiz = ctx => (ctx.i18n.locale() === 'en' ? en : ru)

let questions

jsRoom.enter(ctx => {
  questions = getQuiz(ctx)
  const questionIndex = 0
  const counter = 0
  ctx.session.counter = counter
  ctx.session.questionIndex = questionIndex

  const { title, random, correct_option_id } = questions[questionIndex]

  ctx.replyWithQuiz(`${ctx.i18n.t('Question')}: 1 ${ctx.i18n.t('from')} ${questions.length}\n${title}`, random, {
    correct_option_id,
    is_anonymous: false
  })
  ctx.reply(`${ctx.i18n.t('course')}: www.jscamp.app`)
})

jsRoom.on('poll_answer', ctx => {
  const questionIndex = ++ctx.session.questionIndex
  const result = questions[questionIndex - 1].correct_option_id === ctx.pollAnswer.option_ids[0]
  result && ++ctx.session.counter

  if (questionIndex !== questions.length) {
    const { title, random, correct_option_id } = questions[questionIndex]
    ctx.replyWithQuiz(
      `${ctx.i18n.t('Question')}: ${questionIndex + 1} ${ctx.i18n.t('from')} ${
        questions.length
      }\n${title}\n${ctx.i18n.t('score')} ${ctx.session.counter}`,
      random,
      {
        correct_option_id,
        is_anonymous: false
      }
    )
  } else {
    ctx.reply(
      `${ctx.i18n.t('score')} ${ctx.session.counter}. ${ctx.i18n.t('level')}: ${level(
        ctx.session.counter
      )} ${getSticker(ctx.session.counter)}`
    )
    ctx.reply(`${ctx.i18n.t('course')}: www.jscamp.app`)
  }

  ctx.scene.current.leave()
})

const stage = new Stage([jsRoom])
bot.context.questions = questions

bot.use(session())

bot.use((ctx, next) => next())
bot.use(stage.middleware())
bot.command('start', ctx => ctx.scene.enter('js-room'))
bot.launch()
Enter fullscreen mode Exit fullscreen mode

Internationalization (i18n)

Bot Now communicates in English and Russian, but you can send a pull request in your native language.

i18n

What's next?

Next, we plan to implement testing on TypeScript, React Native, AWS Amplify, so subscribe to our Twitter to stay tuned.

The bot is free, but you can support our startup via Patreon

Become a Patron!

Top comments (0)