fixing telegram

This commit is contained in:
orangecoding
2025-09-12 13:45:54 +02:00
parent ac0ea64c07
commit edc91291b6

View File

@@ -63,31 +63,41 @@ export const send = ({ serviceName, newListings, notificationConfig, jobKey }) =
const jobName = job == null ? jobKey : job.name; const jobName = job == null ? jobKey : job.name;
const throttledCall = getThrottled(chatId, async function (endpoint, body) { const throttledCall = getThrottled(chatId, async function (endpoint, body) {
await fetch(`https://api.telegram.org/bot${token}/${endpoint}`, { const res = await fetch(`https://api.telegram.org/bot${token}/${endpoint}`, {
method: 'post', method: 'post',
body: JSON.stringify(body), body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
}); });
return res;
}); });
const promises = newListings.map(async (o) => { const promises = newListings.map(async (o) => {
const img = normalizeImageUrl(o.image); const img = normalizeImageUrl(o.image);
const textPayload = {
chat_id: chatId,
text: buildText(jobName, serviceName, o),
parse_mode: 'HTML',
disable_web_page_preview: true,
};
if (img) { if (!img) {
return throttledCall('sendPhoto', { return throttledCall('sendMessage', textPayload);
}
try {
return await throttledCall('sendPhoto', {
chat_id: chatId, chat_id: chatId,
photo: img, photo: img,
caption: buildCaption(jobName, serviceName, o), caption: buildCaption(jobName, serviceName, o),
parse_mode: 'HTML', parse_mode: 'HTML',
}); });
} catch (e) {
// If we see a timeout due to sending an image, try sending it without
if (e && (e.code === 'ETIMEDOUT' || e.errno === 'ETIMEDOUT')) {
return throttledCall('sendMessage', textPayload);
}
throw e;
} }
return throttledCall('sendMessage', {
chat_id: chatId,
text: buildText(jobName, serviceName, o),
parse_mode: 'HTML',
disable_web_page_preview: true,
});
}); });
return Promise.all(promises); return Promise.all(promises);