Skip to content

Commit

Permalink
Fix broken css + remove log
Browse files Browse the repository at this point in the history
  • Loading branch information
nhamonin committed Dec 13, 2024
1 parent 574ab7e commit 3c9b944
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
2 changes: 0 additions & 2 deletions routes/faceitWebhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ export default {
const eventHandler = eventHandlers.get(data.event);

if (eventHandler) {
console.log('eventHandler', JSON.stringify(data.payload));

await eventHandler(data);
} else {
console.warn(`Unhandled event type: ${data.event}`);
Expand Down
63 changes: 37 additions & 26 deletions utils/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,50 +60,61 @@ function logEvent(chat, action) {

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

try {
await page.setContent(html);

await page.waitForNetworkIdle({
idleTime: 350,
timeout: 10000,
await page.waitForFunction(() => {
const stylesheets = document.querySelectorAll('link[rel="stylesheet"]');
return Array.from(stylesheets).every((link) => link.sheet !== null);
});

image = await page.screenshot({
fullPage: true,
encoding: 'binary',
await page.waitForNetworkIdle({
idleTime: 1000,
timeout: 15000,
});

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

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

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

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);
}
}
}
await Promise.all(
chatsToSend.map((chat_id) =>
withErrorHandling(
async () => {
await tBot.sendPhoto(
chat_id,
Buffer.from(image, 'binary'),
message_id ? getBasicTelegramOptions(message_id) : {}
);
},
async (e) => {
if (e.message.startsWith(ERROR_TELEGRAM_FORBIDDEN)) {
await handleBlockedToSendMessage(chat_id);
} else {
console.log(e);
}
}
)()
)
);
}

async function getBrowser() {
Expand Down

0 comments on commit 3c9b944

Please sign in to comment.