chz-telegram-bot 0.3.24 → 0.3.26

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 (47) hide show
  1. package/builtin/helpAction.ts +1 -1
  2. package/dist/builtin/helpAction.js +1 -1
  3. package/dist/dtos/cooldownInfo.d.ts +6 -6
  4. package/dist/dtos/cooldownInfo.d.ts.map +1 -1
  5. package/dist/dtos/cooldownInfo.js +4 -4
  6. package/dist/dtos/propertyProviderSets.d.ts +16 -0
  7. package/dist/dtos/propertyProviderSets.d.ts.map +1 -0
  8. package/dist/dtos/propertyProviderSets.js +2 -0
  9. package/dist/entities/actions/commandAction.d.ts +12 -12
  10. package/dist/entities/actions/commandAction.d.ts.map +1 -1
  11. package/dist/entities/actions/commandAction.js +33 -26
  12. package/dist/entities/actions/inlineQueryAction.d.ts +3 -2
  13. package/dist/entities/actions/inlineQueryAction.d.ts.map +1 -1
  14. package/dist/entities/actions/inlineQueryAction.js +4 -4
  15. package/dist/entities/actions/scheduledAction.d.ts +5 -5
  16. package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
  17. package/dist/entities/actions/scheduledAction.js +14 -13
  18. package/dist/helpers/builders/commandActionBuilder.d.ts +18 -7
  19. package/dist/helpers/builders/commandActionBuilder.d.ts.map +1 -1
  20. package/dist/helpers/builders/commandActionBuilder.js +35 -14
  21. package/dist/helpers/builders/inlineQueryActionBuilder.d.ts +8 -1
  22. package/dist/helpers/builders/inlineQueryActionBuilder.d.ts.map +1 -1
  23. package/dist/helpers/builders/inlineQueryActionBuilder.js +11 -3
  24. package/dist/helpers/builders/scheduledActionBuilder.d.ts +13 -4
  25. package/dist/helpers/builders/scheduledActionBuilder.d.ts.map +1 -1
  26. package/dist/helpers/builders/scheduledActionBuilder.js +24 -8
  27. package/dist/services/jsonLogger.d.ts +1 -0
  28. package/dist/services/jsonLogger.d.ts.map +1 -1
  29. package/dist/services/jsonLogger.js +19 -1
  30. package/dist/types/propertyProvider.d.ts +8 -0
  31. package/dist/types/propertyProvider.d.ts.map +1 -0
  32. package/dist/types/propertyProvider.js +2 -0
  33. package/dtos/cooldownInfo.ts +3 -3
  34. package/dtos/propertyProviderSets.ts +20 -0
  35. package/entities/actions/commandAction.ts +59 -36
  36. package/entities/actions/inlineQueryAction.ts +5 -4
  37. package/entities/actions/scheduledAction.ts +26 -15
  38. package/helpers/builders/commandActionBuilder.ts +57 -21
  39. package/helpers/builders/inlineQueryActionBuilder.ts +16 -3
  40. package/helpers/builders/scheduledActionBuilder.ts +36 -10
  41. package/package.json +44 -44
  42. package/services/jsonLogger.ts +23 -1
  43. package/types/propertyProvider.ts +14 -0
  44. package/dist/dtos/actionPermissionsData.d.ts +0 -7
  45. package/dist/dtos/actionPermissionsData.d.ts.map +0 -1
  46. package/dist/dtos/actionPermissionsData.js +0 -14
  47. package/dtos/actionPermissionsData.ts +0 -7
@@ -1,6 +1,7 @@
1
1
  import { InlineQueryHandler } from '../../types/handlers';
2
2
  import { Noop } from '../noop';
3
3
  import { InlineQueryAction } from '../../entities/actions/inlineQueryAction';
4
+ import { InlineActionPropertyProvider } from '../../types/propertyProvider';
4
5
 
5
6
  /**
6
7
  * Builder for `InlineQueryAction`
@@ -9,7 +10,7 @@ export class InlineQueryActionBuilder {
9
10
  private readonly name: string;
10
11
  private pattern: RegExp = /.+/gi;
11
12
 
12
- private active = true;
13
+ private activeProvider: InlineActionPropertyProvider<boolean> = () => true;
13
14
  private handler: InlineQueryHandler = Noop.call;
14
15
 
15
16
  /**
@@ -41,7 +42,19 @@ export class InlineQueryActionBuilder {
41
42
 
42
43
  /** If called during building, action is marked as disabled and never checked. */
