chz-telegram-bot 0.0.19 → 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"}
@@ -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;
@@ -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.19",
3
+ "version": "0.0.20",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "async-sema": "^3.1.1",
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>;