make it better

This commit is contained in:
Secven 2021-07-08 00:09:38 +00:00
parent 940cd25299
commit a353d0d944
8 changed files with 48 additions and 5 deletions

View File

@ -19,6 +19,7 @@
"dotenv": "^10.0.0",
"mongoose": "^5.13.1",
"ms": "^2.1.3",
"node-fetch": "^2.6.1",
"telegraf": "git+https://notabug.org/Secven/telegraf",
"telegraf-i18n": "git+https://notabug.org/Secven/telegraf-i18n.git",
"telegraf-middleware-console-time": "^2.1.0"

View File

@ -31,7 +31,7 @@ config.i18nOpts = {
}
config.settings = {
setCommand: async ctx => {
setCommand: async (ctx, next) => {
ctx.setMyCommands([
{
command: 'start',
@ -40,8 +40,13 @@ config.settings = {
{
command: 'lang',
description: 'Change language'
},
{
command: 'joke',
description: 'IT GIfs or !joke'
}
])
return next()
}
}

View File

@ -1,4 +1,11 @@
const { Telegraf, Router, Markup, Extra, session } = require('telegraf')
const {
Telegraf,
Composer,
Router,
Markup,
Extra,
session
} = require('telegraf')
const { I18n } = require('telegraf-i18n')
const { db } = require('../db/db')
const { config } = require('./config')
@ -28,6 +35,7 @@ const Route = new Router(({ callbackQuery }) => {
;(async () => {
if (config.isDev) {
await bot.use(config.settings.setCommand)
const { TG_APIROOT, BOT_CLUSTER_CORE } = process.env
console.log(
Object.assign(await bot.telegram.getMe(), {
@ -35,7 +43,6 @@ const Route = new Router(({ callbackQuery }) => {
TG_APIROOT
})
)
await bot.use(config.settings.setCommand)
// Dev handlers
bot.use(cliUpdateTime())
bot.use(Telegraf.log())
@ -54,6 +61,7 @@ bot.use(updaterUser)
bot.use(reportErr)
bot.on('callback_query', Route)
bot.route.otherwise(({ deleteMessage }) => deleteMessage())
consign(config.consignOpts).include('handlers').then('route').into(bot)

23
src/handlers/jokeTask.js Normal file
View File

@ -0,0 +1,23 @@
const fetch = require('node-fetch')
class JokeTask {
async getRandomAnimation(ctx) {
const page = Math.floor(Math.random() * 100)
return Promise.resolve(
await fetch(`https://developerslife.ru/top/${page}?json=true`)
)
.then(res => res.json())
.then(data => {
const index = Number(Math.floor(Math.random() * data.result.length))
// eslint-disable-next-line security/detect-object-injection
const send = data.result[index]
return ctx.replyWithAnimation(send.videoURL, {
caption: `${send.description}`,
parse_mode: 'HTML'
})
})
.catch(err => console.error('!joke ', err.message))
}
}
module.exports = new JokeTask()

View File

@ -21,5 +21,6 @@ module.exports = async ({ telegram, updateType, i18n }, next) => {
}
)
.catch(noop)
console.log(__TEMPLATE__)
})
}

5
src/route/joke.js Normal file
View File

@ -0,0 +1,5 @@
module.exports = bot => {
const { jokeTask } = bot.handlers
bot.command('joke', jokeTask.getRandomAnimation)
}

View File

@ -1,6 +1,8 @@
module.exports = bot => {
const { langTask } = bot.handlers
bot.command('lang', langTask.updaterLang)
const routeList = async ({
i18n,
state,
@ -17,6 +19,5 @@ module.exports = bot => {
}
}
bot.command('lang', langTask.updaterLang)
bot.route.on('set_lang', routeList)
}

View File

@ -11,5 +11,4 @@ module.exports = bot => {
}
bot.route.on('startMenu', routeList)
bot.route.otherwise(({ deleteMessage }) => deleteMessage())
}