43
44
  disabled() {
44
- this.active = false;
45
+ this.activeProvider = () => false;
46
+
47
+ return this;
48
+ }
49
+
50
+ /**
51
+ * Configures action to use property value providers instead of static value to allow changes in runtime
52
+ */
53
+ withConfiguration(configuration: {
54
+ isActiveProvider?: InlineActionPropertyProvider<boolean>;
55
+ }) {
56
+ if (configuration.isActiveProvider)
57
+ this.activeProvider = configuration.isActiveProvider;
45
58
 
46
59
  return this;
47
60
  }
@@ -51,7 +64,7 @@ export class InlineQueryActionBuilder {
51
64
  return new InlineQueryAction(
52
65
  this.handler,
53
66
  this.name,
54
- this.active,
67
+ this.activeProvider,
55
68
  this.pattern
56
69
  );
57
70
  }
@@ -3,6 +3,7 @@ import { CachedStateFactory } from '../../entities/cachedStateFactory';
3
3
  import { ActionStateBase } from '../../entities/states/actionStateBase';
4
4
  import { IActionState } from '../../types/actionState';
5
5
  import { ScheduledHandler } from '../../types/handlers';
6
+ import { ScheduledActionPropertyProvider } from '../../types/propertyProvider';
6
7
  import { Hours, HoursOfDay } from '../../types/timeValues';
7
8
  import { Noop } from '../noop';
8
9
 
@@ -12,17 +13,20 @@ import { Noop } from '../noop';
12
13
  export class ScheduledActionBuilderWithState<
13
14
  TActionState extends IActionState
