@aracna/telegram-bot 1.9.10 → 1.9.12

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.
@@ -10,7 +10,7 @@ class Download extends child_1.Child {
10
10
  file = await this.telegram.get.file(id);
11
11
  if (file instanceof Error)
12
12
  return file;
13
- buffer = await this.api.get(file.file_path || '');
13
+ buffer = await this.api.get(file.file_path ?? '');
14
14
  if (buffer instanceof Error)
15
15
  return buffer;
16
16
  return buffer.data;
@@ -3,7 +3,7 @@ import { Child } from '../modules/child';
3
3
  export declare class Polling extends Child {
4
4
  active: boolean;
5
5
  offset: number;
6
- start(ms: number | undefined, parameters: Partial<GetUpdates>): void;
6
+ start(ms?: number, parameters?: Partial<GetUpdates>): void;
7
7
  stop(): void;
8
8
  private get;
9
9
  }
@@ -1,11 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.REGEXP_COMMAND_WITH_USERNAME = exports.REGEXP_COMMAND = void 0;
4
- // export const REGEXP_ARRAY: RegExp = /^\[.+\]$/
5
4
  exports.REGEXP_COMMAND = /\/[a-z_]+/m;
6
5
  exports.REGEXP_COMMAND_WITH_USERNAME = /\/[a-zA-Z0-9_@]+/;
7
- // export const REGEXP_ID: RegExp = /^[a-z0-9]{16}$/
8
- // export const REGEXP_NUMBER: RegExp = /^(\+|-)?[0-9]+(\.[0-9])?[0-9]*$/
9
- // export const REGEXP_OBJECT: RegExp = /^\{.+\}$/
10
- // export const REGEXP_REPLY_TO_MESSAGE_CHAT_ID: RegExp = /:\s(\+|-)?[0-9]+/m
11
- // export const REGEXP_USERNAME: RegExp = /[a-zA-Z_0-9]{5,32}/
package/index.d.ts CHANGED
@@ -80,12 +80,6 @@ import { Update } from '@aracna/telegram-bot-types';
80
80
  import { User } from '@aracna/telegram-bot-types';
81
81
  import { UserProfilePhotos } from '@aracna/telegram-bot-types';
82
82
 
83
- declare class _ {
84
- api: ConfigurationAPI;
85
- default: ConfigurationDefault;
86
- handler: ConfigurationHandler;
87
- }
88
-
89
83
  declare class Add extends Child {
90
84
  stickerToSet(userID: number, name: string, sticker: InputSticker, parameters: Partial<AddStickerToSet>): Promise<Message | FetchError>;
91
85
  }
@@ -155,6 +149,12 @@ declare interface ConfigurationAPI {
155
149
  };
156
150
  }
157
151
 
