diff --git a/src/bots/factory/bots/approved/hooks/downloading.ts b/src/bots/factory/bots/approved/hooks/downloading.ts new file mode 100644 index 0000000..3aa42c7 --- /dev/null +++ b/src/bots/factory/bots/approved/hooks/downloading.ts @@ -0,0 +1,70 @@ +import { Context } from 'telegraf'; + +import * as BookLibrary from "../services/book_library"; +import { clearBookCache, getBookCache } from '../services/book_cache'; +import { getBookCacheBuffer } from '../services/book_cache_buffer'; +import { download } from '../services/downloader'; +import { BotState, Cache } from '@/bots/manager'; + + +async function _sendFile(ctx: Context, state: BotState, chatId: number, id: number, format: string) { + const sendWithoutCache = async () => { + const book = await BookLibrary.getBookById(id); + const data = await download(book.source.id, book.remote_id, format); + await ctx.telegram.sendDocument(chatId, data) + } + + const getCachedMessage = async () => { + if (state.cache === Cache.ORIGINAL) { + return getBookCache(id, format); + } + + return getBookCacheBuffer(id, format); + }; + + const sendCached = async () => { + const cache = await getCachedMessage(); + await ctx.telegram.copyMessage(chatId, cache.chat_id, cache.message_id, { + allow_sending_without_reply: true, + }); + }; + + if (state.cache === Cache.NO_CACHE) { + await sendWithoutCache(); + return; + } + + try { + await sendCached(); + } catch (e) { + await clearBookCache(id, format); + await sendCached(); + } +} + + +export async function sendFile(ctx: Context, state: BotState) { + if (!ctx.message || !('text' in ctx.message)) { + return; + } + + const [_, format, id] = ctx.message.text.split('_'); + const chatId = ctx.message.chat.id; + + const sendSendingAction = async () => { + await ctx.telegram.sendChatAction(chatId, "upload_document"); + } + + sendSendingAction(); + const action = setInterval(() => sendSendingAction(), 1000); + + try { + await _sendFile(ctx, state, chatId, parseInt(id), format); + } catch (e) { + await ctx.reply("Ошибка! Попробуйте позже :(", { + reply_to_message_id: ctx.message.message_id, + }); + } finally { + clearInterval(action); + } +} diff --git a/src/bots/factory/bots/approved/hooks/setCommands.ts b/src/bots/factory/bots/approved/hooks/setCommands.ts new file mode 100644 index 0000000..7444f73 --- /dev/null +++ b/src/bots/factory/bots/approved/hooks/setCommands.ts @@ -0,0 +1,21 @@ +import { Telegraf, TelegramError } from "telegraf"; + + +export async function setCommands(bot: Telegraf) { + async function setMyCommands() { + await bot.telegram.setMyCommands([ + {command: "random", description: "Попытать удачу"}, + {command: "update_log", description: "Обновления каталога"}, + {command: "settings", description: "Настройки"}, + {command: "help", description: "Помощь"}, + ]); + } + + try { + await setMyCommands(); + } catch (e: unknown) { + if (e instanceof TelegramError && e.response.error_code === 429) { + setTimeout(() => setMyCommands(), 1000 * (e.response.parameters?.retry_after || 5)); + } + } +} \ No newline at end of file diff --git a/src/bots/factory/bots/approved/index.ts b/src/bots/factory/bots/approved/index.ts index 70612d1..d4d27ed 100644 --- a/src/bots/factory/bots/approved/index.ts +++ b/src/bots/factory/bots/approved/index.ts @@ -1,8 +1,8 @@ import * as Sentry from '@sentry/node'; -import { Context, Telegraf, Markup, TelegramError, Telegram } from 'telegraf'; +import { Context, Telegraf, Markup } from 'telegraf'; -import { BotState, Cache } from '@/bots/manager'; +import { BotState } from '@/bots/manager'; import env from '@/config'; @@ -11,13 +11,12 @@ import * as Messages from "./messages"; import * as CallbackData from "./callback_data"; import * as BookLibrary from "./services/book_library"; -import { CachedMessage, clearBookCache, getBookCache } from './services/book_cache'; -import { getBookCacheBuffer } from './services/book_cache_buffer'; -import { download } from './services/downloader'; import { createOrUpdateUserSettings, getUserSettings } from './services/user_settings'; import { formatBook, formatAuthor, formatSequence, formatTranslator } from './format'; import { getPaginatedMessage, registerLanguageSettingsCallback, registerPaginationCommand, registerRandomItemCallback } from './utils'; import { getRandomKeyboard, getUpdateLogKeyboard, getUserAllowedLangsKeyboard } from './keyboard'; +import { sendFile } from './hooks/downloading'; +import { setCommands } from './hooks/setCommands'; Sentry.init({ @@ -32,22 +31,7 @@ export async function createApprovedBot(token: string, state: BotState): Promise } }); - async function setMyCommands() { - await bot.telegram.setMyCommands([ - {command: "random", description: "Попытать удачу"}, - {command: "update_log", description: "Обновления каталога"}, - {command: "settings", description: "Настройки"}, - {command: "help", description: "Помощь"}, - ]); - } - - try { - await setMyCommands(); - } catch (e: unknown) { - if (e instanceof TelegramError && e.response.error_code === 429) { - setTimeout(() => setMyCommands(), 1000 * (e.response.parameters?.retry_after || 5)); - } - } + setCommands(bot); bot.use(async (ctx: Context, next) => { if (ctx.from) { @@ -144,65 +128,7 @@ export async function createApprovedBot(token: string, state: BotState): Promise registerLanguageSettingsCallback(bot, 'on', CallbackData.ENABLE_LANG_PREFIX); registerLanguageSettingsCallback(bot, 'off', CallbackData.DISABLE_LANG_PREFIX); - bot.hears(/^\/d_[a-zA-Z0-9]+_[\d]+$/gm, async (ctx: Context) => { - if (!ctx.message || !('text' in ctx.message)) { - return; - } - - const [_, format, id] = ctx.message.text.split('_'); - const chatId = ctx.message.chat.id; - - const sendSendingAction = async () => { - await ctx.telegram.sendChatAction(chatId, "upload_document"); - } - - const sendWithoutCache = async () => { - const action = setInterval(() => sendSendingAction(), 1000); - - try { - sendSendingAction(); - const book = await BookLibrary.getBookById(parseInt(id)); - const data = await download(book.source.id, book.remote_id, format); - await ctx.telegram.sendDocument(chatId, data) - } finally { - clearInterval(action); - } - } - - const getCachedMessage = async () => { - if (state.cache === Cache.ORIGINAL) { - return getBookCache(parseInt(id), format); - } - - return getBookCacheBuffer(parseInt(id), format); - }; - - const sendCached = async () => { - const action = setInterval(() => sendSendingAction(), 1000); - - try { - sendSendingAction(); - const cache = await getCachedMessage(); - await ctx.telegram.copyMessage(chatId, cache.chat_id, cache.message_id, { - allow_sending_without_reply: true, - }); - } finally { - clearInterval(action); - } - }; - - if (state.cache === Cache.NO_CACHE) { - await sendWithoutCache(); - return; - } - - try { - await sendCached(); - } catch (e) { - await clearBookCache(parseInt(id), format); - await sendCached(); - } - }); + bot.hears(/^\/d_[a-zA-Z0-9]+_[\d]+$/gm, (ctx) => sendFile(ctx, state)); bot.hears(/^\/b_info_[\d]+$/gm, async (ctx: Context) => { if (!ctx.message || !('text' in ctx.message)) {