14
15
  > {
15
- private active = true;
16
- private time: HoursOfDay = 0;
16
+ private readonly name: string;
17
17
  private readonly cachedStateFactories = new Map<
18
18
  string,
19
19
  CachedStateFactory
20
20
  >();
21
- private whitelist: number[] = [];
22
21
  private readonly stateConstructor: () => TActionState;
23
22
  private handler: ScheduledHandler<TActionState> = Noop.call;
24
23
 
25
- private readonly name: string;
24
+ private whitelistProvider: ScheduledActionPropertyProvider<number[]> =
25
+ () => [];
26
+ private activeProvider: ScheduledActionPropertyProvider<boolean> = () =>
27
+ true;
28
+ private timeinHoursProvider: ScheduledActionPropertyProvider<HoursOfDay> =
29
+ () => 0;
26
30
 
27
31
  /**
28
32
  * Builder for `ScheduledAction` with state represented by `TActionState`
@@ -39,7 +43,7 @@ export class ScheduledActionBuilderWithState<
39
43
  * @param chatIds Chat ids to execute in.
40
44
  */
41
45
  in(chatIds: number[]) {
42
- this.whitelist = chatIds;
46
+ this.whitelistProvider = () => chatIds;
43
47
 
44
48
  return this;
45
49
  }
@@ -49,7 +53,7 @@ export class ScheduledActionBuilderWithState<
49
53
  * @param time Time of day (0 - 23) to execute action.
50
54
  */
51
55
  runAt(time: HoursOfDay) {
52
- this.time = time;
56
+ this.timeinHoursProvider = () => time;
53
57
 
54
58
  return this;
55
59
  }
@@ -85,7 +89,27 @@ export class ScheduledActionBuilderWithState<
85
89
 
86
90
  /** If called during building, action is marked as disabled and never checked. */
87
91
  disabled() {
88
- this.active = false;
92
+ this.activeProvider = () => false;
93
+
94
+ return this;
95
+ }
96
+
97
+ /**
98
+ * Configures action to use property value providers instead of static value to allow changes in runtime
99
+ */
100
+ withConfiguration(configuration: {
101
+ timeinHoursProvider?: ScheduledActionPropertyProvider<HoursOfDay>;
102
+ isActiveProvider?: ScheduledActionPropertyProvider<boolean>;
103
+ chatsWhitelistProvider?: ScheduledActionPropertyProvider<number[]>;
104
+ }) {
105
+ if (configuration.chatsWhitelistProvider)
106
+ this.whitelistProvider = configuration.chatsWhitelistProvider;
107
+
108
+ if (configuration.timeinHoursProvider)
109
+ this.timeinHoursProvider = configuration.timeinHoursProvider;
110
+
111
+ if (configuration.isActiveProvider)
112
+ this.activeProvider = configuration.isActiveProvider;
89
113
 
90
114
  return this;
91
115
  }
@@ -95,9 +119,11 @@ export class ScheduledActionBuilderWithState<
95
119
  return new ScheduledAction<TActionState>(
96
120
  this.name,
97
121
  this.handler,
98
- this.time,
99
- this.active,
100
- this.whitelist,
122
+ {
123
+ chatsWhitelistProvider: this.whitelistProvider,
124
+ isActiveProvider: this.activeProvider,
125
+ timeinHoursProvider: this.timeinHoursProvider
126
+ },
101
127
  this.cachedStateFactories,
102
128
  this.stateConstructor
103
129
  );
package/package.json CHANGED
@@ -1,44 +1,44 @@
1
- {
2
- "name": "chz-telegram-bot",
3
- "description": "Opinionated TypeScript framework that provides a structured approach to building Telegram bots.",
4
- "author": {
5
- "name": "Alex Halanin",
6
- "url": "https://github.com/AlexSolari"
7
- },
8
- "license": "MIT",
9
- "keywords": [
10
- "telegram",
11
- "telegram bot"
12
- ],
13
- "repository": {
14
- "type": "git",
15
- "url": "https://github.com/AlexSolari/botFramework.git"
16
- },
17
- "version": "0.3.24",
18
- "type": "module",
19
- "dependencies": {
20
- "async-sema": "^3.1.1",
21
- "moment": "^2.29.4",
22
- "telegraf": "^4.16.3"
23
- },
24
- "main": "dist/index.js",
25
- "types": "dist/index.d.ts",
26
- "devDependencies": {
27
- "@eslint/js": "^9.29.0",
28
- "@types/markdown-escape": "^1.1.3",
29
- "@types/node": "^22.5.5",
30
- "eslint": "^9.29.0",
31
- "globals": "^16.2.0",
32
- "typescript": "^5.9.0-beta",
33
- "typescript-eslint": "^8.34.1"
34
- },
35
- "scripts": {
36
- "build": "tsc",
37
- "lint": "npx eslint && tsc --noEmit"
38
- },
39
- "overrides": {
40
- "telegraf": {
41
- "node-fetch": "3.3.2"
42
- }
43
- }
44
- }
1
+ {
2
+ "name": "chz-telegram-bot",
3
+ "description": "Opinionated TypeScript framework that provides a structured approach to building Telegram bots.",
4
+ "author": {
5
+ "name": "Alex Halanin",
6
+ "url": "https://github.com/AlexSolari"
7
+ },
8
+ "license": "MIT",
9
+ "keywords": [
10
+ "telegram",
11
+ "telegram bot"
12
+ ],
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/AlexSolari/botFramework.git"
16
+ },
17
+ "version": "0.3.26",
18
+ "type": "module",
19
+ "dependencies": {
20
+ "async-sema": "^3.1.1",
21
+ "moment": "^2.29.4",
22
+ "telegraf": "^4.16.3"
23
+ },
24
+ "main": "dist/index.js",
25
+ "types": "dist/index.d.ts",
26
+ "devDependencies": {
27
+ "@eslint/js": "^9.29.0",
28
+ "@types/markdown-escape": "^1.1.3",
29
+ "@types/node": "^22.5.5",
30
+ "eslint": "^9.29.0",
31
+ "globals": "^16.2.0",
32
+ "typescript": "^5.9.0-beta",
33
+ "typescript-eslint": "^8.34.1"
34
+ },
35
+ "scripts": {
36
+ "build": "tsc",
37
+ "lint": "npx eslint && tsc --noEmit"
38
+ },
39
+ "overrides": {
40
+ "telegraf": {
41
+ "node-fetch": "3.3.2"
42
+ }
43
+ }
44
+ }
@@ -12,6 +12,25 @@ export class JsonLogger implements ILogger {
12
12
  return JSON.stringify(plainObject);
13
13
  }