152
+ declare class ConfigurationBuilder {
153
+ api: ConfigurationAPI;
154
+ default: ConfigurationDefault;
155
+ handler: ConfigurationHandler;
156
+ }
157
+
158
158
  declare interface ConfigurationDefault {
159
159
  buttons: {
160
160
  text: (chatID: number) => Promise<InlineKeyboardButton[]>;
@@ -303,7 +303,7 @@ declare class Pin extends Child {
303
303
  declare class Polling extends Child {
304
304
  active: boolean;
305
305
  offset: number;
306
- start(ms: number | undefined, parameters: Partial<GetUpdates>): void;
306
+ start(ms?: number, parameters?: Partial<GetUpdates>): void;
307
307
  stop(): void;
308
308
  private get;
309
309
  }
@@ -446,7 +446,7 @@ export declare class Telegram {
446
446
  private get handlerIDs();
447
447
  }
448
448
 
449
- export declare const TelegramConfiguration: _;
449
+ export declare const TelegramConfiguration: ConfigurationBuilder;
450
450
 
451
451
  export declare enum TelegramLoggerName {
452
452
  MODULE = "TELEGRAM_MODULE"
@@ -1,8 +1,8 @@
1
1
  import { ConfigurationAPI, ConfigurationDefault, ConfigurationHandler } from '../definitions/interfaces';
2
- declare class _ {
2
+ declare class ConfigurationBuilder {
3
3
  api: ConfigurationAPI;
4
4
  default: ConfigurationDefault;
5
5
  handler: ConfigurationHandler;
6
6
  }
7
- export declare const Configuration: _;
7
+ export declare const Configuration: ConfigurationBuilder;
8
8
  export {};
@@ -2,9 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Configuration = void 0;
4
4
  const dummy_1 = require("./dummy");
5
- class _ {
5
+ class ConfigurationBuilder {
6
6
  api = dummy_1.Dummy.configurationAPI;
7
7
  default = dummy_1.Dummy.configurationDefault;
8
8
  handler = dummy_1.Dummy.configurationHandler;
9
9
  }
10
- exports.Configuration = new _();
10
+ exports.Configuration = new ConfigurationBuilder();
@@ -105,8 +105,8 @@ class Telegram {
105
105
  return;
106
106
  this.id = v.id;
107
107
  this.name.first = v.first_name;
108
- this.name.last = v.last_name || '';
109
- this.username = v.username || '';
108
+ this.name.last = v.last_name ?? '';
109
+ this.username = v.username ?? '';
110
110
  });
111
111
  }
112
112
  on(type, middleware, key, description, options) {
@@ -117,9 +117,14 @@ class Telegram {
117
117
  handler.key = key;
118
118
  handler.middleware = middleware;
119
119
  handler.type = type;
120
- handler.options = (0, core_1.mergeObjects)(handler.options, options || {});
120
+ handler.options = (0, core_1.mergeObjects)(handler.options, options ?? {});
121
121
  potential = this.findMatchingHandler(handler.type, handler.key);
122
- potential.id ? (potential.middleware = middleware) : this.handlers.push(handler);
122
+ if (potential.id) {
123
+ potential.middleware = middleware;
124
+ module_logger_1.ModuleLogger.debug('Telegram', 'register', `The handler has been updated.`, handler);
125
+ return;
126
+ }
127
+ this.handlers.push(handler);
123
128
  module_logger_1.ModuleLogger.debug('Telegram', 'register', `The handler has been registered.`, handler);
124
129
  }
125
130
  handle(update) {
@@ -305,7 +310,7 @@ class Telegram {
305
310
  }
306
311
  handleReplyToMessage(reply) {
307
312
  let body, handler;
308
- body = reply_to_message_utils_1.ReplyToMessageUtils.decodeBody(reply.reply_to_message?.entities || []);
313
+ body = reply_to_message_utils_1.ReplyToMessageUtils.decodeBody(reply.reply_to_message?.entities ?? []);
309
314
  (0, core_1.setObjectProperty)(reply, 'body', body);
310
315
  handler = this.findMatchingHandler(enums_1.UpdateType.REPLY_TO_MESSAGE, body.type);
311
316
  if (!handler.id)
@@ -334,17 +339,17 @@ class Telegram {
334
339
  return handler;
335
340
  handler.middleware(start);
336
341
  if (handler.id.length > 0 && handler.options.deleteOnMessageStart) {
337
- this.delete.message(body.chatID ? start.from?.id || 0 : start.chat.id, start.message_id);
342
+ this.delete.message(body.chatID ? start.from?.id ?? 0 : start.chat.id, start.message_id);
338
343
  }
339
344
  return handler;
340
345
  }
341
346
  findMatchingHandler(type, key = '') {
342
- return this.handlers.find((v) => v.key === key && v.type === type) || dummy_1.Dummy.handler;
347
+ return this.handlers.find((v) => v.key === key && v.type === type) ?? dummy_1.Dummy.handler;
343
348
  }
344
349
  get commands() {
345
350
  return this.handlers
346
351
  .filter((v) => v.key && v.type === enums_1.UpdateType.MESSAGE)
347
- .map((v) => ({ command: v.key || '', description: v.description || '' }));
352
+ .map((v) => ({ command: v.key ?? '', description: v.description ?? '' }));
348
353
  }
349
354
  get handlerIDs() {
350
355
  return this.handlers.reduce((r, v) => [...r, v.id], []);
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "author": "Dario Sechi",
3
3
  "devDependencies": {
4
- "@aracna/core": "^1.1.75",
5
- "@aracna/telegram-bot-types": "^1.2.9",
4
+ "@aracna/core": "^1.1.77",
5
+ "@aracna/telegram-bot-types": "^1.2.10",
6
6
  "@microsoft/api-extractor": "^7.39.0",
7
7
  "@types/node": "^20.10.5",
8
8
  "@vitest/coverage-v8": "^1.1.0",
@@ -12,6 +12,10 @@
12
12
  "typescript": "^5.3.3",
13
13
  "vitest": "^1.1.0"
14
14
  },
15
+ "engines": {
16
+ "node": ">=16",
17
+ "pnpm": ">=8"
18
+ },
15
19
  "license": "MIT",
16
20
  "main": "index.js",
17
21
  "name": "@aracna/telegram-bot",
@@ -23,7 +27,7 @@
23
27
  "directory": "dist"
24
28
  },
25
29
  "types": "index.d.ts",
26
- "version": "1.9.10",
30
+ "version": "1.9.12",
27
31
  "scripts": {
28
32
  "build": "rm -rf dist && pnpm tsc && pnpm api-extractor run --local",
29
33
  "prepublish": "pnpm test && pnpm build && pnpm version patch && cp LICENSE package.json README.md dist",
@@ -20,7 +20,7 @@ class CommandUtils {
20
20
  return this.get(string);
21
21
  }
22
22
  static get(string) {
23
- return (constants_1.REGEXP_COMMAND.exec(string?.slice(0, 512) || '') || [''])[0].slice(1);
23
+ return (constants_1.REGEXP_COMMAND.exec(string?.slice(0, 512) ?? '') ?? [''])[0].slice(1);
24
24
  }
25
25
  static omit(string) {
26
26
  return string.replace(constants_1.REGEXP_COMMAND_WITH_USERNAME, '').trim();
@@ -26,18 +26,16 @@ class ContextUtils {
26
26
  return this.getUser(context).first_name;
27
27
  }
28
28
  static getUserLastName(context) {
29
- return this.getUser(context).last_name || '';
29
+ return this.getUser(context).last_name ?? '';
30
30
  }
31
31
  static getUserUsername(context) {
32
- return this.getUser(context).username || '';
32
+ return this.getUser(context).username ?? '';
33
33
  }
34
34
  static getUser(context) {
35
- switch (true) {
36
- case (0, core_1.hasObjectProperty)(context, 'from'):
37
- return (0, core_1.getObjectProperty)(context, 'from', { first_name: '', id: 0, is_bot: false, username: '' });
38
- default:
39
- return { first_name: '', id: 0, is_bot: false, username: '' };
35
+ if ((0, core_1.hasObjectProperty)(context, 'from')) {
36
+ return (0, core_1.getObjectProperty)(context, 'from', { first_name: '', id: 0, is_bot: false, username: '' });
40
37
  }
38
+ return { first_name: '', id: 0, is_bot: false, username: '' };
41
39
  }
42
40
  }
43
41
  exports.ContextUtils = ContextUtils;
@@ -7,7 +7,7 @@ class ReplyToMessageUtils {
7
7
  static decodeBody(entities) {
8
8
  let entity, encoded, body;
9
9
  entity = entities[entities.length - 1];
10
- if (!entity || !entity.url)
10
+ if (!entity?.url)
11
11
  return dummy_1.Dummy.messageBody;
12
12
  encoded = entity.url.replace('https://t.me/?a=', '');
13
13
  body = (0, core_1.tc)(() => JSON.parse(Buffer.from(encoded, 'base64').toString()));