@codebam/cf-workers-telegram-bot 9.3.3 → 9.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/telegram_api.js +13 -15
- package/dist/webhook.js +6 -0
- package/package.json +1 -1
package/dist/telegram_api.js
CHANGED
|
@@ -26,6 +26,9 @@ export default class TelegramApi {
|
|
|
26
26
|
*/
|
|
27
27
|
async fetchAndLog(url, slug, data) {
|
|
28
28
|
const response = await fetch(url);
|
|
29
|
+
if (response.status !== 200) {
|
|
30
|
+
throw new Error(`Telegram API error: ${String(response.status)} ${response.statusText}`);
|
|
31
|
+
}
|
|
29
32
|
const cloned = response.clone();
|
|
30
33
|
try {
|
|
31
34
|
const json = await cloned.json();
|
|
@@ -59,24 +62,19 @@ export default class TelegramApi {
|
|
|
59
62
|
*/
|
|
60
63
|
async getFile(botApi, data, token) {
|
|
61
64
|
if (!data.file_id || data.file_id === '') {
|
|
62
|
-
|
|
65
|
+
throw new Error('No file_id provided');
|
|
63
66
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
const json = await response.json();
|
|
71
|
-
if (!json.ok || !json.result?.file_path) {
|
|
72
|
-
return new Response(json.description ?? 'Failed to get file path', { status: 400 });
|
|
73
|
-
}
|
|
74
|
-
return await fetch(`https://api.telegram.org/file/bot${token}/${json.result.file_path}`);
|
|
67
|
+
const url = this.getApiUrl(botApi, 'getFile', data);
|
|
68
|
+
const response = await this.fetchAndLog(url, 'getFile', data);
|
|
69
|
+
const json = await response.json();
|
|
70
|
+
if (!json.ok || !json.result?.file_path) {
|
|
71
|
+
throw new Error(json.description ?? 'Failed to get file path');
|
|
75
72
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
73
|
+
const fileResponse = await fetch(`https://api.telegram.org/file/bot${token}/${json.result.file_path}`);
|
|
74
|
+
if (fileResponse.status !== 200) {
|
|
75
|
+
throw new Error(`Telegram File API error: ${String(fileResponse.status)} ${fileResponse.statusText}`);
|
|
79
76
|
}
|
|
77
|
+
return fileResponse;
|
|
80
78
|
}
|
|
81
79
|
/**
|
|
82
80
|
* Send a message to a given botApi
|
package/dist/webhook.js
CHANGED
|
@@ -35,6 +35,9 @@ export default class Webhook {
|
|
|
35
35
|
});
|
|
36
36
|
try {
|
|
37
37
|
const response = await fetch(`${url.toString()}?${params.toString()}`);
|
|
38
|
+
if (response.status !== 200) {
|
|
39
|
+
throw new Error(`Telegram API error (setWebhook): ${String(response.status)} ${response.statusText}`);
|
|
40
|
+
}
|
|
38
41
|
const cloned = response.clone();
|
|
39
42
|
try {
|
|
40
43
|
const json = await cloned.json();
|
|
@@ -65,6 +68,9 @@ export default class Webhook {
|
|
|
65
68
|
const url = new URL(`${baseUrl}${baseUrl.endsWith('/') ? '' : '/'}deleteWebhook`);
|
|
66
69
|
try {
|
|
67
70
|
const response = await fetch(url.toString());
|
|
71
|
+
if (response.status !== 200) {
|
|
72
|
+
throw new Error(`Telegram API error (deleteWebhook): ${String(response.status)} ${response.statusText}`);
|
|
73
|
+
}
|
|
68
74
|
const cloned = response.clone();
|
|
69
75
|
try {
|
|
70
76
|
const json = await cloned.json();
|