14
14
 
15
+ private getCircularReplacer() {
16
+ const ancestors: unknown[] = [];
17
+ return function <V>(this: V, _: unknown, value: V) {
18
+ if (typeof value !== 'object' || value === null) {
19
+ return value;
20
+ }
21
+ // `this` is the object that value is contained in,
22
+ // i.e., its direct parent.
23
+ while (ancestors.length > 0 && ancestors.at(-1) !== this) {
24
+ ancestors.pop();
25
+ }
26
+ if (ancestors.includes(value)) {
27
+ return '[Circular]';
28
+ }
29
+ ancestors.push(value);
30
+ return value;
31
+ };
32
+ }
33
+
15
34
  createScope(botName: string, traceId: TraceId, chatName: string) {
16
35
  return {
17
36
  logObjectWithTraceId: (data: any) => {
@@ -63,7 +82,10 @@ export class JsonLogger implements ILogger {
63
82
  extraData?: unknown
64
83
  ) {
65
84
  const dataString = extraData
66
- ? `,"extraData":${JSON.stringify(extraData)}`
85
+ ? `,"extraData":${JSON.stringify(
86
+ extraData,
87
+ this.getCircularReplacer()
88
+ )}`
67
89
  : '';
68
90
 
69
91
  console.error(
@@ -0,0 +1,14 @@
1
+ import { ChatContext } from '../entities/context/chatContext';
2
+ import { InlineQueryContext } from '../entities/context/inlineQueryContext';
3
+ import { MessageContext } from '../entities/context/messageContext';
4
+ import { IActionState } from './actionState';
5
+
6
+ export type CommandActionPropertyProvider<T> = (
7
+ ctx: MessageContext<IActionState>
8
+ ) => T;
9
+
10
+ export type InlineActionPropertyProvider<T> = (ctx: InlineQueryContext) => T;
11
+
12
+ export type ScheduledActionPropertyProvider<T> = (
13
+ ctx: ChatContext<IActionState>
14
+ ) => T;
@@ -1,7 +0,0 @@
1
- export declare class ActionPermissionsData {
2
- readonly userIdsWhitelist: number[];
3
- readonly chatIdsWhitelist: number[];
4
- readonly chatIdsBlacklist: number[];
5
- constructor(userIdsWhitelist: number[], chatIdsWhitelist: number[], chatIdsBlacklist: number[]);
6
- }
7
- //# sourceMappingURL=actionPermissionsData.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"actionPermissionsData.d.ts","sourceRoot":"","sources":["../../dtos/actionPermissionsData.ts"],"names":[],"mappings":"AAAA,qBAAa,qBAAqB;IAE1B,QAAQ,CAAC,gBAAgB,EAAE,MAAM,EAAE;IACnC,QAAQ,CAAC,gBAAgB,EAAE,MAAM,EAAE;IACnC,QAAQ,CAAC,gBAAgB,EAAE,MAAM,EAAE;gBAF1B,gBAAgB,EAAE,MAAM,EAAE,EAC1B,gBAAgB,EAAE,MAAM,EAAE,EAC1B,gBAAgB,EAAE,MAAM,EAAE;CAE1C"}
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ActionPermissionsData = void 0;
4
- class ActionPermissionsData {
5
- userIdsWhitelist;
6
- chatIdsWhitelist;
7
- chatIdsBlacklist;
8
- constructor(userIdsWhitelist, chatIdsWhitelist, chatIdsBlacklist) {
9
- this.userIdsWhitelist = userIdsWhitelist;
10
- this.chatIdsWhitelist = chatIdsWhitelist;
11
- this.chatIdsBlacklist = chatIdsBlacklist;
12
- }
13
- }
14
- exports.ActionPermissionsData = ActionPermissionsData;
@@ -1,7 +0,0 @@
1
- export class ActionPermissionsData {
2
- constructor(
3
- readonly userIdsWhitelist: number[],
4
- readonly chatIdsWhitelist: number[],
5
- readonly chatIdsBlacklist: number[]
6
- ) {}
7
- }