@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.
@@ -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
- return new Response('No file_id provided', { status: 400 });
65
+ throw new Error('No file_id provided');
63
66
  }
64
- try {
65
- const url = this.getApiUrl(botApi, 'getFile', data);
66
- const response = await this.fetchAndLog(url, 'getFile', data);
67
- if (!response.ok) {
68
- return new Response(`API error: ${String(response.status)} ${response.statusText}`, { status: response.status });
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
- catch (e) {
77
- console.error(`Error in getFile: ${e instanceof Error ? e.message : String(e)}`);
78
- return new Response(`Error retrieving file: ${e instanceof Error ? e.message : String(e)}`, { status: 500 });
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();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "9.3.3",
3
+ "version": "9.4.0",
4
4
  "description": "serverless telegram bot on cf workers",
5
5
  "main": "./dist/main.js",
6
6
  "module": "./dist/main.js",