mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 07:25:36 +01:00
Add caption when sending file
This commit is contained in:
@@ -19,6 +19,7 @@
|
|||||||
"envalid": "^7.2.2",
|
"envalid": "^7.2.2",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"got": "^11.8.3",
|
"got": "^11.8.3",
|
||||||
|
"js-base64": "^3.7.2",
|
||||||
"moment": "^2.29.1",
|
"moment": "^2.29.1",
|
||||||
"safe-compare": "^1.1.4",
|
"safe-compare": "^1.1.4",
|
||||||
"telegraf": "^4.4.2"
|
"telegraf": "^4.4.2"
|
||||||
|
|||||||
@@ -1,22 +1,14 @@
|
|||||||
import { Context } from 'telegraf';
|
import { Context } from 'telegraf';
|
||||||
|
|
||||||
import * as BookLibrary from "../services/book_library";
|
|
||||||
import { clearBookCache, getBookCache, downloadFromCache } from '../services/book_cache';
|
import { clearBookCache, getBookCache, downloadFromCache } from '../services/book_cache';
|
||||||
import { getBookCacheBuffer } from '../services/book_cache_buffer';
|
import { getBookCacheBuffer } from '../services/book_cache_buffer';
|
||||||
import { download } from '../services/downloader';
|
|
||||||
import { BotState, Cache } from '@/bots/manager';
|
import { BotState, Cache } from '@/bots/manager';
|
||||||
|
|
||||||
|
|
||||||
async function _sendFile(ctx: Context, state: BotState, chatId: number, id: number, format: string) {
|
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 sendWithDownloadFromChannel = async () => {
|
const sendWithDownloadFromChannel = async () => {
|
||||||
const data = await downloadFromCache(id, format);
|
const data = await downloadFromCache(id, format);
|
||||||
await ctx.telegram.sendDocument(chatId, data);
|
await ctx.telegram.sendDocument(chatId, { source: data.source, filename: data.filename }, { caption: data.caption });
|
||||||
}
|
}
|
||||||
|
|
||||||
const getCachedMessage = async () => {
|
const getCachedMessage = async () => {
|
||||||
@@ -35,19 +27,14 @@ async function _sendFile(ctx: Context, state: BotState, chatId: number, id: numb
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (state.cache === Cache.NO_CACHE) {
|
if (state.cache === Cache.NO_CACHE) {
|
||||||
try {
|
return sendWithDownloadFromChannel();
|
||||||
await sendWithDownloadFromChannel();
|
|
||||||
} catch (e) {
|
|
||||||
await sendWithoutCache();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await sendCached();
|
return await sendCached();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
await clearBookCache(id, format);
|
await clearBookCache(id, format);
|
||||||
await sendCached();
|
return sendCached();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,9 +55,11 @@ export async function sendFile(ctx: Context, state: BotState) {
|
|||||||
const action = setInterval(() => sendSendingAction(), 1000);
|
const action = setInterval(() => sendSendingAction(), 1000);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await _sendFile(ctx, state, chatId, parseInt(id), format);
|
return await _sendFile(ctx, state, chatId, parseInt(id), format);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
await ctx.reply("Ошибка! Попробуйте позже :(", {
|
console.log(e);
|
||||||
|
|
||||||
|
return await ctx.reply("Ошибка! Попробуйте позже :(", {
|
||||||
reply_to_message_id: ctx.message.message_id,
|
reply_to_message_id: ctx.message.message_id,
|
||||||
});
|
});
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ export async function createApprovedBot(token: string, state: BotState): Promise
|
|||||||
registerLanguageSettingsCallback(bot, 'on', CallbackData.ENABLE_LANG_PREFIX);
|
registerLanguageSettingsCallback(bot, 'on', CallbackData.ENABLE_LANG_PREFIX);
|
||||||
registerLanguageSettingsCallback(bot, 'off', CallbackData.DISABLE_LANG_PREFIX);
|
registerLanguageSettingsCallback(bot, 'off', CallbackData.DISABLE_LANG_PREFIX);
|
||||||
|
|
||||||
bot.hears(/^\/d_[a-zA-Z0-9]+_[\d]+$/gm, (ctx) => sendFile(ctx, state));
|
bot.hears(/^\/d_[a-zA-Z0-9]+_[\d]+$/gm, async (ctx) => sendFile(ctx, state));
|
||||||
|
|
||||||
bot.hears(/^\/b_info_[\d]+$/gm, async (ctx: Context) => {
|
bot.hears(/^\/b_info_[\d]+$/gm, async (ctx: Context) => {
|
||||||
if (!ctx.message || !('text' in ctx.message)) {
|
if (!ctx.message || !('text' in ctx.message)) {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import got from 'got';
|
import got from 'got';
|
||||||
|
import { decode } from 'js-base64';
|
||||||
|
|
||||||
import env from '@/config';
|
import env from '@/config';
|
||||||
|
|
||||||
@@ -55,17 +56,23 @@ export async function clearBookCache(bookId: number, fileType: string): Promise<
|
|||||||
export interface DownloadedFile {
|
export interface DownloadedFile {
|
||||||
source: Buffer;
|
source: Buffer;
|
||||||
filename: string;
|
filename: string;
|
||||||
|
caption: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function downloadFromCache(bookId: number, fileType: string): Promise<DownloadedFile> {
|
export async function downloadFromCache(bookId: number, fileType: string): Promise<DownloadedFile> {
|
||||||
const response = await got<string>(`${env.DOWNLOADER_URL}/api/v1/download/${bookId}/${fileType}`, {
|
const response = await got<string>(`${env.CACHE_SERVER_URL}/api/v1/download/${bookId}/${fileType}`, {
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': env.DOWNLOADER_API_KEY,
|
'Authorization': env.CACHE_SERVER_API_KEY,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const captionData = response.headers['x-caption-b64'];
|
||||||
|
|
||||||
|
if (captionData === undefined || Array.isArray(captionData)) throw Error('No caption?');
|
||||||
|
|
||||||
return {
|
return {
|
||||||
source: response.rawBody,
|
source: response.rawBody,
|
||||||
filename: (response.headers['content-disposition'] || '').split('filename=')[1]
|
filename: (response.headers['content-disposition'] || '').replaceAll('"', "").split('filename=')[1],
|
||||||
|
caption: decode(captionData),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user