JavaSript Bot
Tags: javascript, reactnative, awsamplify, telegrambot
Published: 2021-03-26 08:19:00

With the help of [this Telegram bot](https://t.me/javascriptcamp_bot) you can test your knowledge of JavaScript basics.

![javascript bot](https://miro.medium.com/max/1400/1*x9F9oX8vTt5e-bVxL4oOog.png)

We use questions from 29 topics [of our course](https://www.jscamp.app/docs/javascript01/) on JavaScript basics. More details [about us](https://www.jscamp.app/ru/docs/javascript00/) 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](https://miro.medium.com/max/1400/1*KCe76zg2M56lT-234Xi1NA.png)

## Telegraph.js

Our bot is implemented on the framework [Telegraph.js](https://telegraf.js.org/)

![Telegraph.js](https://www.jscamp.app/ru/assets/images/telegraf-d36e4607b15da8027e31376bdbf3cc87.jpg)

## OpenSource

The source code of the project is available on [GitHub](https://github.com/gHashTag/javascriptcamp_bot/tree/heroku), so you can participate in its development.

![open source](https://media.giphy.com/media/7FgmaCJgUAMxRWatWB/giphy.gif)

```javascript
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()
```

## Internationalization (i18n)

[Bot Now](https://github.com/gHashTag/javascriptcamp_bot/tree/heroku/src/quiz) communicates in English and Russian, but you can send a pull request in your native language.

![i18n](https://www.jscamp.app/ru/assets/images/i18n-a62c87991c92c39578a22a032f1341e3.png)

## What's next?

Next, we plan to implement testing on TypeScript, React Native, AWS Amplify, so subscribe to our [Twitter](https://twitter.com/serverlesskiy) to stay tuned.

The bot is free, but you can support our startup via [Patreon](https://www.patreon.com/javascriptcamp)

![Become a Patron!](https://www.jscamp.app/ru/assets/images/patreon-125e12ee418648f7d837c9fcdab55d26.jpg)