feat: send typing and uploading actions
This commit is contained in:
parent
80bcd539bd
commit
7541deb178
|
@ -16,6 +16,7 @@ export async function queueCommand(ctx: Grammy.CommandContext<Context>) {
|
||||||
.then((jobs) => jobs.map((job, index) => ({ ...job.value, place: index + 1 })));
|
.then((jobs) => jobs.map((job, index) => ({ ...job.value, place: index + 1 })));
|
||||||
const jobs = [...processingJobs, ...waitingJobs];
|
const jobs = [...processingJobs, ...waitingJobs];
|
||||||
const { bold } = GrammyParseMode;
|
const { bold } = GrammyParseMode;
|
||||||
|
|
||||||
return fmt([
|
return fmt([
|
||||||
"Current queue:\n",
|
"Current queue:\n",
|
||||||
...jobs.length > 0
|
...jobs.length > 0
|
||||||
|
@ -47,8 +48,9 @@ export async function queueCommand(ctx: Grammy.CommandContext<Context>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleFutureUpdates() {
|
async function handleFutureUpdates() {
|
||||||
for (let idx = 0; idx < 20; idx++) {
|
for (let idx = 0; idx < 30; idx++) {
|
||||||
await new Promise((resolve) => setTimeout(resolve, 3000));
|
await ctx.api.sendChatAction(ctx.chat.id, "typing");
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 4000));
|
||||||
const nextFormattedMessage = await getMessageText();
|
const nextFormattedMessage = await getMessageText();
|
||||||
if (nextFormattedMessage.text !== formattedMessage.text) {
|
if (nextFormattedMessage.text !== formattedMessage.text) {
|
||||||
await ctx.api.editMessageText(
|
await ctx.api.editMessageText(
|
||||||
|
|
|
@ -113,6 +113,8 @@ async function processJob(job: IKV.Model<JobSchema>, worker: WorkerData, config:
|
||||||
|
|
||||||
// we have to check if job is still processing at every step because TypeScript
|
// we have to check if job is still processing at every step because TypeScript
|
||||||
if (job.value.status.type === "processing") {
|
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 now there is no status message
|
||||||
if (!job.value.status.message) {
|
if (!job.value.status.message) {
|
||||||
// send a new status message
|
// send a new status message
|
||||||
|
@ -160,25 +162,28 @@ async function processJob(job: IKV.Model<JobSchema>, worker: WorkerData, config:
|
||||||
// process the job
|
// process the job
|
||||||
const handleProgress = async (progress: SdProgressResponse) => {
|
const handleProgress = async (progress: SdProgressResponse) => {
|
||||||
if (job.value.status.type === "processing" && job.value.status.message) {
|
if (job.value.status.type === "processing" && job.value.status.message) {
|
||||||
await bot.api.editMessageText(
|
await Promise.all([
|
||||||
job.value.status.message.chat.id,
|
bot.api.sendChatAction(job.value.chat.id, "upload_photo", { maxAttempts: 1 }),
|
||||||
job.value.status.message.message_id,
|
bot.api.editMessageText(
|
||||||
`Generating your prompt now... ${
|
job.value.status.message.chat.id,
|
||||||
(progress.progress * 100).toFixed(0)
|
job.value.status.message.message_id,
|
||||||
}% using ${worker.name}`,
|
`Generating your prompt now... ${
|
||||||
{ maxAttempts: 1 },
|
(progress.progress * 100).toFixed(0)
|
||||||
).catch(() => undefined);
|
}% 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<unknown>;
|
let response: SdResponse<unknown>;
|
||||||
const taskType = job.value.task.type; // don't narrow this to never pls typescript
|
const taskType = job.value.task.type; // don't narrow this to never pls typescript
|
||||||
|
@ -243,6 +248,8 @@ async function processJob(job: IKV.Model<JobSchema>, worker: WorkerData, config:
|
||||||
let resultMessages: GrammyTypes.Message.MediaMessage[] | undefined;
|
let resultMessages: GrammyTypes.Message.MediaMessage[] | undefined;
|
||||||
while (true) {
|
while (true) {
|
||||||
sendMediaAttempt++;
|
sendMediaAttempt++;
|
||||||
|
await bot.api.sendChatAction(job.value.chat.id, "upload_photo", { maxAttempts: 1 })
|
||||||
|
.catch(() => undefined);
|
||||||
|
|
||||||
// parse files from reply JSON
|
// parse files from reply JSON
|
||||||
const inputFiles = await Promise.all(
|
const inputFiles = await Promise.all(
|
||||||
|
@ -270,7 +277,12 @@ async function processJob(job: IKV.Model<JobSchema>, worker: WorkerData, config:
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger().warning(`Sending images (attempt ${sendMediaAttempt}) failed: ${err}`);
|
logger().warning(`Sending images (attempt ${sendMediaAttempt}) failed: ${err}`);
|
||||||
if (sendMediaAttempt >= 6) throw 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue