Personal Telegram developer console for GitHub
fix: propagate Telegram API failures
davoodmehraban89 committed
Oct 2, 2026 at 22:19 UTC
40e31294950ba6e3d85744728476e81cc239fbe9
1 file changed
+25
-13
src/telegram.js
+25
-13
@@ -7,20 +7,38 @@ export class TelegramClient {
7
8
async callApi(method, payload) {
9
if (!this.botToken) {
10
- // In mock/test environment without token
10
return { ok: true, result: { message_id: 1001, ...payload } };
11
}
12
13
+ let response;
14
try {
15
- const response = await this.fetchFn(`${this.baseUrl}/${method}`, {
15
+ response = await this.fetchFn(`${this.baseUrl}/${method}`, {
16
method: 'POST',
17
headers: { 'Content-Type': 'application/json' },
18
body: JSON.stringify(payload)
19
});
20
- return await response.json();
20
} catch (err) {
22
- return { ok: false, description: err.message };
21
+ throw new Error(`Telegram API ${method} network failure: ${err.message}`);
22
}
23
+
24
+ let body;
25
+ try {
26
+ body = await response.json();
27
+ } catch {
28
+ body = null;
29
+ }
30
+
31
+ if (!response.ok) {
32
+ const detail = body?.description ? ` ${body.description}` : '';
33
+ throw new Error(`Telegram API ${method} failed: ${response.status}${detail}`);
34
+ }
35
+
36
+ if (!body || body.ok !== true) {
37
+ const detail = body?.description || 'Unknown Telegram API error';
38
+ throw new Error(`Telegram API ${method} failed: ${detail}`);
39
+ }
40
+
41
+ return body;
42
}
43
44
async sendMessage(chatId, text, replyMarkup = null, parseMode = 'HTML') {
@@ -29,9 +47,7 @@ export class TelegramClient {
47
text,
48
parse_mode: parseMode
49
};
32
- if (replyMarkup) {
33
- payload.reply_markup = replyMarkup;
34
- }
50
+ if (replyMarkup) payload.reply_markup = replyMarkup;
51
return this.callApi('sendMessage', payload);
52
}
53
@@ -42,16 +58,12 @@ export class TelegramClient {
58
text,
59
parse_mode: parseMode
60
};
45
- if (replyMarkup) {
46
- payload.reply_markup = replyMarkup;
47
- }
61
+ if (replyMarkup) payload.reply_markup = replyMarkup;
62
return this.callApi('editMessageText', payload);
63
}
64
65
async answerCallbackQuery(callbackQueryId, text = '', showAlert = false) {
52
- const payload = {
53
- callback_query_id: callbackQueryId
54
- };
66
+ const payload = { callback_query_id: callbackQueryId };
67
if (text) payload.text = text;
68
if (showAlert) payload.show_alert = showAlert;
69
return this.callApi('answerCallbackQuery', payload);