make it better

This commit is contained in:
Secven 2021-07-15 05:05:36 +00:00
parent 84fbbee61d
commit 3c1cf8f964
9 changed files with 106 additions and 63 deletions

View File

@ -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",

View File

@ -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
}

View File

@ -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))

View File

@ -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(
'📍 <b>There is no database !</b>',
extra
.HTML()
Extra.HTML()
.inReplyTo(message.message_id)
.webPreview(false)
.markup(m =>

View File

@ -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()

View File

@ -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))
)
}
}

View File

@ -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')
])
)
)
}
}

View File

@ -4,9 +4,11 @@ module.exports = bot => {
const expect = bot.rateLimit({
window: 1000 * 20,
limit: 1,
onLimitExceeded: ({ replyWithHTML }) =>
replyWithHTML(
'<code>I can handle from you only one request in 15 seconds.</code>'
onLimitExceeded: ({ reply }) =>
reply(
'<code>I can handle from you only one request in 15 seconds.</code>',
true,
{ disable_notification: true }
)
})

View File

@ -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)
}