From 460da1fd8acef3e68c5262704003cfec5025e438 Mon Sep 17 00:00:00 2001 From: Kurbanov Bulat Date: Sun, 9 Jan 2022 18:50:40 +0300 Subject: [PATCH] Add sending action --- src/bots/factory/bots/approved/index.ts | 45 +++++++++++++++++-------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/src/bots/factory/bots/approved/index.ts b/src/bots/factory/bots/approved/index.ts index b2ab8be..70612d1 100644 --- a/src/bots/factory/bots/approved/index.ts +++ b/src/bots/factory/bots/approved/index.ts @@ -1,6 +1,6 @@ import * as Sentry from '@sentry/node'; -import { Context, Telegraf, Markup, TelegramError } from 'telegraf'; +import { Context, Telegraf, Markup, TelegramError, Telegram } from 'telegraf'; import { BotState, Cache } from '@/bots/manager'; @@ -151,17 +151,23 @@ export async function createApprovedBot(token: string, state: BotState): Promise const [_, format, id] = ctx.message.text.split('_'); const chatId = ctx.message.chat.id; - - if (state.cache === Cache.NO_CACHE) { - const book = await BookLibrary.getBookById(parseInt(id)); - const data = await download(book.source.id, book.remote_id, format); - ctx.telegram.sendDocument(ctx.message.chat.id, data, { - reply_to_message_id: ctx.message.message_id - }) - return; + + const sendSendingAction = async () => { + await ctx.telegram.sendChatAction(chatId, "upload_document"); } - let cache: CachedMessage | null = null; + 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) { @@ -172,12 +178,23 @@ export async function createApprovedBot(token: string, state: BotState): Promise }; const sendCached = async () => { - cache = await getCachedMessage(); - await ctx.telegram.copyMessage(chatId, cache.chat_id, cache.message_id, { - allow_sending_without_reply: true, - }); + 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();