From 7541deb178160c2e807adaed94a7edea719111b2 Mon Sep 17 00:00:00 2001 From: pinks Date: Tue, 12 Sep 2023 22:39:53 +0200 Subject: [PATCH] feat: send typing and uploading actions --- bot/queueCommand.ts | 6 ++++-- tasks/processJobs.ts | 50 +++++++++++++++++++++++++++----------------- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/bot/queueCommand.ts b/bot/queueCommand.ts index d9de069..31d2f41 100644 --- a/bot/queueCommand.ts +++ b/bot/queueCommand.ts @@ -16,6 +16,7 @@ export async function queueCommand(ctx: Grammy.CommandContext) { .then((jobs) => jobs.map((job, index) => ({ ...job.value, place: index + 1 }))); const jobs = [...processingJobs, ...waitingJobs]; const { bold } = GrammyParseMode; + return fmt([ "Current queue:\n", ...jobs.length > 0 @@ -47,8 +48,9 @@ export async function queueCommand(ctx: Grammy.CommandContext) { } async function handleFutureUpdates() { - for (let idx = 0; idx < 20; idx++) { - await new Promise((resolve) => setTimeout(resolve, 3000)); + for (let idx = 0; idx < 30; idx++) { + await ctx.api.sendChatAction(ctx.chat.id, "typing"); + await new Promise((resolve) => setTimeout(resolve, 4000)); const nextFormattedMessage = await getMessageText(); if (nextFormattedMessage.text !== formattedMessage.text) { await ctx.api.editMessageText( diff --git a/tasks/processJobs.ts b/tasks/processJobs.ts index 8aa56be..d64a7b4 100644 --- a/tasks/processJobs.ts +++ b/tasks/processJobs.ts @@ -113,6 +113,8 @@ async function processJob(job: IKV.Model, worker: WorkerData, config: // we have to check if job is still processing at every step because TypeScript if (job.value.status.type === "processing") { + await bot.api.sendChatAction(job.value.chat.id, "upload_photo", { maxAttempts: 1 }) + .catch(() => undefined); // if now there is no status message if (!job.value.status.message) { // send a new status message @@ -160,25 +162,28 @@ async function processJob(job: IKV.Model, worker: WorkerData, config: // process the job const handleProgress = async (progress: SdProgressResponse) => { if (job.value.status.type === "processing" && job.value.status.message) { - await bot.api.editMessageText( - job.value.status.message.chat.id, - job.value.status.message.message_id, - `Generating your prompt now... ${ - (progress.progress * 100).toFixed(0) - }% using ${worker.name}`, - { maxAttempts: 1 }, - ).catch(() => undefined); + await Promise.all([ + bot.api.sendChatAction(job.value.chat.id, "upload_photo", { maxAttempts: 1 }), + bot.api.editMessageText( + job.value.status.message.chat.id, + job.value.status.message.message_id, + `Generating your prompt now... ${ + (progress.progress * 100).toFixed(0) + }% using ${worker.name}`, + { maxAttempts: 1 }, + ), + job.update((value) => ({ + ...value, + status: { + type: "processing", + progress: progress.progress, + worker: worker.name, + updatedDate: new Date(), + message: value.status.type !== "done" ? value.status.message : undefined, + }, + }), { maxAttempts: 1 }), + ]).catch(() => undefined); } - await job.update((value) => ({ - ...value, - status: { - type: "processing", - progress: progress.progress, - worker: worker.name, - updatedDate: new Date(), - message: value.status.type !== "done" ? value.status.message : undefined, - }, - }), { maxAttempts: 1 }).catch(() => undefined); }; let response: SdResponse; const taskType = job.value.task.type; // don't narrow this to never pls typescript @@ -243,6 +248,8 @@ async function processJob(job: IKV.Model, worker: WorkerData, config: let resultMessages: GrammyTypes.Message.MediaMessage[] | undefined; while (true) { sendMediaAttempt++; + await bot.api.sendChatAction(job.value.chat.id, "upload_photo", { maxAttempts: 1 }) + .catch(() => undefined); // parse files from reply JSON const inputFiles = await Promise.all( @@ -270,7 +277,12 @@ async function processJob(job: IKV.Model, worker: WorkerData, config: } catch (err) { logger().warning(`Sending images (attempt ${sendMediaAttempt}) failed: ${err}`); if (sendMediaAttempt >= 6) throw err; - await Async.delay(10000); + // wait 2 * 5 seconds before retrying + for (let i = 0; i < 2; i++) { + await bot.api.sendChatAction(job.value.chat.id, "upload_photo", { maxAttempts: 1 }) + .catch(() => undefined); + await Async.delay(5000); + } } }