chz-telegram-bot 0.0.18 → 0.0.20

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.
@@ -1,11 +1,11 @@
1
1
  import { Sema as Semaphore } from 'async-sema';
2
2
  import { ScheduledHandler } from '../../types/handlers';
3
3
  import { HoursOfDay } from '../../types/timeValues';
4
+ import { IActionState } from '../../types/actionState';
4
5
  import { IActionWithState } from '../../types/actionWithState';
5
6
  import { CachedStateFactory } from '../cachedStateFactory';
6
7
  import { ChatContext } from '../context/chatContext';
7
- import { ActionStateBase } from '../states/actionStateBase';
8
- export declare class ScheduledAction implements IActionWithState {
8
+ export declare class ScheduledAction<TActionState extends IActionState> implements IActionWithState {
9
9
  static semaphore: Semaphore;
10
10
  name: string;
11
11
  timeinHours: HoursOfDay;
@@ -13,10 +13,10 @@ export declare class ScheduledAction implements IActionWithState {
13
13
  chatsWhitelist: number[];
14
14
  key: string;
15
15
  cachedState: Map<string, unknown>;
16
- stateConstructor: () => ActionStateBase;
16
+ stateConstructor: () => TActionState;
17
17
  cachedStateFactories: Map<string, CachedStateFactory>;
18
- handler: ScheduledHandler;
19
- constructor(name: string, handler: ScheduledHandler, timeinHours: HoursOfDay, active: boolean, whitelist: number[], cachedStateFactories: Map<string, CachedStateFactory>);
18
+ handler: ScheduledHandler<TActionState>;
19
+ constructor(name: string, handler: ScheduledHandler<TActionState>, timeinHours: HoursOfDay, active: boolean, whitelist: number[], cachedStateFactories: Map<string, CachedStateFactory>, stateConstructor: () => TActionState);
20
20
  exec(ctx: ChatContext): Promise<void>;
21
21
  private getCachedValue;
22
22
  private shouldTrigger;
@@ -1 +1 @@
1
- {"version":3,"file":"scheduledAction.d.ts","sourceRoot":"","sources":["../../../entities/actions/scheduledAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;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E,MAAM,6BAA6B,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAKrD,qBAAa,eAAe,CAAC,YAAY,SAAS,YAAY,CAC1D,YAAW,gBAAgB;IAE3B,MAAM,CAAC,SAAS,YAAoB;IAEpC,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,UAAU,CAAC;IACxB,MAAM,EAAE,OAAO,CAAC;IAChB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,GAAG,EAAE,MAAM,CAAC;IAEZ,WAAW,uBAA8B;IACzC,gBAAgB,EAAE,MAAM,YAAY,CAAC;IACrC,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;IACtD,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;gBAGpC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACvC,WAAW,EAAE,UAAU,EACvB,MAAM,EAAE,OAAO,EACf,SAAS,EAAE,MAAM,EAAE,EACnB,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,EACrD,gBAAgB,EAAE,MAAM,YAAY;IAYlC,IAAI,CAAC,GAAG,EAAE,WAAW;YAkCb,cAAc;IAqC5B,OAAO,CAAC,aAAa;CASxB"}
@@ -7,14 +7,12 @@ exports.ScheduledAction = void 0;
7
7
  const moment_1 = __importDefault(require("moment"));
8
8
  const async_sema_1 = require("async-sema");
9
9
  const timeConvertions_1 = require("../../helpers/timeConvertions");
10
- const actionStateBase_1 = require("../states/actionStateBase");
11
10
  const actionExecutionResult_1 = require("../actionExecutionResult");
12
11
  const logger_1 = require("../../services/logger");
13
12
  const taskScheduler_1 = require("../../services/taskScheduler");
14
13
  class ScheduledAction {
15
- constructor(name, handler, timeinHours, active, whitelist, cachedStateFactories) {
14
+ constructor(name, handler, timeinHours, active, whitelist, cachedStateFactories, stateConstructor) {
16
15
  this.cachedState = new Map();
17
- this.stateConstructor = () => new actionStateBase_1.ActionStateBase();
18
16
  this.name = name;
19
17
  this.handler = handler;
20
18
  this.timeinHours = timeinHours;
@@ -22,6 +20,7 @@ class ScheduledAction {
22
20
  this.chatsWhitelist = whitelist;
23
21
  this.cachedStateFactories = cachedStateFactories;
24
22
  this.key = `scheduled:${this.name.replace('.', '-')}`;
23
+ this.stateConstructor = stateConstructor;
25
24
  }
26
25
  async exec(ctx) {
27
26
  if (!this.active || !this.chatsWhitelist.includes(ctx.chatId))
@@ -30,7 +29,7 @@ class ScheduledAction {
30
29
  const isAllowedToTrigger = this.shouldTrigger(state);
31
30
  if (isAllowedToTrigger) {
32
31
  logger_1.Logger.logWithTraceId(ctx.botName, ctx.traceId, ctx.chatName, ` - Executing [${this.name}] in ${ctx.chatId}`);
33
- await this.handler(ctx, (key) => this.getCachedValue(key, ctx.botName));
32
+ await this.handler(ctx, (key) => this.getCachedValue(key, ctx.botName), state);
34
33
  state.lastExecutedDate = (0, moment_1.default)().valueOf();
35
34
  await ctx.storage.saveActionExecutionResult(this, ctx.chatId, new actionExecutionResult_1.ActionExecutionResult(state, isAllowedToTrigger));
36
35
  }
@@ -15,7 +15,7 @@ export declare class BotInstance {
15
15
  name: string;
16
16
  token: string;
17
17
  commands: CommandAction<IActionState>[];
18
- scheduled: ScheduledAction[];
18
+ scheduled: ScheduledAction<IActionState>[];
19
19
  chats: Map<string, number>;
20
20
  storageClient?: IStorageClient;
21
21
  storagePath?: string;
@@ -1 +1 @@
1
- {"version":3,"file":"botInstance.d.ts","sourceRoot":"","sources":["../../entities/botInstance.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAGlD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,eAAe,E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BAEZ,OAAO,EAAE;QACjB,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,CAAC;QACxC,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;QAC3C,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC3B,aAAa,CAAC,EAAE,cAAc,CAAC;QAC/B,WAAW,CAAC,EAAE,MAAM,CAAC;KACxB;IAuEK,IAAI,CAAC,IAAI,EAAE,MAAM;YAYT,YAAY;YAqBZ,eAAe;CAmBhC"}
@@ -1,20 +1,26 @@
1
1
  import { ScheduledAction } from '../../entities/actions/scheduledAction';
2
2
  import { CachedStateFactory } from '../../entities/cachedStateFactory';
3
+ import { ActionStateBase } from '../../entities/states/actionStateBase';
4
+ import { IActionState } from '../../types/actionState';
3
5
  import { ScheduledHandler } from '../../types/handlers';
4
6
  import { Hours, HoursOfDay } from '../../types/timeValues';
5
- export declare class ScheduledActionBuilder {
7
+ export declare class ScheduledActionBuilderWithState<TActionState extends IActionState> {
6
8
  active: boolean;
7
9
  time: HoursOfDay;
8
10
  cachedStateFactories: Map<string, CachedStateFactory>;
9
11
  whitelist: number[];
10
- handler: ScheduledHandler;
12
+ stateConstructor: () => TActionState;
13
+ handler: ScheduledHandler<TActionState>;
11
14
  name: string;
12
- constructor(name: string);
15
+ constructor(name: string, stateConstructor: () => TActionState);
13
16
  allowIn(chatId: number): this;
14
17
  runAt(time: HoursOfDay): this;
15
- do(handler: ScheduledHandler): this;
18
+ do(handler: ScheduledHandler<TActionState>): this;
16
19
  withSharedCache(key: string, itemFactory: () => Promise<unknown>, invalidationTimeoutInHours?: Hours): this;
17
20
  disabled(): this;
18
- build(): ScheduledAction;
21
+ build(): ScheduledAction<TActionState>;
22
+ }
23
+ export declare class ScheduledActionBuilder extends ScheduledActionBuilderWithState<ActionStateBase> {
24
+ constructor(name: string);
19
25
  }
20
26
  //# sourceMappingURL=scheduledActionBuilder.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"scheduledActionBuilder.d.ts","sourceRoot":"","sources":["../../../helpers/builders/scheduledActionBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wCAAwC,CAAC;AACzE,OAAO,EAAE,kBAAkB,EAAE,MAAM,mCAAmC,CAAC;AACvE,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAG3D,qBAAa,sBAAsB;IAC/B,MAAM,UAAQ;IACd,IAAI,EAAE,UAAU,CAAK;IACrB,oBAAoB,kCAAyC;IAC7D,SAAS,EAAE,MAAM,EAAE,CAAM;IACzB,OAAO,EAAE,gBAAgB,CAAa;IAEtC,IAAI,EAAE,MAAM,CAAC;gBAED,IAAI,EAAE,MAAM;IAIxB,OAAO,CAAC,MAAM,EAAE,MAAM;IAMtB,KAAK,CAAC,IAAI,EAAE,UAAU;IAMtB,EAAE,CAAC,OAAO,EAAE,gBAAgB;IAM5B,eAAe,CACX,GAAG,EAAE,MAAM,EACX,WAAW,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,EACnC,0BAA0B,GAAE,KAAmB;IAUnD,QAAQ;IAMR,KAAK;CAUR"}
1
+ {"version":3,"file":"scheduledActionBuilder.d.ts","sourceRoot":"","sources":["../../../helpers/builders/scheduledActionBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wCAAwC,CAAC;AACzE,OAAO,EAAE,kBAAkB,EAAE,MAAM,mCAAmC,CAAC;AACvE,OAAO,EAAE,eAAe,EAAE,MAAM,uCAAuC,CAAC;AACxE,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAG3D,qBAAa,+BAA+B,CACxC,YAAY,SAAS,YAAY;IAEjC,MAAM,UAAQ;IACd,IAAI,EAAE,UAAU,CAAK;IACrB,oBAAoB,kCAAyC;IAC7D,SAAS,EAAE,MAAM,EAAE,CAAM;IACzB,gBAAgB,EAAE,MAAM,YAAY,CAAC;IACrC,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAa;IAEpD,IAAI,EAAE,MAAM,CAAC;gBAED,IAAI,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,YAAY;IAK9D,OAAO,CAAC,MAAM,EAAE,MAAM;IAMtB,KAAK,CAAC,IAAI,EAAE,UAAU;IAMtB,EAAE,CAAC,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC;IAM1C,eAAe,CACX,GAAG,EAAE,MAAM,EACX,WAAW,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,EACnC,0BAA0B,GAAE,KAAmB;IAUnD,QAAQ;IAMR,KAAK;CAWR;AAED,qBAAa,sBAAuB,SAAQ,+BAA+B,CAAC,eAAe,CAAC;gBAC5E,IAAI,EAAE,MAAM;CAG3B"}
@@ -1,17 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ScheduledActionBuilder = void 0;
3
+ exports.ScheduledActionBuilder = exports.ScheduledActionBuilderWithState = void 0;
4
4
  const scheduledAction_1 = require("../../entities/actions/scheduledAction");
5
5
  const cachedStateFactory_1 = require("../../entities/cachedStateFactory");
6
+ const actionStateBase_1 = require("../../entities/states/actionStateBase");
6
7
  const noop_1 = require("../noop");
7
- class ScheduledActionBuilder {
8
- constructor(name) {
8
+ class ScheduledActionBuilderWithState {
9
+ constructor(name, stateConstructor) {
9
10
  this.active = true;
10
11
  this.time = 0;
11
12
  this.cachedStateFactories = new Map();
12
13
  this.whitelist = [];
13
14
  this.handler = noop_1.Noop.call;
14
15
  this.name = name;
16
+ this.stateConstructor = stateConstructor;
15
17
  }
16
18
  allowIn(chatId) {
17
19
  this.whitelist.push(chatId);
@@ -34,7 +36,13 @@ class ScheduledActionBuilder {
34
36
  return this;
35
37
  }
36
38
  build() {
37
- return new scheduledAction_1.ScheduledAction(this.name, this.handler, this.time, this.active, this.whitelist, this.cachedStateFactories);
39
+ return new scheduledAction_1.ScheduledAction(this.name, this.handler, this.time, this.active, this.whitelist, this.cachedStateFactories, this.stateConstructor);
40
+ }
41
+ }
42
+ exports.ScheduledActionBuilderWithState = ScheduledActionBuilderWithState;
43
+ class ScheduledActionBuilder extends ScheduledActionBuilderWithState {
44
+ constructor(name) {
45
+ super(name, () => new actionStateBase_1.ActionStateBase());
38
46
  }
39
47
  }
40
48
  exports.ScheduledActionBuilder = ScheduledActionBuilder;
package/dist/main.d.ts CHANGED
@@ -7,7 +7,7 @@ declare function startBot(options: {
7
7
  name: string;
8
8
  tokenFilePath: string;
9
9
  commands: CommandAction<IActionState>[];
10
- scheduled: ScheduledAction[];
10
+ scheduled: ScheduledAction<IActionState>[];
11
11
  chats: Map<string, number>;
12
12
  storageClient?: IStorageClient;
13
13
  storagePath?: string;
@@ -1 +1 @@
1
- {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../main.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEjD,OAAO,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAC;AACjE,OAAO,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AACrE,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAQrD,iBAAe,QAAQ,CAAC,OAAO,EAAE;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,CAAC;IACxC,SAAS,EAAE,eAAe,EAAE,CAAC;IAC7B,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC3B,aAAa,CAAC,EAAE,cAAc,CAAC;IAC/B,WAAW,CAAC,EAAE,MAAM,CAAC;CACxB,wBAcA;AAED,iBAAe,QAAQ,CAAC,MAAM,EAAE,MAAM,iBASrC;AAED,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC"}
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../main.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEjD,OAAO,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAC;AACjE,OAAO,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AACrE,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAQrD,iBAAe,QAAQ,CAAC,OAAO,EAAE;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,CAAC;IACxC,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;IAC3C,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC3B,aAAa,CAAC,EAAE,cAAc,CAAC;IAC/B,WAAW,CAAC,EAAE,MAAM,CAAC;CACxB,wBAcA;AAED,iBAAe,QAAQ,CAAC,MAAM,EAAE,MAAM,iBASrC;AAED,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"jsonFileStorage.d.ts","sourceRoot":"","sources":["../../services/jsonFileStorage.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,C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AAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAE5D,qBAAa,eAAgB,YAAW,cAAc;IAClD,SAAS,YAAoB;IAC7B,OAAO,CAAC,KAAK,CAA4C;IACzD,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,OAAO,CAAS;gBAEZ,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM;YAY5B,IAAI;YAUJ,YAAY;YAmBZ,IAAI;IAalB,OAAO,CAAC,eAAe;IAOjB,IAAI,CAAC,YAAY,SAAS,YAAY,EAAE,GAAG,EAAE,MAAM;IAMnD,YAAY,CAAC,OAAO,EAAE,gBAAgB,EAAE,EAAE,OAAO,EAAE,MAAM;IAUzD,cAAc,CAAC,YAAY,SAAS,YAAY,EAClD,MAAM,EAAE,gBAAgB,EACxB,MAAM,EAAE,MAAM;IAYZ,yBAAyB,CAC3B,MAAM,EAAE,gBAAgB,EACxB,MAAM,EAAE,MAAM,EACd,iBAAiB,EAAE,qBAAqB;IAYtC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAItB,cAAc,CAAC,YAAY,SAAS,YAAY,EAClD,eAAe,EAAE,MAAM,EACvB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC;CAcrD"}
@@ -5,6 +5,7 @@ const fs_1 = require("fs");
5
5
  const path_1 = require("path");
6
6
  const promises_1 = require("fs/promises");
7
7
  const async_sema_1 = require("async-sema");
8
+ const actionStateBase_1 = require("../entities/states/actionStateBase");
8
9
  class JsonFileStorage {
9
10
  constructor(botName, path) {
10
11
  this.semaphore = new async_sema_1.Sema(1);
@@ -68,7 +69,7 @@ class JsonFileStorage {
68
69
  async getActionState(entity, chatId) {
69
70
  return await this.lock(async () => {
70
71
  const data = await this.loadInternal(entity.key);
71
- return data[chatId] ?? entity.stateConstructor();
72
+ return Object.assign(entity.stateConstructor(), data[chatId]);
72
73
  });
73
74
  }
74
75
  async saveActionExecutionResult(action, chatId, transactionResult) {
@@ -86,7 +87,7 @@ class JsonFileStorage {
86
87
  async updateStateFor(sourceActionKey, chatId, update) {
87
88
  await this.lock(async () => {
88
89
  const data = await this.loadInternal(sourceActionKey);
89
- const state = data[chatId];
90
+ const state = Object.assign(new actionStateBase_1.ActionStateBase(), data[chatId]);
90
91
  await update(state);
91
92
  await this.save(data, sourceActionKey);
92
93
  });
@@ -1,4 +1,5 @@
1
1
  declare class JsonLogger {
2
+ private serializeError;
2
3
  logWithTraceId(botName: string, traceId: string | number, chatName: string, text: string): void;
3
4
  errorWithTraceId<TData>(botName: string, traceId: string | number, chatName: string, errorObj: unknown, extraData?: TData | undefined): void;
4
5
  }
@@ -1 +1 @@
1
- {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../services/logger.ts"],"names":[],"mappings":"AAAA,cAAM,UAAU;IACZ,cAAc,CACV,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM;IAKhB,gBAAgB,CAAC,KAAK,EAClB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,OAAO,EACjB,SAAS,CAAC,EAAE,KAAK,GAAG,SAAS;CAYpC;AAED,eAAO,MAAM,MAAM,YAAmB,CAAC"}
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../services/logger.ts"],"names":[],"mappings":"AAAA,cAAM,UAAU;IAEZ,OAAO,CAAC,cAAc;IAQtB,cAAc,CACV,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM;IAKhB,gBAAgB,CAAC,KAAK,EAClB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,OAAO,EACjB,SAAS,CAAC,EAAE,KAAK,GAAG,SAAS;CAYpC;AAED,eAAO,MAAM,MAAM,YAAmB,CAAC"}
@@ -2,6 +2,14 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Logger = void 0;
4
4
  class JsonLogger {
5
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
+ serializeError(error) {
7
+ const plainObject = {};
8
+ Object.getOwnPropertyNames(error).forEach(function (key) {
9
+ plainObject[key] = error[key];
10
+ });
11
+ return JSON.stringify(plainObject);
12
+ }
5
13
  logWithTraceId(botName, traceId, chatName, text) {
6
14
  console.log(JSON.stringify({ botName, traceId, chatName, text }));
7
15
  }
@@ -10,7 +18,7 @@ class JsonLogger {
10
18
  botName,
11
19
  traceId,
12
20
  chatName,
13
- errorObj,
21
+ error: this.serializeError(errorObj),
14
22
  extraData
15
23
  }));
16
24
  }
@@ -1 +1 @@
1
- {"version":3,"file":"telegramApi.d.ts","sourceRoot":"","sources":["../../services/telegramApi.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AAEpE,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAIlD,OAAO,EAAE,QAAQ,EAAE,MAAM,gCAAgC,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAiB,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,MAAM,mCAAmC,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AAClE,OAAO,EAAE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BAAmB,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,MAAM,mCAAmC,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AAClE,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAErD,qBAAa,kBAAkB;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,QAAQ,CAAC;IACnB,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC3B,YAAY,EAAE,KAAK,CAAC,WAAW,CAAC,CAAM;IACtC,OAAO,EAAE,cAAc,CAAC;gBAGpB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,cAAc,EACvB,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC;YAkBhB,eAAe;YAkBf,WAAW;YAqBX,eAAe;IA2E7B,OAAO,CAAC,OAAO;IAIf,OAAO,CAAC,eAAe;IAQvB,uBAAuB,CACnB,eAAe,EAAE,eAAe,EAChC,UAAU,EAAE,MAAM;IAsBtB,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM;CAW5D;AAED,MAAM,WAAW,mBAAmB;IAChC,OAAO,EAAE,CAAC,QAAQ,EAAE,WAAW,GAAG,YAAY,GAAG,YAAY,KAAK,IAAI,CAAC;IACvE,KAAK,EAAE,CAAC,QAAQ,EAAE,QAAQ,KAAK,IAAI,CAAC;IACpC,KAAK,EAAE,CAAC,YAAY,EAAE,aAAa,KAAK,IAAI,CAAC;CAChD"}
@@ -25,7 +25,6 @@ class TelegramApiService {
25
25
  await this.processResponse(message);
26
26
  }
27
27
  catch (error) {
28
- console.dir(error);
29
28
  logger_1.Logger.errorWithTraceId(this.botName, message.traceId, this.chats.get(message.chatId), error, message);
30
29
  }
31
30
  }
@@ -3,5 +3,5 @@ import { MessageContext } from '../entities/context/messageContext';
3
3
  import { IActionState } from './actionState';
4
4
  import { CachedValueAccessor } from './cachedValueAccessor';
5
5
  export type CommandHandler<TActionState extends IActionState> = (ctx: MessageContext<TActionState>, state: TActionState) => Promise<void>;
6
- export type ScheduledHandler = (ctx: ChatContext, getCached: CachedValueAccessor) => Promise<void>;
6
+ export type ScheduledHandler<TActionState extends IActionState> = (ctx: ChatContext, getCached: CachedValueAccessor, state: TActionState) => Promise<void>;
7
7
  //# sourceMappingURL=handlers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"handlers.d.ts","sourceRoot":"","sources":["../../types/handlers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D,MAAM,MAAM,cAAc,CAAC,YAAY,SAAS,YAAY,IAAI,CAC5D,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC,EACjC,KAAK,EAAE,YAAY,KAClB,OAAO,CAAC,IAAI,CAAC,CAAC;AAEnB,MAAM,MAAM,gBAAgB,GAAG,CAC3B,GAAG,EAAE,WAAW,EAChB,SAAS,EAAE,mBAAmB,KAC7B,OAAO,CAAC,IAAI,CAAC,CAAC"}
1
+ {"version":3,"file":"handlers.d.ts","sourceRoot":"","sources":["../../types/handlers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D,MAAM,MAAM,cAAc,CAAC,YAAY,SAAS,YAAY,IAAI,CAC5D,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC,EACjC,KAAK,EAAE,YAAY,KAClB,OAAO,CAAC,IAAI,CAAC,CAAC;AAEnB,MAAM,MAAM,gBAAgB,CAAC,YAAY,SAAS,YAAY,IAAI,CAC9D,GAAG,EAAE,WAAW,EAChB,SAAS,EAAE,mBAAmB,EAC9B,KAAK,EAAE,YAAY,KAClB,OAAO,CAAC,IAAI,CAAC,CAAC"}
@@ -7,12 +7,13 @@ import { IActionState } from '../../types/actionState';
7
7
  import { IActionWithState } from '../../types/actionWithState';
8
8
  import { CachedStateFactory } from '../cachedStateFactory';
9
9
  import { ChatContext } from '../context/chatContext';
10
- import { ActionStateBase } from '../states/actionStateBase';
11
10
  import { ActionExecutionResult } from '../actionExecutionResult';
12
11
  import { Logger } from '../../services/logger';
13
12
  import { Scheduler } from '../../services/taskScheduler';
14
13
 
15
- export class ScheduledAction implements IActionWithState {
14
+ export class ScheduledAction<TActionState extends IActionState>
15
+ implements IActionWithState
16
+ {
16
17
  static semaphore = new Semaphore(1);
17
18
 
18
19
  name: string;
@@ -22,17 +23,18 @@ export class ScheduledAction implements IActionWithState {
22
23
  key: string;
23
24
 
24
25
  cachedState = new Map<string, unknown>();
25
- stateConstructor = () => new ActionStateBase();
26
+ stateConstructor: () => TActionState;
26
27
  cachedStateFactories: Map<string, CachedStateFactory>;
27
- handler: ScheduledHandler;
28
+ handler: ScheduledHandler<TActionState>;
28
29
 
29
30
  constructor(
30
31
  name: string,
31
- handler: ScheduledHandler,
32
+ handler: ScheduledHandler<TActionState>,
32
33
  timeinHours: HoursOfDay,
33
34
  active: boolean,
34
35
  whitelist: number[],
35
- cachedStateFactories: Map<string, CachedStateFactory>
36
+ cachedStateFactories: Map<string, CachedStateFactory>,
37
+ stateConstructor: () => TActionState
36
38
  ) {
37
39
  this.name = name;
38
40
  this.handler = handler;
@@ -41,12 +43,16 @@ export class ScheduledAction implements IActionWithState {
41
43
  this.chatsWhitelist = whitelist;
42
44
  this.cachedStateFactories = cachedStateFactories;
43
45
  this.key = `scheduled:${this.name.replace('.', '-')}`;
46
+ this.stateConstructor = stateConstructor;
44
47
  }
45
48
 
46
49
  async exec(ctx: ChatContext) {
47
50
  if (!this.active || !this.chatsWhitelist.includes(ctx.chatId)) return;
48
51
 
49
- const state = await ctx.storage.getActionState(this, ctx.chatId);
52
+ const state = await ctx.storage.getActionState<TActionState>(
53
+ this,
54
+ ctx.chatId
55
+ );
50
56
  const isAllowedToTrigger = this.shouldTrigger(state);
51
57
 
52
58
  if (isAllowedToTrigger) {
@@ -57,8 +63,11 @@ export class ScheduledAction implements IActionWithState {
57
63
  ` - Executing [${this.name}] in ${ctx.chatId}`
58
64
  );
59
65
 
60
- await this.handler(ctx, <TResult>(key: string) =>
61
- this.getCachedValue<TResult>(key, ctx.botName)
66
+ await this.handler(
67
+ ctx,
68
+ <TResult>(key: string) =>
69
+ this.getCachedValue<TResult>(key, ctx.botName),
70
+ state
62
71
  );
63
72
 
64
73
  state.lastExecutedDate = moment().valueOf();
@@ -19,7 +19,7 @@ export class BotInstance {
19
19
  private api: TelegramApiService;
20
20
  private telegraf: Telegraf;
21
21
  private commands: CommandAction<IActionState>[];
22
- private scheduled: ScheduledAction[];
22
+ private scheduled: ScheduledAction<IActionState>[];
23
23
  private chats: Map<string, number>;
24
24
  private messageQueue: IncomingMessage[] = [];
25
25
  storage: IStorageClient;
@@ -28,7 +28,7 @@ export class BotInstance {
28
28
  name: string;
29
29
  token: string;
30
30
  commands: CommandAction<IActionState>[];
31
- scheduled: ScheduledAction[];
31
+ scheduled: ScheduledAction<IActionState>[];
32
32
  chats: Map<string, number>;
33
33
  storageClient?: IStorageClient;
34
34
  storagePath?: string;
@@ -128,7 +128,7 @@ export class BotInstance {
128
128
  ctx.botName,
129
129
  ctx.traceId,
130
130
  chatName,
131
- error as string | Error,
131
+ error,
132
132
  ctx
133
133
  );
134
134
  }
@@ -149,7 +149,7 @@ export class BotInstance {
149
149
  ctx.botName,
150
150
  ctx.traceId,
151
151
  ctx.chatName,
152
- error as string | Error,
152
+ error,
153
153
  ctx
154
154
  );
155
155
  }
@@ -1,20 +1,26 @@
1
1
  import { ScheduledAction } from '../../entities/actions/scheduledAction';
2
2
  import { CachedStateFactory } from '../../entities/cachedStateFactory';
3
+ import { ActionStateBase } from '../../entities/states/actionStateBase';
4
+ import { IActionState } from '../../types/actionState';
3
5
  import { ScheduledHandler } from '../../types/handlers';
4
6
  import { Hours, HoursOfDay } from '../../types/timeValues';
5
7
  import { Noop } from '../noop';
6
8
 
7
- export class ScheduledActionBuilder {
9
+ export class ScheduledActionBuilderWithState<
10
+ TActionState extends IActionState
11
+ > {
8
12
  active = true;
9
13
  time: HoursOfDay = 0;
10
14
  cachedStateFactories = new Map<string, CachedStateFactory>();
11
15
  whitelist: number[] = [];
12
- handler: ScheduledHandler = Noop.call;
16
+ stateConstructor: () => TActionState;
17
+ handler: ScheduledHandler<TActionState> = Noop.call;
13
18
 
14
19
  name: string;
15
20
 
16
- constructor(name: string) {
21
+ constructor(name: string, stateConstructor: () => TActionState) {
17
22
  this.name = name;
23
+ this.stateConstructor = stateConstructor;
18
24
  }
19
25
 
20
26
  allowIn(chatId: number) {
@@ -29,7 +35,7 @@ export class ScheduledActionBuilder {
29
35
  return this;
30
36
  }
31
37
 
32
- do(handler: ScheduledHandler) {
38
+ do(handler: ScheduledHandler<TActionState>) {
33
39
  this.handler = handler;
34
40
 
35
41
  return this;
@@ -55,13 +61,20 @@ export class ScheduledActionBuilder {
55
61
  }
56
62
 
57
63
  build() {
58
- return new ScheduledAction(
64
+ return new ScheduledAction<TActionState>(
59
65
  this.name,
60
66
  this.handler,
61
67
  this.time,
62
68
  this.active,
63
69
  this.whitelist,
64
- this.cachedStateFactories
70
+ this.cachedStateFactories,
71
+ this.stateConstructor
65
72
  );
66
73
  }
67
74
  }
75
+
76
+ export class ScheduledActionBuilder extends ScheduledActionBuilderWithState<ActionStateBase> {
77
+ constructor(name: string) {
78
+ super(name, () => new ActionStateBase());
79
+ }
80
+ }
package/main.ts CHANGED
@@ -17,7 +17,7 @@ async function startBot(options: {
17
17
  name: string;
18
18
  tokenFilePath: string;
19
19
  commands: CommandAction<IActionState>[];
20
- scheduled: ScheduledAction[];
20
+ scheduled: ScheduledAction<IActionState>[];
21
21
  chats: Map<string, number>;
22
22
  storageClient?: IStorageClient;
23
23
  storagePath?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chz-telegram-bot",
3
- "version": "0.0.18",
3
+ "version": "0.0.20",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "async-sema": "^3.1.1",
@@ -98,7 +98,10 @@ export class JsonFileStorage implements IStorageClient {
98
98
  return await this.lock(async () => {
99
99
  const data = await this.loadInternal(entity.key);
100
100
 
101
- return (data[chatId] as TActionState) ?? entity.stateConstructor();
101
+ return Object.assign(
102
+ entity.stateConstructor(),
103
+ data[chatId]
104
+ ) as TActionState;
102
105
  });
103
106
  }
104
107
 
@@ -128,7 +131,10 @@ export class JsonFileStorage implements IStorageClient {
128
131
  ) {
129
132
  await this.lock(async () => {
130
133
  const data = await this.loadInternal(sourceActionKey);
131
- const state = data[chatId] as TActionState;
134
+ const state = Object.assign(
135
+ new ActionStateBase(),
136
+ data[chatId]
137
+ ) as TActionState;
132
138
 
133
139
  await update(state);
134
140
 
@@ -1,4 +1,13 @@
1
1
  class JsonLogger {
2
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3
+ private serializeError(error: any) {
4
+ const plainObject: Record<string, unknown> = {};
5
+ Object.getOwnPropertyNames(error).forEach(function (key) {
6
+ plainObject[key] = error[key];
7
+ });
8
+ return JSON.stringify(plainObject);
9
+ }
10
+
2
11
  logWithTraceId(
3
12
  botName: string,
4
13
  traceId: string | number,
@@ -20,7 +29,7 @@ class JsonLogger {
20
29
  botName,
21
30
  traceId,
22
31
  chatName,
23
- errorObj,
32
+ error: this.serializeError(errorObj),
24
33
  extraData
25
34
  })
26
35
  );
@@ -52,7 +52,6 @@ export class TelegramApiService {
52
52
  try {
53
53
  await this.processResponse(message);
54
54
  } catch (error) {
55
- console.dir(error);
56
55
  Logger.errorWithTraceId(
57
56
  this.botName,
58
57
  message.traceId,
package/types/handlers.ts CHANGED
@@ -8,7 +8,8 @@ export type CommandHandler<TActionState extends IActionState> = (
8
8
  state: TActionState
9
9
  ) => Promise<void>;
10
10
 
11
- export type ScheduledHandler = (
11
+ export type ScheduledHandler<TActionState extends IActionState> = (
12
12
  ctx: ChatContext,
13
- getCached: CachedValueAccessor
13
+ getCached: CachedValueAccessor,
14
+ state: TActionState
14
15
  ) => Promise<void>;
@@ -1,9 +0,0 @@
1
- export enum Day {
2
- Sunday = 0,
3
- Monday = 1,
4
- Tuesday = 2,
5
- Wednesday = 3,
6
- Thursday = 4,
7
- Friday = 5,
8
- Saturday = 6
9
- }