diff --git a/lib/notification/adapter/telegram.js b/lib/notification/adapter/telegram.js index 15b5289..a33b1ce 100644 --- a/lib/notification/adapter/telegram.js +++ b/lib/notification/adapter/telegram.js @@ -63,31 +63,41 @@ export const send = ({ serviceName, newListings, notificationConfig, jobKey }) = const jobName = job == null ? jobKey : job.name; 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', body: JSON.stringify(body), headers: { 'Content-Type': 'application/json' }, }); + return res; }); const promises = newListings.map(async (o) => { 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) { - return throttledCall('sendPhoto', { + if (!img) { + return throttledCall('sendMessage', textPayload); + } + + try { + return await throttledCall('sendPhoto', { chat_id: chatId, photo: img, caption: buildCaption(jobName, serviceName, o), 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);