56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
import { PhotoAttachment, VK } from "vk-io";
|
|
import { env } from "../env";
|
|
import { MEDIUM_SIZES, getAttachmentBySizes } from "./utils";
|
|
import { makeLiquidRescale } from "./queues/liquid.queue";
|
|
|
|
export const bot = new VK({ token: env.VK_BOT_TOKEN });
|
|
|
|
bot.updates.on('message_new', async (context, next) => {
|
|
if (context.isChat) return; // ignore chats for now
|
|
if (!context.hasAllAttachments('photo')) return;
|
|
|
|
const processingMessage = await context.reply('Обрабатываю...');
|
|
|
|
const results = [];
|
|
|
|
for (let attachment of context.attachments) {
|
|
if (attachment.type === 'photo') {
|
|
const [size] = getAttachmentBySizes(attachment as PhotoAttachment, MEDIUM_SIZES);
|
|
const imageUrl = size?.url;
|
|
|
|
if (imageUrl) {
|
|
const liquidRescaleJob = await makeLiquidRescale({ imageUrl: imageUrl });
|
|
let result;
|
|
|
|
try {
|
|
result = await liquidRescaleJob.finished();
|
|
} catch (err) {
|
|
return context.reply('Упс... что-то пошло не так...');
|
|
}
|
|
|
|
results.push(Buffer.from(result.data));
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
const attachments: PhotoAttachment[] = [];
|
|
|
|
for (let image of results) {
|
|
const attachment = await bot.upload.messagePhoto({
|
|
peer_id: context.senderId,
|
|
source: {
|
|
values: {
|
|
value: image,
|
|
contentType: 'image/jpeg'
|
|
}
|
|
}
|
|
});
|
|
|
|
attachments.push(attachment);
|
|
}
|
|
|
|
await processingMessage.editMessage({ attachment: attachments });
|
|
|
|
return next();
|
|
}); |