@aracna/telegram-bot 2.0.3 → 2.0.5

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.
Files changed (79) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +18 -18
  3. package/apis/telegram-api.d.ts +7 -5
  4. package/apis/telegram-api.js +13 -4
  5. package/apis/telegram-file-api.d.ts +6 -4
  6. package/apis/telegram-file-api.js +12 -2
  7. package/classes/client.js +7 -7
  8. package/definitions/interfaces.d.ts +12 -0
  9. package/index.d.ts +150 -152
  10. package/package.json +17 -17
  11. package/requests/add-requests.d.ts +2 -1
  12. package/requests/add-requests.js +2 -2
  13. package/requests/answer-requests.d.ts +6 -5
  14. package/requests/answer-requests.js +10 -10
  15. package/requests/approve-requests.d.ts +2 -1
  16. package/requests/approve-requests.js +2 -2
  17. package/requests/ban-requests.d.ts +3 -2
  18. package/requests/ban-requests.js +4 -4
  19. package/requests/close-requests.d.ts +3 -2
  20. package/requests/close-requests.js +4 -4
  21. package/requests/copy-requests.d.ts +3 -2
  22. package/requests/copy-requests.js +4 -4
  23. package/requests/create-requests.d.ts +6 -5
  24. package/requests/create-requests.js +10 -10
  25. package/requests/decline-requests.d.ts +2 -1
  26. package/requests/decline-requests.js +2 -2
  27. package/requests/delete-requests.d.ts +9 -8
  28. package/requests/delete-requests.js +16 -16
  29. package/requests/download-requests.d.ts +3 -2
  30. package/requests/download-requests.js +6 -6
  31. package/requests/edit-requests.d.ts +10 -9
  32. package/requests/edit-requests.js +18 -18
  33. package/requests/export-requests.d.ts +2 -1
  34. package/requests/export-requests.js +2 -2
  35. package/requests/forward-requests.d.ts +3 -2
  36. package/requests/forward-requests.js +4 -4
  37. package/requests/get-requests.d.ts +22 -21
  38. package/requests/get-requests.js +42 -42
  39. package/requests/hide-requests.d.ts +2 -1
  40. package/requests/hide-requests.js +2 -2
  41. package/requests/leave-requests.d.ts +2 -1
  42. package/requests/leave-requests.js +2 -2
  43. package/requests/log-requests.d.ts +2 -1
  44. package/requests/log-requests.js +2 -2
  45. package/requests/pin-requests.d.ts +2 -1
  46. package/requests/pin-requests.js +2 -2
  47. package/requests/promote-requests.d.ts +2 -1
  48. package/requests/promote-requests.js +2 -2
  49. package/requests/refund-requests.d.ts +2 -1
  50. package/requests/refund-requests.js +2 -2
  51. package/requests/reopen-requests.d.ts +3 -2
  52. package/requests/reopen-requests.js +4 -4
  53. package/requests/replace-requests.d.ts +2 -1
  54. package/requests/replace-requests.js +2 -2
  55. package/requests/restrict-requests.d.ts +2 -1
  56. package/requests/restrict-requests.js +2 -2
  57. package/requests/revoke-requests.d.ts +2 -1
  58. package/requests/revoke-requests.js +2 -2
  59. package/requests/send-requests.d.ts +22 -22
  60. package/requests/send-requests.js +44 -44
  61. package/requests/set-requests.d.ts +23 -22
  62. package/requests/set-requests.js +44 -44
  63. package/requests/stop-requests.d.ts +3 -2
  64. package/requests/stop-requests.js +4 -4
  65. package/requests/unban-requests.d.ts +3 -2
  66. package/requests/unban-requests.js +4 -4
  67. package/requests/unhide-requests.d.ts +2 -1
  68. package/requests/unhide-requests.js +2 -2
  69. package/requests/unpin-requests.d.ts +5 -4
  70. package/requests/unpin-requests.js +8 -8
  71. package/requests/upload-requests.d.ts +2 -1
  72. package/requests/upload-requests.js +2 -2
  73. package/requests/webhook-requests.d.ts +5 -4
  74. package/requests/webhook-requests.js +8 -8
  75. package/utils/reply-to-message-utils.d.ts +3 -3
  76. package/utils/reply-to-message-utils.js +1 -1
  77. package/utils/start-message-utils.d.ts +3 -3
  78. package/definitions/telegram-api-definitions.d.ts +0 -12
  79. package/definitions/telegram-api-definitions.js +0 -2
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 Dario Sechi
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Dario Sechi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,18 +1,18 @@
1
- ![NPM](https://img.shields.io/npm/l/@aracna/telegram-bot)
2
- ![npm (scoped)](https://img.shields.io/npm/v/@aracna/telegram-bot)
3
- [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=aracna_telegram-bot&metric=coverage)](https://sonarcloud.io/summary/new_code?id=aracna_telegram-bot)
4
- [![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=aracna_telegram-bot&metric=ncloc)](https://sonarcloud.io/summary/new_code?id=aracna_telegram-bot)
5
- [![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=aracna_telegram-bot&metric=duplicated_lines_density)](https://sonarcloud.io/summary/new_code?id=aracna_telegram-bot)
6
- [![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=aracna_telegram-bot&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=aracna_telegram-bot)
7
- [![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=aracna_telegram-bot&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=aracna_telegram-bot)
8
- [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=aracna_telegram-bot&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=aracna_telegram-bot)
9
- [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=aracna_telegram-bot&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=aracna_telegram-bot)
10
- [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=aracna_telegram-bot&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=aracna_telegram-bot)
11
-
12
- # Aracna Telegram Bot
13
-
14
- This library makes building Telegram Bot back-ends a breeze by abstracting all the tedious parts and exposing a simple API to work with.
15
-
16
- ### Documentation
17
-
18
- You can find the telegram bot documentation on the [Aracna Website](https://aracna.dariosechi.it).
1
+ ![NPM](https://img.shields.io/npm/l/@aracna/telegram-bot)
2
+ ![npm (scoped)](https://img.shields.io/npm/v/@aracna/telegram-bot)
3
+ [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=aracna_telegram-bot&metric=coverage)](https://sonarcloud.io/summary/new_code?id=aracna_telegram-bot)
4
+ [![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=aracna_telegram-bot&metric=ncloc)](https://sonarcloud.io/summary/new_code?id=aracna_telegram-bot)
5
+ [![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=aracna_telegram-bot&metric=duplicated_lines_density)](https://sonarcloud.io/summary/new_code?id=aracna_telegram-bot)
6
+ [![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=aracna_telegram-bot&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=aracna_telegram-bot)
7
+ [![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=aracna_telegram-bot&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=aracna_telegram-bot)
8
+ [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=aracna_telegram-bot&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=aracna_telegram-bot)
9
+ [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=aracna_telegram-bot&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=aracna_telegram-bot)
10
+ [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=aracna_telegram-bot&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=aracna_telegram-bot)
11
+
12
+ # Aracna Telegram Bot
13
+
14
+ This library makes building Telegram Bot back-ends a breeze by abstracting all the tedious parts and exposing a simple API to work with.
15
+
16
+ ### Documentation
17
+
18
+ You can find the telegram bot documentation on the [Aracna Website](https://aracna.dariosechi.it).
@@ -1,9 +1,11 @@
1
- import { type FetchError, Queue, type RequestMethod, RestAPI } from '@aracna/core';
2
- import { TelegramApiDefinitions } from '../definitions/telegram-api-definitions';
3
- declare class API extends RestAPI<TelegramApiDefinitions.Config> {
1
+ import { FetchError, Queue, type RequestMethod, RestAPI } from '@aracna/core';
2
+ import { TelegramApiConfig } from '../definitions/interfaces';
3
+ declare class API extends RestAPI<TelegramApiConfig> {
4
4
  queue: Queue;
5
- post<V, W, X = undefined>(path: string, body: W | undefined, config: TelegramApiDefinitions.Config): Promise<V | FetchError<X>>;
6
- transformBody<V>(method: RequestMethod, path: string, body: V, config: TelegramApiDefinitions.Config): Promise<V | FormData | undefined>;
5
+ post<V, W, X = undefined>(path: string, body: W | undefined, config?: TelegramApiConfig): Promise<V | FetchError<X>>;
6
+ transformBody<V>(method: RequestMethod, path: string, body: V, config: TelegramApiConfig): Promise<V | FormData | undefined>;
7
+ getToken(): string | undefined;
8
+ setToken(token?: string): this;
7
9
  }
8
10
  export declare const TelegramAPI: API;
9
11
  export {};
@@ -5,11 +5,14 @@ const core_1 = require("@aracna/core");
5
5
  class API extends core_1.RestAPI {
6
6
  queue = new core_1.Queue({ autostart: true });
7
7
  async post(path, body, config) {
8
- let fn, callback, promise;
8
+ let token, promise, fn, callback;
9
+ token = config?.token ?? this.config?.token;
10
+ if (!token)
11
+ return core_1.FetchError.from(new Error(`The token is required.`));
9
12
  promise = new core_1.DeferredPromise();
10
13
  fn = async () => {
11
14
  let response;
12
- response = await super.post('/bot' + config.token + '/' + path, body, config);
15
+ response = await super.post('/bot' + token + '/' + path, body, config);
13
16
  if (response instanceof Error)
14
17
  return response;
15
18
  return response.data.result;
@@ -38,6 +41,13 @@ class API extends core_1.RestAPI {
38
41
  }
39
42
  return body;
40
43
  }
44
+ getToken() {
45
+ return this.config.token;
46
+ }
47
+ setToken(token) {
48
+ this.config.token = token;
49
+ return this;
50
+ }
41
51
  }
42
52
  exports.TelegramAPI = new API('https://api.telegram.org', {
43
53
  decode: {
@@ -50,6 +60,5 @@ exports.TelegramAPI = new API('https://api.telegram.org', {
50
60
  json: {
51
61
  castBigIntToString: true
52
62
  }
53
- },
54
- token: undefined
63
+ }
55
64
  });
@@ -1,7 +1,9 @@
1
- import { type FetchError, RestAPI } from '@aracna/core';
2
- import { TelegramApiDefinitions } from '../definitions/telegram-api-definitions';
3
- declare class API extends RestAPI<TelegramApiDefinitions.Config> {
4
- get<V, W = undefined>(path: string, config: TelegramApiDefinitions.Config): Promise<V | FetchError<W>>;
1
+ import { FetchError, RestAPI } from '@aracna/core';
2
+ import type { TelegramFileApiConfig } from '../definitions/interfaces';
3
+ declare class API extends RestAPI<TelegramFileApiConfig> {
4
+ get<V, W = undefined>(path: string, config?: TelegramFileApiConfig): Promise<V | FetchError<W>>;
5
+ getToken(): string | undefined;
6
+ setToken(token?: string): this;
5
7
  }
6
8
  export declare const TelegramFileAPI: API;
7
9
  export {};
@@ -4,11 +4,21 @@ exports.TelegramFileAPI = void 0;
4
4
  const core_1 = require("@aracna/core");
5
5
  class API extends core_1.RestAPI {
6
6
  async get(path, config) {
7
- let response;
8
- response = await super.get('/file/bot' + config.token + '/' + path, config);
7
+ let token, response;
8
+ token = config?.token ?? this.config?.token;
9
+ if (!token)
10
+ return core_1.FetchError.from(new Error(`The token is required.`));
11
+ response = await super.get('/file/bot' + token + '/' + path, config);
9
12
  if (response instanceof Error)
10
13
  return response;
11
14
  return response.data;
12
15
  }
16
+ getToken() {
17
+ return this.config.token;
18
+ }
19
+ setToken(token) {
20
+ this.config.token = token;
21
+ return this;
22
+ }
13
23
  }
14
24
  exports.TelegramFileAPI = new API('https://api.telegram.org');
package/classes/client.js CHANGED
@@ -32,11 +32,11 @@ class Client {
32
32
  if (!options?.webhook?.url) {
33
33
  return new Error(`The webhook URL is required.`);
34
34
  }
35
- del = await (0, webhook_requests_1.deleteWebhook)(this.token, options.webhook.delete);
35
+ del = await (0, webhook_requests_1.deleteWebhook)(options.webhook.delete, { token: this.token });
36
36
  if (del instanceof Error)
37
37
  return del;
38
38
  class_logger_1.ClassLogger.verbose('Telegram', 'connect', `The webhook has been deleted.`);
39
- set = await (0, webhook_requests_1.setWebhook)(this.token, options.webhook);
39
+ set = await (0, webhook_requests_1.setWebhook)(options.webhook, { token: this.token });
40
40
  if (set instanceof Error)
41
41
  return set;
42
42
  class_logger_1.ClassLogger.info('Telegram', 'connect', `The webhook has been set.`);
@@ -49,7 +49,7 @@ class Client {
49
49
  class_logger_1.ClassLogger.verbose('Telegram', 'disconnect', `The offset has been reset.`, [this.offset]);
50
50
  (0, core_1.clearInterval)(this.id);
51
51
  class_logger_1.ClassLogger.info('Telegram', 'disconnect', `The client has been disconnected.`);
52
- await (0, webhook_requests_1.deleteWebhook)(this.token, options?.webhook);
52
+ await (0, webhook_requests_1.deleteWebhook)(options?.webhook, { token: this.token });
53
53
  class_logger_1.ClassLogger.info('Telegram', 'disconnect', `The webhook has been deleted.`);
54
54
  }
55
55
  on(type, middleware, options) {
@@ -180,7 +180,7 @@ class Client {
180
180
  return;
181
181
  listener.middleware(query);
182
182
  if (listener.options.deleteOnCallbackQuery && query.message) {
183
- (0, delete_requests_1.deleteMessage)(this.token, { chat_id: body.c ? query.from.id : query.message.chat.id, message_id: query.message.message_id });
183
+ (0, delete_requests_1.deleteMessage)({ chat_id: body.c ? query.from.id : query.message.chat.id, message_id: query.message.message_id }, { token: this.token });
184
184
  }
185
185
  return listener;
186
186
  }
@@ -337,7 +337,7 @@ class Client {
337
337
  return;
338
338
  listener.middleware(reply);
339
339
  if (listener.id.length > 0 && listener.options.deleteOnReply && reply.from && reply.reply_to_message) {
340
- (0, delete_requests_1.deleteMessages)(this.token, { chat_id: reply.from.id, message_ids: [reply.message_id, reply.reply_to_message.message_id] });
340
+ (0, delete_requests_1.deleteMessages)({ chat_id: reply.from.id, message_ids: [reply.message_id, reply.reply_to_message.message_id] }, { token: this.token });
341
341
  }
342
342
  return listener;
343
343
  }
@@ -358,7 +358,7 @@ class Client {
358
358
  return;
359
359
  listener.middleware(start);
360
360
  if (listener.id.length > 0 && listener.options.deleteOnMessageStart) {
361
- (0, delete_requests_1.deleteMessage)(this.token, { chat_id: body.c ? start.from?.id ?? 0n : start.chat.id, message_id: start.message_id });
361
+ (0, delete_requests_1.deleteMessage)({ chat_id: body.c ? start.from?.id ?? 0n : start.chat.id, message_id: start.message_id }, { token: this.token });
362
362
  }
363
363
  return listener;
364
364
  }
@@ -370,7 +370,7 @@ class Client {
370
370
  offset: options?.offset ?? this.offset,
371
371
  timeout: options?.timeout
372
372
  };
373
- updates = await (0, get_requests_1.getUpdates)(this.token, body);
373
+ updates = await (0, get_requests_1.getUpdates)(body, { token: this.token });
374
374
  if (updates instanceof Error)
375
375
  return;
376
376
  for (let update of updates) {
@@ -1,3 +1,4 @@
1
+ import type { FetchResponse, RestApiConfig } from '@aracna/core';
1
2
  import type { BusinessConnection, BusinessMessagesDeleted, ChatBoostRemoved, ChatBoostUpdated, ChatJoinRequest, ChatMemberUpdated, ChosenInlineResult, DeleteWebhook, EditMessageMedia, GetUpdates, InlineQuery, InputMediaAudio, InputMediaDocument, InputMediaPhoto, InputMediaVideo, Message, MessageReactionCountUpdated, MessageReactionUpdated, CallbackQuery as NativeCallbackQuery, Poll, PollAnswer, PreCheckoutQuery, SendMediaGroup, SendMessage, SendPaidMedia, SetWebhook, ShippingQuery } from '@aracna/telegram-bot-types';
2
3
  import type { ClientListenerMiddleware, InputFile, UpdateType } from './types';
3
4
  export interface CallbackQuery<T = any> extends NativeCallbackQuery {
@@ -114,3 +115,14 @@ export interface StartBody<T = any> {
114
115
  d?: T;
115
116
  m?: string;
116
117
  }
118
+ export interface TelegramApiConfig extends RestApiConfig {
119
+ token?: string;
120
+ }
121
+ export interface TelegramApiResponse<T> extends FetchResponse<TelegramApiResponseData<T>> {
122
+ }
123
+ export interface TelegramApiResponseData<T> {
124
+ ok: boolean;
125
+ result: T;
126
+ }
127
+ export interface TelegramFileApiConfig extends TelegramApiConfig {
128
+ }