Skip to content

Commit 035f0e9

Browse files
authored
Check Telegram response (#205) (#211)
* Add error handling and logging to Telegram message sending * Add debug logging for new listings
1 parent a5efd9a commit 035f0e9

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

lib/FredyRuntime.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class FredyRuntime {
7777
}
7878

7979
_findNew(listings) {
80+
logger.debug(`Checking ${listings.length} listings for new entries (Provider: '${this._providerId}')`);
8081
const hashes = getKnownListingHashesForJobAndProvider(this._jobKey, this._providerId) || [];
8182

8283
const newListings = listings.filter((o) => !hashes.includes(o.id));
@@ -95,6 +96,7 @@ class FredyRuntime {
9596
}
9697

9798
_save(newListings) {
99+
logger.debug(`Storing ${newListings.length} new listings (Provider: '${this._providerId}')`);
98100
storeListings(this._jobKey, this._providerId, newListings);
99101
return newListings;
100102
}
@@ -103,7 +105,9 @@ class FredyRuntime {
103105
const filteredList = listings.filter((listing) => {
104106
const similar = this._similarityCache.hasSimilarEntries(listing.title, listing.address);
105107
if (similar) {
106-
logger.debug(`Filtering similar entry for title: ${listing.title} and address ${listing.address}`);
108+
logger.debug(
109+
`Filtering similar entry for title '${listing.title}' and address '${listing.address}' (Provider: '${this._providerId}')`,
110+
);
107111
}
108112
return !similar;
109113
});
@@ -112,7 +116,11 @@ class FredyRuntime {
112116
}
113117

114118
_handleError(err) {
115-
if (err.name !== 'NoNewListingsWarning') logger.error(err);
119+
if (err.name === 'NoNewListingsWarning') {
120+
logger.debug(`No new listings found (Provider: '${this._providerId}').`);
121+
} else {
122+
logger.error(err);
123+
}
116124
}
117125
}
118126

lib/notification/adapter/telegram.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getJob } from '../../services/storage/jobStorage.js';
33
import fetch from 'node-fetch';
44
import pThrottle from 'p-throttle';
55
import { normalizeImageUrl } from '../../utils.js';
6+
import logger from '../../services/logger.js';
67

78
const RATE_LIMIT_INTERVAL = 1000;
89
const chatThrottleMap = new Map();
@@ -68,6 +69,11 @@ export const send = ({ serviceName, newListings, notificationConfig, jobKey }) =
6869
body: JSON.stringify(body),
6970
headers: { 'Content-Type': 'application/json' },
7071
});
72+
73+
if (!res.ok) {
74+
const errorBody = await res.text();
75+
throw new Error(`API error for '${jobName}'. '${endpoint}' returned ${errorBody}`);
76+
}
7177
return res;
7278
});
7379

@@ -81,23 +87,23 @@ export const send = ({ serviceName, newListings, notificationConfig, jobKey }) =
8187
};
8288

8389
if (!img) {
84-
return throttledCall('sendMessage', textPayload);
90+
return await throttledCall('sendMessage', textPayload).catch(async (e) => {
91+
logger.error(`Error sending message to Telegram: ${e.message}`);
92+
});
8593
}
8694

87-
try {
88-
return await throttledCall('sendPhoto', {
89-
chat_id: chatId,
90-
photo: img,
91-
caption: buildCaption(jobName, serviceName, o),
92-
parse_mode: 'HTML',
95+
return await throttledCall('sendPhoto', {
96+
chat_id: chatId,
97+
photo: img,
98+
caption: buildCaption(jobName, serviceName, o),
99+
parse_mode: 'HTML',
100+
}).catch(async (e) => {
101+
logger.error(`Error sending photo to Telegram and use a fallback: ${e.message}`);
102+
return await throttledCall('sendMessage', textPayload).catch((e) => {
103+
logger.error(`Error sending message to Telegram: ${e.message}`);
104+
throw e;
93105
});
94-
} catch (e) {
95-
// If we see a timeout due to sending an image, try sending it without
96-
if (e && (e.code === 'ETIMEDOUT' || e.errno === 'ETIMEDOUT')) {
97-
return throttledCall('sendMessage', textPayload);
98-
}
99-
throw e;
100-
}
106+
});
101107
});
102108

103109
return Promise.all(promises);

0 commit comments

Comments
 (0)