Skip to content

Commit

Permalink
Add more parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
varanauskas authored Sep 24, 2023
1 parent 04957f3 commit a464dcd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
16 changes: 12 additions & 4 deletions lib/telegram.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { RetryableError, wrapRetryable } from "./retryable.mjs";

const request = wrapRetryable(async function request(telegramToken, method, chat_id, body) {
let response;
let json;
try {
response = await fetch(`https://api.telegram.org/bot${telegramToken}/${method}`, {
method: "POST",
Expand All @@ -12,17 +11,26 @@ const request = wrapRetryable(async function request(telegramToken, method, chat
...body
})
});
json = await JSON.parse(response);
} catch (error) {
throw new RetryableError(`Telegram general error`, true, error);
throw new RetryableError(`Telegram fetch error`, true, error);
}
const text = await response.text();
if (response.status === 502) {
throw new RetryableError(`Telegram error 502 (${text})`, true);
}
let json;
try {
json = JSON.parse(text);
} catch (error) {
throw new RetryableError(`Telegram JSON parse error (${text})`, true, error);
}
if (!json.ok) {
let canRetryOrRetryAfter = response.status === 502;
if (response.status === 429) {
const { retry_after } = json.parameters ?? { retry_after: 10 };
canRetryOrRetryAfter = retry_after;
}
throw new RetryableError(`Telegram error ${response.status} (${JSON.stringify(json)})`, canRetryOrRetryAfter);
throw new RetryableError(`Telegram error ${response.status} (${text})`, canRetryOrRetryAfter);
}
return json.result;
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tokentx-notifier",
"version": "1.4.3",
"version": "1.4.4",
"description": "A telegram bot that sends a message when new ERC20 transactions are detected",
"keywords": ["telegram", "crypto", "ethereum", "erc20", "bot"],
"homepage": "https://github.com/varanauskas/tokentx-notifier",
Expand Down

0 comments on commit a464dcd

Please sign in to comment.