diff --git a/package.json b/package.json index 4e4d2d3..4e84210 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,6 @@ "consign": "^0.1.6", "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", diff --git a/src/config/config.js b/src/config/config.js index 8261b19..16f76dd 100644 --- a/src/config/config.js +++ b/src/config/config.js @@ -1,6 +1,5 @@ const { readdirSync } = require('fs') const path = require('path') -const ms = require('ms') const oS = require('os') const cS = require('chalk') @@ -15,10 +14,19 @@ config.consignOpts = { } config.limitSecurity = { - window: ms('15s'), - limit: 4, + window: 1000 * 25, + limit: 8, + onLimitExceeded: ({ reply }) => + reply('Please wait a second', { + disable_notification: true + }) +} + +config.cbQueryOpts = { + window: 1000 * 13, + limit: 3, onLimitExceeded: ({ answerCbQuery }) => - answerCbQuery('โ—๏ธPlease wait a second !', true) + answerCbQuery('Please wait a second', true) } config.i18nOpts = { @@ -50,7 +58,6 @@ module.exports = { config, readdirSync, path, - ms, oS, cS } diff --git a/src/config/telegraf.js b/src/config/telegraf.js index 5e70eae..a47422f 100644 --- a/src/config/telegraf.js +++ b/src/config/telegraf.js @@ -1,4 +1,11 @@ -const { Telegraf, Router, Markup, Extra, session } = require('telegraf') +const { + Telegraf, + Router, + Markup, + Extra, + Composer, + session +} = require('telegraf') const { I18n } = require('telegraf-i18n') const { db } = require('../db/db') const { config } = require('./config') @@ -44,10 +51,16 @@ const Route = new Router(({ callbackQuery }) => { bot.route = Route bot.rateLimit = rateLimit -bot.context.db = db -bot.context.extra = Extra -bot.context.markup = Markup +bot.context = { + db, + Extra, + Markup, + Composer +} + +bot.use(Composer.mount('callback_query', rateLimit(config.cbQueryOpts))) +bot.use(Composer.mount('inline_query', rateLimit(config.cbQueryOpts))) bot.use(rateLimit(config.limitSecurity)) bot.use(session()) bot.use(new I18n(config.i18nOpts)) diff --git a/src/handlers/eyeofgodTask.js b/src/handlers/eyeofgodTask.js index 4549d81..1923add 100644 --- a/src/handlers/eyeofgodTask.js +++ b/src/handlers/eyeofgodTask.js @@ -1,12 +1,11 @@ const { SERVICE } = require('../util/service') class EyeofgodTask { - checkLeak = async ({ db, i18n, extra, message, session, replyWithHTML }) => { + checkLeak = async ({ match, db, i18n, Extra, message, replyWithHTML }) => { try { - const id = (session.eyeof = message.text.trim()) + const id = match[0] const base = await db.Eye.findOne({ id }).sort({ id: 1 }).lean() - /// const expectData = await SERVICE.getInfoOperator(base?.phone) - + // const expectData = await SERVICE.getInfoOperator(base?.phone) return await replyWithHTML( i18n.t('eyeofgod', { id: base.id, @@ -15,8 +14,7 @@ class EyeofgodTask { fullName: `${base?.first_name ?? ''} | ${base?.last_name ?? ''}` // ...expectData }), - extra - .HTML() + Extra.HTML() .inReplyTo(message.message_id) .webPreview(false) .markup(m => @@ -32,8 +30,7 @@ class EyeofgodTask { } catch { return await replyWithHTML( '๐Ÿ“ There is no database !', - extra - .HTML() + Extra.HTML() .inReplyTo(message.message_id) .webPreview(false) .markup(m => diff --git a/src/handlers/jokeTask.js b/src/handlers/jokeTask.js index 38f47f4..b8228dc 100644 --- a/src/handlers/jokeTask.js +++ b/src/handlers/jokeTask.js @@ -1,23 +1,42 @@ const fetch = require('node-fetch') class JokeTask { - async getRandomAnimation({ replyWithAnimation }) { - const page = Math.floor(Math.random() * 100) - return Promise.resolve( - await fetch(`https://developerslife.ru/top/${page}?json=true`) + async getRandomAnimation({ Extra, replyWithAnimation }) { + const { videoURL: sendGif, description: caption } = await getRandomGif() + console.log(await getRandomGif()) + return replyWithAnimation(sendGif, { + caption, + ...Extra.markup(m => + m.inlineKeyboard([m.callbackButton('MOAR', 'joke|moar')]) + ) + }) + } + + async updateAnimation({ Extra, answerCbQuery, editMessageMedia }) { + await answerCbQuery() + const { videoURL: sendGif, description: caption } = await getRandomGif() + return editMessageMedia( + { + caption, + type: 'animation', + media: sendGif + }, + Extra.markup(m => + m.inlineKeyboard([m.callbackButton('MOAR', 'joke|moar')]) + ) ) - .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 { videoURL: sendGif, description: caption } = data.result[index] - return replyWithAnimation(sendGif, { - caption, - parse_mode: 'HTML' - }) - }) - .catch(err => console.error('!joke ', err.message)) } } +const getRandomGif = async () => { + const rand = Math.floor(Math.random() * 100) + return await fetch(`https://developerslife.ru/top/${rand}?json=true`) + .then(res => res.json()) + .then(data => { + const index = Number(Math.floor(Math.random() * data.result.length)) + return data.result[Number(index)] + }) + .catch(err => console.error(err.message())) +} + module.exports = new JokeTask() diff --git a/src/handlers/langTask.js b/src/handlers/langTask.js index 1dfe179..9e80789 100644 --- a/src/handlers/langTask.js +++ b/src/handlers/langTask.js @@ -1,7 +1,7 @@ const { readdirSync, path } = require('../config/config') class LangTask { - updaterLang = async ({ i18n, extra, markup, replyWithHTML }) => { + updaterLang = async ({ i18n, Extra, markup, replyWithHTML }) => { const locales = {} const markupArr = [] const __PATH__ = path.resolve(__dirname, '../locales') @@ -21,7 +21,7 @@ class LangTask { return await replyWithHTML( i18n.t('default.lang'), - extra.HTML().markup(m => m.inlineKeyboard(markupArr)) + Extra.HTML().markup(m => m.inlineKeyboard(markupArr)) ) } } diff --git a/src/handlers/startTask.js b/src/handlers/startTask.js index a5e51ab..38ae209 100644 --- a/src/handlers/startTask.js +++ b/src/handlers/startTask.js @@ -1,36 +1,35 @@ class StartTask { - async start({ reply, replyWithSticker, message, i18n, from, extra }) { + async start({ reply, replyWithSticker, message, i18n, from, Extra }) { await replyWithSticker( 'CAACAgIAAxkBAAIZH2DvF-OAI9Kzy_QazXqHYR0rUD7-AAIXAAPDVgMeQjlqcLJ92EYgBA' ) - const markup = extra - .HTML() - .inReplyTo(message.message_id) - .webPreview(false) - .markup(m => - m.inlineKeyboard( - [ - m.callbackButton('๐Ÿ’ฐ Donate', 'startMenu|donate'), - m.urlButton('โค๏ธ My Notabug', 'https://notabug.org/Secven'), - m.urlButton('๐Ÿ˜Š Subscribe', 'https://t.me/ThisOpenSource'), - m.callbackButton(i18n.t('default.closeMessage'), 'delete') - ], - { columns: 2 } + return await reply( + i18n.t('main.start', { ...from }), + Extra.HTML() + .inReplyTo(message.message_id) + .webPreview(false) + .markup(m => + m.inlineKeyboard( + [ + m.callbackButton('๐Ÿ’ฐ Donate', 'startMenu|donate'), + m.urlButton('โค๏ธ My Notabug', 'https://notabug.org/Secven'), + m.urlButton('๐Ÿ˜Š Subscribe', 'https://t.me/ThisOpenSource'), + m.callbackButton(i18n.t('default.closeMessage'), 'delete') + ], + { columns: 2 } + ) ) - ) - return await reply(i18n.t('main.start', { ...from }), markup) + ) } - donateMe = async ({ i18n, extra, editMessageText }) => { + donateMe = async ({ i18n, Extra, editMessageText }) => { return editMessageText( i18n.t('main.donate'), - extra - .HTML() - .markup(m => - m.inlineKeyboard([ - m.callbackButton(i18n.t('default.closeMessage'), 'delete') - ]) - ) + Extra.HTML().markup(m => + m.inlineKeyboard([ + m.callbackButton(i18n.t('default.closeMessage'), 'delete') + ]) + ) ) } } diff --git a/src/route/eyeofgod.js b/src/route/eyeofgod.js index 18b76b5..fa1e6f0 100644 --- a/src/route/eyeofgod.js +++ b/src/route/eyeofgod.js @@ -4,9 +4,11 @@ module.exports = bot => { const expect = bot.rateLimit({ window: 1000 * 20, limit: 1, - onLimitExceeded: ({ replyWithHTML }) => - replyWithHTML( - 'I can handle from you only one request in 15 seconds.' + onLimitExceeded: ({ reply }) => + reply( + 'I can handle from you only one request in 15 seconds.', + true, + { disable_notification: true } ) }) diff --git a/src/route/joke.js b/src/route/joke.js index 065befc..59429da 100644 --- a/src/route/joke.js +++ b/src/route/joke.js @@ -3,5 +3,12 @@ module.exports = bot => { bot.hears(['/joke', '!joke', '!jo'], jokeTask.getRandomAnimation) - // TODO: add moar + const routeList = ctx => { + switch (ctx.state.value) { + case 'moar': + return jokeTask.updateAnimation(ctx) + } + } + + bot.route.on('joke', routeList) }