Add caption when sending file

This commit is contained in:
2022-01-10 02:47:26 +03:00
parent a0f855b10b
commit 97f44459f8
4 changed files with 20 additions and 23 deletions

View File

@@ -19,6 +19,7 @@
"envalid": "^7.2.2",
"express": "^4.17.1",
"got": "^11.8.3",
"js-base64": "^3.7.2",
"moment": "^2.29.1",
"safe-compare": "^1.1.4",
"telegraf": "^4.4.2"

View File

@@ -1,22 +1,14 @@
import { Context } from 'telegraf';
import * as BookLibrary from "../services/book_library";
import { clearBookCache, getBookCache, downloadFromCache } 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 sendWithDownloadFromChannel = async () => {
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 () => {
@@ -35,19 +27,14 @@ async function _sendFile(ctx: Context, state: BotState, chatId: number, id: numb
};
if (state.cache === Cache.NO_CACHE) {
try {
await sendWithDownloadFromChannel();
} catch (e) {
await sendWithoutCache();
}
return;
return sendWithDownloadFromChannel();
}
try {
await sendCached();
return await sendCached();
} catch (e) {
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);
try {
await _sendFile(ctx, state, chatId, parseInt(id), format);
return await _sendFile(ctx, state, chatId, parseInt(id), format);
} catch (e) {
await ctx.reply("Ошибка! Попробуйте позже :(", {
console.log(e);
return await ctx.reply("Ошибка! Попробуйте позже :(", {
reply_to_message_id: ctx.message.message_id,
});
} finally {

View File

@@ -128,7 +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, (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) => {
if (!ctx.message || !('text' in ctx.message)) {

View File

@@ -1,4 +1,5 @@
import got from 'got';
import { decode } from 'js-base64';
import env from '@/config';
@@ -55,17 +56,23 @@ export async function clearBookCache(bookId: number, fileType: string): Promise<
export interface DownloadedFile {
source: Buffer;
filename: string;
caption: string;
}
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: {
'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 {
source: response.rawBody,
filename: (response.headers['content-disposition'] || '').split('filename=')[1]
filename: (response.headers['content-disposition'] || '').replaceAll('"', "").split('filename=')[1],
caption: decode(captionData),
}
}