Skip to content

Commit

Permalink
Fix image generation problem with new puppeteer dependency version
Browse files Browse the repository at this point in the history
  • Loading branch information
nhamonin committed Dec 13, 2024
1 parent 30aa1a0 commit 574ab7e
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 deletions utils/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ function logEvent(chat, action) {

async function sendPhoto(chatIDs, message_id, html, logEnabled = true) {
const tBot = getTelegramBot();
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
const page = await browser.newPage();
let image = null;

try {
Expand All @@ -72,43 +71,39 @@ async function sendPhoto(chatIDs, message_id, html, logEnabled = true) {
timeout: 10000,
});

await withErrorHandling(async () => {
image = await page.screenshot({
fullPage: true,
});
})();
image = await page.screenshot({
fullPage: true,
encoding: 'binary',
});
} catch (e) {
console.log(e);
console.error(e);
return;
} finally {
await page.close();
await context.close();
}

if (!image) {
console.error('Failed to generate image');
return;
}

const chatsToSend =
!logEnabled || !isProduction || (chatIDs.length === 1 && chatIDs[0] === +TELEGRAM_ADMIN_CHAT_ID)
? chatIDs
: [...chatIDs, TELEGRAM_LOGS_CHAT_ID];

await Promise.all(
chatsToSend.map((chat_id) =>
withErrorHandling(
async () => {
await tBot.sendPhoto(
chat_id,
image,
message_id ? getBasicTelegramOptions(message_id) : {}
);
},
async (e) => {
if (e.message.startsWith(ERROR_TELEGRAM_FORBIDDEN)) {
await handleBlockedToSendMessage(chat_id);
} else {
console.log(e);
}
}
)()
)
);
for (const chat_id of chatsToSend) {
try {
const options = message_id ? getBasicTelegramOptions(message_id) : {};
await tBot.sendPhoto(chat_id, Buffer.from(image), options);
} catch (e) {
if (e.message.startsWith(ERROR_TELEGRAM_FORBIDDEN)) {
await handleBlockedToSendMessage(chat_id);
} else {
console.error(`Failed to send photo to chat ${chat_id}:`, e);
}
}
}
}

async function getBrowser() {
Expand Down

0 comments on commit 574ab7e

Please sign in to comment.