{"size": 1220, "ext": "js", "max_stars_repo_path": "src/index.js", "max_stars_repo_name": "Frodiwe/OnlinerRobot", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/index.js", "max_issues_repo_name": "Frodiwe/OnlinerRobot", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/index.js", "max_forks_repo_name": "Frodiwe/OnlinerRobot", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.3720930233, "max_line_length": 96, "alphanum_fraction": 0.6631147541}
const Telegraf = require('telegraf')
const { TelegrafMongoSession } = require('telegraf-session-mongodb')
const { MongoClient } = require('mongodb')

const bot = new Telegraf(process.env.BOT_TOKEN)
const session = {}
bot.use((...args) => session.middleware(...args))

bot.start((ctx) => {
  return ctx.reply(
    (!ctx.session || !ctx.session.url)
      ? ('Please, send a link from the Onliner with preselected filters.')
      : ('Current link is:\n\n' + ctx.session.url),
    {
      disable_web_page_preview: true,
    },
  )
})
bot.command('stop', (ctx) => {
  ctx.session.url = null

  return ctx.reply(
    'Sorry if you were insulted by this bot, I\'ve just tried to make this world a bit better.',
  )
})
bot.hears(/https:\/\/r.onliner.by\/ak\//ig, (ctx) => {
  ctx.session.url = ctx.message.text

  return ctx.reply('Thanks, the link has been updated.')
})

MongoClient.connect(process.env.MONGO_URI, { useNewUrlParser: true }).
  then((client) => {
    const db = client.db()
    const mongoSession = new TelegrafMongoSession(db, {
      collectionName: process.env.SESSIONS_COLLECTION,
    })

    session.middleware = mongoSession.middleware.bind(mongoSession)
    bot.launch()
  }).
  catch(console.error)
