chz-telegram-bot 0.0.19 → 0.0.21

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 (43) hide show
  1. package/dist/entities/actions/scheduledAction.d.ts +5 -5
  2. package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
  3. package/dist/entities/actions/scheduledAction.js +3 -4
  4. package/dist/entities/botInstance.d.ts +3 -1
  5. package/dist/entities/botInstance.d.ts.map +1 -1
  6. package/dist/entities/botInstance.js +29 -20
  7. package/dist/entities/context/chatContext.d.ts +4 -3
  8. package/dist/entities/context/chatContext.d.ts.map +1 -1
  9. package/dist/entities/context/chatContext.js +6 -6
  10. package/dist/entities/context/messageContext.d.ts +4 -3
  11. package/dist/entities/context/messageContext.d.ts.map +1 -1
  12. package/dist/entities/context/messageContext.js +6 -6
  13. package/dist/entities/responses/imageMessage.d.ts +2 -1
  14. package/dist/entities/responses/imageMessage.d.ts.map +1 -1
  15. package/dist/entities/responses/imageMessage.js +2 -2
  16. package/dist/entities/responses/textMessage.d.ts +2 -1
  17. package/dist/entities/responses/textMessage.d.ts.map +1 -1
  18. package/dist/entities/responses/textMessage.js +3 -3
  19. package/dist/entities/responses/videoMessage.d.ts +2 -1
  20. package/dist/entities/responses/videoMessage.d.ts.map +1 -1
  21. package/dist/entities/responses/videoMessage.js +2 -2
  22. package/dist/helpers/builders/scheduledActionBuilder.d.ts +11 -5
  23. package/dist/helpers/builders/scheduledActionBuilder.d.ts.map +1 -1
  24. package/dist/helpers/builders/scheduledActionBuilder.js +12 -4
  25. package/dist/main.d.ts +1 -1
  26. package/dist/main.d.ts.map +1 -1
  27. package/dist/types/handlers.d.ts +1 -1
  28. package/dist/types/handlers.d.ts.map +1 -1
  29. package/dist/types/messageSendingOptions.d.ts +7 -0
  30. package/dist/types/messageSendingOptions.d.ts.map +1 -0
  31. package/dist/types/messageSendingOptions.js +2 -0
  32. package/entities/actions/scheduledAction.ts +18 -9
  33. package/entities/botInstance.ts +53 -43
  34. package/entities/context/chatContext.ts +13 -14
  35. package/entities/context/messageContext.ts +13 -10
  36. package/entities/responses/imageMessage.ts +4 -3
  37. package/entities/responses/textMessage.ts +5 -5
  38. package/entities/responses/videoMessage.ts +4 -3
  39. package/helpers/builders/scheduledActionBuilder.ts +19 -6
  40. package/main.ts +1 -1
  41. package/package.json +1 -1
  42. package/types/handlers.ts +3 -2
  43. package/types/messageSendingOptions.ts +7 -0
@@ -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;AAExD,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAEpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAK5D,qBAAa,eAAgB,YAAW,gBAAgB;IACpD,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,wBAA+B;IAC/C,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;IACtD,OAAO,EAAE,gBAAgB,CAAC;gBAGtB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,gBAAgB,EACzB,WAAW,EAAE,UAAU,EACvB,MAAM,EAAE,OAAO,EACf,SAAS,EAAE,MAAM,EAAE,EACnB,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC;IAWnD,IAAI,CAAC,GAAG,EAAE,WAAW;YA4Bb,cAAc;IAqC5B,OAAO,CAAC,aAAa;CASxB"}
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;AAExD,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,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,11 +15,13 @@ 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;
22
22
  });
23
+ private initializeScheduledProcessing;
24
+ private initializeMessageProcessing;
23
25
  stop(code: string): Promise<void>;
24
26
  private runScheduled;
25
27
  private processMessages;
@@ -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,EAAE,MAAM,2BAA2B,CAAC;AAK5D,qBAAa,WAAW;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,GAAG,CAAqB;IAChC,OAAO,CAAC,QAAQ,CAAW;IAC3B,OAAO,CAAC,QAAQ,CAAgC;IAChD,OAAO,CAAC,SAAS,CAAoB;IACrC,OAAO,CAAC,KAAK,CAAsB;IACnC,OAAO,CAAC,YAAY,CAAyB;IAC7C,OAAO,EAAE,cAAc,CAAC;gBAEZ,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,EAAE,CAAC;QAC7B,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
+ {"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,EAAE,MAAM,2BAA2B,CAAC;AAK5D,qBAAa,WAAW;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,GAAG,CAAqB;IAChC,OAAO,CAAC,QAAQ,CAAW;IAC3B,OAAO,CAAC,QAAQ,CAAgC;IAChD,OAAO,CAAC,SAAS,CAAkC;IACnD,OAAO,CAAC,KAAK,CAAsB;IACnC,OAAO,CAAC,YAAY,CAAyB;IAC7C,OAAO,EAAE,cAAc,CAAC;gBAEZ,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;IAkCD,OAAO,CAAC,6BAA6B;IAcrC,OAAO,CAAC,2BAA2B;IAkC7B,IAAI,CAAC,IAAI,EAAE,MAAM;YAYT,YAAY;YAoBZ,eAAe;CAmBhC"}
@@ -21,26 +21,36 @@ class BotInstance {
21
21
  options.storageClient ??
22
22
  new jsonFileStorage_1.JsonFileStorage(options.name, options.storagePath);
23
23
  this.api = new telegramApi_1.TelegramApiService(this.name, this.telegraf.telegram, this.storage, this.chats);
24
- this.telegraf.on('message', async (ctx) => {
25
- const msg = new incomingMessage_1.IncomingMessage(ctx.update.message);
26
- const messageContent = msg.text || '<non-text message>';
27
- const messageFromName = msg.from?.first_name ?? 'Unknown';
28
- const messageFromId = msg.from?.id ?? 'Unknown';
29
- logger_1.Logger.logWithTraceId(this.name, msg.traceId, msg.chatName, `${messageFromName} (${messageFromId}): ${messageContent}`);
30
- if (msg.text) {
31
- this.messageQueue.push(msg);
32
- }
33
- });
34
- this.telegraf.launch();
35
- taskScheduler_1.Scheduler.createTask('MessageProcessing', async () => {
36
- while (this.messageQueue.length > 0) {
37
- await this.processMessages();
38
- }
39
- }, (0, timeConvertions_1.secondsToMilliseconds)(0.3), false, this.name);
40
- taskScheduler_1.Scheduler.createTask('ScheduledProcessing', async () => {
41
- await this.runScheduled();
42
- }, (0, timeConvertions_1.hoursToMilliseconds)(0.5), true, this.name);
24
+ this.initializeMessageProcessing();
25
+ this.initializeScheduledProcessing();
43
26
  this.storage.saveMetadata([...this.commands, ...this.scheduled], this.name);
27
+ this.telegraf.launch();
28
+ }
29
+ initializeScheduledProcessing() {
30
+ if (this.scheduled.length > 0) {
31
+ taskScheduler_1.Scheduler.createTask('ScheduledProcessing', async () => {
32
+ await this.runScheduled();
33
+ }, (0, timeConvertions_1.hoursToMilliseconds)(0.5), true, this.name);
34
+ }
35
+ }
36
+ initializeMessageProcessing() {
37
+ if (this.commands.length > 0) {
38
+ this.telegraf.on('message', async (ctx) => {
39
+ const msg = new incomingMessage_1.IncomingMessage(ctx.update.message);
40
+ const messageContent = msg.text || '<non-text message>';
41
+ const messageFromName = msg.from?.first_name ?? 'Unknown';
42
+ const messageFromId = msg.from?.id ?? 'Unknown';
43
+ logger_1.Logger.logWithTraceId(this.name, msg.traceId, msg.chatName, `${messageFromName} (${messageFromId}): ${messageContent}`);
44
+ if (msg.text) {
45
+ this.messageQueue.push(msg);
46
+ }
47
+ });
48
+ taskScheduler_1.Scheduler.createTask('MessageProcessing', async () => {
49
+ while (this.messageQueue.length > 0) {
50
+ await this.processMessages();
51
+ }
52
+ }, (0, timeConvertions_1.secondsToMilliseconds)(0.3), false, this.name);
53
+ }
44
54
  }
45
55
  async stop(code) {
46
56
  logger_1.Logger.logWithTraceId(this.name, `System:Bot-${this.name}-Stop`, 'System', 'Stopping bot...');
@@ -55,7 +65,6 @@ class BotInstance {
55
65
  await trig.exec(ctx);
56
66
  }
57
67
  catch (error) {
58
- console.dir(error);
59
68
  logger_1.Logger.errorWithTraceId(ctx.botName, ctx.traceId, chatName, error, ctx);
60
69
  }
61
70
  }
@@ -1,5 +1,6 @@
1
1
  import { IBotApiInteractions } from '../../services/telegramApi';
2
2
  import { IStorageClient } from '../../types/storage';
3
+ import { MessageSendingOptions, TextMessageSendingOptions } from '../../types/messageSendingOptions';
3
4
  export declare class ChatContext {
4
5
  botName: string;
5
6
  actionKey: string;
@@ -9,9 +10,9 @@ export declare class ChatContext {
9
10
  traceId: number | string;
10
11
  storage: IStorageClient;
11
12
  constructor(botName: string, actionKey: string, interactions: IBotApiInteractions, chatId: number, chatName: string, traceId: number | string, storage: IStorageClient);
12
- sendTextToChat(text: string, disableWebPreview?: boolean, pinned?: boolean): void;
13
- sendImageToChat(name: string, pinned?: boolean): void;
14
- sendVideoToChat(name: string, pinned?: boolean): void;
13
+ sendTextToChat(text: string, options?: TextMessageSendingOptions): void;
14
+ sendImageToChat(name: string, options?: MessageSendingOptions): void;
15
+ sendVideoToChat(name: string, options?: MessageSendingOptions): void;
15
16
  unpinMessage(messageId: number): void;
16
17
  }
17
18
  //# sourceMappingURL=chatContext.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"chatContext.d.ts","sourceRoot":"","sources":["../../../entities/context/chatContext.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAMrD,qBAAa,WAAW;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,mBAAmB,CAAC;IAClC,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,OAAO,EAAE,cAAc,CAAC;gBAGpB,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,MAAM,EACjB,YAAY,EAAE,mBAAmB,EACjC,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,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E,MAAM;CAUjC"}
@@ -16,16 +16,16 @@ class ChatContext {
16
16
  this.traceId = traceId;
17
17
  this.storage = storage;
18
18
  }
19
- sendTextToChat(text, disableWebPreview, pinned) {
20
- this.interactions.respond(new textMessage_1.TextMessage(text, this.chatId, undefined, this.traceId, disableWebPreview ?? false, pinned ?? false, this.actionKey));
19
+ sendTextToChat(text, options) {
20
+ this.interactions.respond(new textMessage_1.TextMessage(text, this.chatId, undefined, this.traceId, this.actionKey, options));
21
21
  }
22
- sendImageToChat(name, pinned) {
22
+ sendImageToChat(name, options) {
23
23
  const filePath = `./content/${name}.png`;
24
- this.interactions.respond(new imageMessage_1.ImageMessage({ source: (0, path_1.resolve)(filePath) }, this.chatId, undefined, this.traceId, pinned ?? false, this.actionKey));
24
+ this.interactions.respond(new imageMessage_1.ImageMessage({ source: (0, path_1.resolve)(filePath) }, this.chatId, undefined, this.traceId, this.actionKey, options));
25
25
  }
26
- sendVideoToChat(name, pinned) {
26
+ sendVideoToChat(name, options) {
27
27
  const filePath = `./content/${name}.mp4`;
28
- this.interactions.respond(new videoMessage_1.VideoMessage({ source: (0, path_1.resolve)(filePath) }, this.chatId, undefined, this.traceId, pinned ?? false, this.actionKey));
28
+ this.interactions.respond(new videoMessage_1.VideoMessage({ source: (0, path_1.resolve)(filePath) }, this.chatId, undefined, this.traceId, this.actionKey, options));
29
29
  }
30
30
  unpinMessage(messageId) {
31
31
  this.interactions.unpin(new unpin_1.UnpinResponse(messageId, this.chatId, this.traceId, this.actionKey));
@@ -3,6 +3,7 @@ import { TelegramEmoji } from 'telegraf/types';
3
3
  import { IStorageClient } from '../../types/storage';
4
4
  import { IActionState } from '../../types/actionState';
5
5
  import { ChatContext } from './chatContext';
6
+ import { MessageSendingOptions, TextMessageSendingOptions } from '../../types/messageSendingOptions';
6
7
  export declare class MessageContext<TActionState extends IActionState> extends ChatContext {
7
8
  messageId: number;
8
9
  messageText: string;
@@ -14,9 +15,9 @@ export declare class MessageContext<TActionState extends IActionState> extends C
14
15
  constructor(botName: string, scheduledKey: string, interactions: IBotApiInteractions, chatId: number, chatName: string, messageId: number, messageText: string, fromUserId: number | undefined, traceId: number | string, fromUserName: string, storage: IStorageClient);
15
16
  loadStateOf<TAnotherActionState extends IActionState>(commandName: string): Promise<TAnotherActionState>;
16
17
  updateState(stateUpdateAction: (state: TActionState) => void): void;
17
- replyWithText(text: string, disableWebPreview?: boolean, pinned?: boolean): void;
18
- replyWithImage(name: string, pinned?: boolean): void;
19
- replyWithVideo(name: string, pinned?: boolean): void;
18
+ replyWithText(text: string, options?: TextMessageSendingOptions): void;
19
+ replyWithImage(name: string, options?: MessageSendingOptions): void;
20
+ replyWithVideo(name: string, options?: MessageSendingOptions): void;
20
21
  react(emoji: TelegramEmoji): void;
21
22
  }
22
23
  //# sourceMappingURL=messageContext.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"messageContext.d.ts","sourceRoot":"","sources":["../../../entities/context/messageContext.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAMvD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,qBAAa,cAAc,CACvB,YAAY,SAAS,YAAY,CACnC,SAAQ,WAAW;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,gBAAgB,EAAE,CAAM;IACtC,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;IAC/B,aAAa,EAAE,OAAO,CAAQ;IAC9B,aAAa,EAAE,KAAK,CAAC,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAAC,CAAM;IACzD,YAAY,EAAE,MAAM,CAAC;gBAGjB,OAAO,EAAE,MAAM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M,CAAC;IACpB,YAAY,EAAE,gBAAgB,EAAE,CAAM;IACtC,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;IAC/B,aAAa,EAAE,OAAO,CAAQ;IAC9B,aAAa,EAAE,KAAK,CAAC,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAAC,CAAM;IACzD,YAAY,EAAE,MAAM,CAAC;gBAGjB,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,mBAAmB,EACjC,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,EACjB,WAAW,EAAE,MAAM,EACnB,UAAU,EAAE,MAAM,GAAG,SAAS,EAC9B,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,YAAY,EAAE,MAAM,EACpB,OAAO,EAAE,cAAc;IAkBrB,WAAW,CAAC,mBAAmB,SAAS,YAAY,EACtD,WAAW,EAAE,MAAM,GACpB,OAAO,CAAC,mBAAmB,CAAC;IAU/B,WAAW,CAAC,iBAAiB,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI;IAM5D,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,yBAAyB;IAa/D,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAc5D,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAc5D,KAAK,CAAC,KAAK,EAAE,aAAa;CAW7B"}
@@ -25,16 +25,16 @@ class MessageContext extends chatContext_1.ChatContext {
25
25
  updateState(stateUpdateAction) {
26
26
  this.updateActions.push(stateUpdateAction);
27
27
  }
28
- replyWithText(text, disableWebPreview, pinned) {
29
- this.interactions.respond(new textMessage_1.TextMessage(text, this.chatId, this.messageId, this.traceId, disableWebPreview ?? false, pinned ?? false, this.actionKey));
28
+ replyWithText(text, options) {
29
+ this.interactions.respond(new textMessage_1.TextMessage(text, this.chatId, this.messageId, this.traceId, this.actionKey, options));
30
30
  }
31
- replyWithImage(name, pinned) {
31
+ replyWithImage(name, options) {
32
32
  const filePath = `./content/${name}.png`;
33
- this.interactions.respond(new imageMessage_1.ImageMessage({ source: (0, path_1.resolve)(filePath) }, this.chatId, this.messageId, this.traceId, pinned ?? false, this.actionKey));
33
+ this.interactions.respond(new imageMessage_1.ImageMessage({ source: (0, path_1.resolve)(filePath) }, this.chatId, this.messageId, this.traceId, this.actionKey, options));
34
34
  }
35
- replyWithVideo(name, pinned) {
35
+ replyWithVideo(name, options) {
36
36
  const filePath = `./content/${name}.mp4`;
37
- this.interactions.respond(new videoMessage_1.VideoMessage({ source: (0, path_1.resolve)(filePath) }, this.chatId, this.messageId, this.traceId, pinned ?? false, this.actionKey));
37
+ this.interactions.respond(new videoMessage_1.VideoMessage({ source: (0, path_1.resolve)(filePath) }, this.chatId, this.messageId, this.traceId, this.actionKey, options));
38
38
  }
39
39
  react(emoji) {
40
40
  this.interactions.react(new reaction_1.Reaction(this.traceId, this.chatId, this.messageId, emoji, this.actionKey));
@@ -1,5 +1,6 @@
1
1
  import { InputFile } from 'telegraf/types';
2
2
  import { IReplyMessage } from '../../types/response';
3
+ import { MessageSendingOptions } from '../../types/messageSendingOptions';
3
4
  export declare class ImageMessage implements IReplyMessage<InputFile> {
4
5
  kind: "image";
5
6
  content: InputFile;
@@ -9,6 +10,6 @@ export declare class ImageMessage implements IReplyMessage<InputFile> {
9
10
  disableWebPreview: boolean;
10
11
  shouldPin: boolean;
11
12
  sourceActionKey: string;
12
- constructor(image: InputFile, chatId: number, replyId: number | undefined, traceId: number | string, pinned: boolean, sourceActionKey: string);
13
+ constructor(image: InputFile, chatId: number, replyId: number | undefined, traceId: number | string, sourceActionKey: string, options?: MessageSendingOptions);
13
14
  }
14
15
  //# sourceMappingURL=imageMessage.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"imageMessage.d.ts","sourceRoot":"","sources":["../../../entities/responses/imageMessage.ts"],"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MAAM,GAAG,MAAM,EACxB,eAAe,EAAE,MAAM,EACvB,OAAO,CAAC,EAAE,qBAAqB;CAStC"}
@@ -3,14 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ImageMessage = void 0;
4
4
  const response_1 = require("../../types/response");
5
5
  class ImageMessage {
6
- constructor(image, chatId, replyId, traceId, pinned, sourceActionKey) {
6
+ constructor(image, chatId, replyId, traceId, sourceActionKey, options) {
7
7
  this.kind = response_1.BotResponseTypes.image;
8
8
  this.disableWebPreview = false;
9
9
  this.content = image;
10
10
  this.chatId = chatId;
11
11
  this.replyId = replyId;
12
12
  this.traceId = traceId;
13
- this.shouldPin = pinned;
13
+ this.shouldPin = options?.pin ?? false;
14
14
  this.sourceActionKey = sourceActionKey;
15
15
  }
16
16
  }
@@ -1,3 +1,4 @@
1
+ import { TextMessageSendingOptions } from '../../types/messageSendingOptions';
1
2
  import { IReplyMessage } from '../../types/response';
2
3
  export declare class TextMessage implements IReplyMessage<string> {
3
4
  kind: "text";
@@ -8,6 +9,6 @@ export declare class TextMessage implements IReplyMessage<string> {
8
9
  disableWebPreview: boolean;
9
10
  shouldPin: boolean;
10
11
  sourceActionKey: string;
11
- constructor(text: string, chatId: number, replyId: number | undefined, traceId: string | number, disableWebPreview: boolean, pinned: boolean, sourceActionKey: string);
12
+ constructor(text: string, chatId: number, replyId: number | undefined, traceId: string | number, sourceActionKey: string, options?: TextMessageSendingOptions);
12
13
  }
13
14
  //# sourceMappingURL=textMessage.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"textMessage.d.ts","sourceRoot":"","sources":["../../../entities/responses/textMessage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAEvE,qBAAa,WAAY,YAAW,aAAa,CAAC,MAAM,CAAC;IACrD,IAAI,SAAyB;IAE7B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5B,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,iBAAiB,EAAE,OAAO,CAAC;IAC3B,SAAS,EAAE,OAAO,CAAC;IACnB,eAAe,EAAE,MAAM,CAAC;gBAGpB,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,GAAG,SAAS,EAC3B,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,iBAAiB,EAAE,OAAO,EAC1B,MAAM,EAAE,OAAO,EACf,eAAe,EAAE,MAAM;CAU9B"}
1
+ {"version":3,"file":"textMessage.d.ts","sourceRoot":"","sources":["../../../entities/responses/textMessage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,mCAAmC,CAAC;AAC9E,OAAO,EAAoB,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAEvE,qBAAa,WAAY,YAAW,aAAa,CAAC,MAAM,CAAC;IACrD,IAAI,SAAyB;IAE7B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5B,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,iBAAiB,EAAE,OAAO,CAAC;IAC3B,SAAS,EAAE,OAAO,CAAC;IACnB,eAAe,EAAE,MAAM,CAAC;gBAGpB,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,GAAG,SAAS,EAC3B,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,eAAe,EAAE,MAAM,EACvB,OAAO,CAAC,EAAE,yBAAyB;CAU1C"}
@@ -3,14 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TextMessage = void 0;
4
4
  const response_1 = require("../../types/response");
5
5
  class TextMessage {
6
- constructor(text, chatId, replyId, traceId, disableWebPreview, pinned, sourceActionKey) {
6
+ constructor(text, chatId, replyId, traceId, sourceActionKey, options) {
7
7
  this.kind = response_1.BotResponseTypes.text;
8
8
  this.content = text;
9
9
  this.chatId = chatId;
10
10
  this.replyId = replyId;
11
11
  this.traceId = traceId;
12
- this.disableWebPreview = disableWebPreview;
13
- this.shouldPin = pinned;
12
+ this.disableWebPreview = options?.disableWebPreview ?? false;
13
+ this.shouldPin = options?.pin ?? false;
14
14
  this.sourceActionKey = sourceActionKey;
15
15
  }
16
16
  }
@@ -1,5 +1,6 @@
1
1
  import { InputFile } from 'telegraf/types';
2
2
  import { IReplyMessage } from '../../types/response';
3
+ import { MessageSendingOptions } from '../../types/messageSendingOptions';
3
4
  export declare class VideoMessage implements IReplyMessage<InputFile> {
4
5
  kind: "video";
5
6
  content: InputFile;
@@ -9,6 +10,6 @@ export declare class VideoMessage implements IReplyMessage<InputFile> {
9
10
  disableWebPreview: boolean;
10
11
  shouldPin: boolean;
11
12
  sourceActionKey: string;
12
- constructor(video: InputFile, chatId: number, replyId: number | undefined, traceId: number | string, pinned: boolean, sourceActionKey: string);
13
+ constructor(video: InputFile, chatId: number, replyId: number | undefined, traceId: number | string, sourceActionKey: string, options?: MessageSendingOptions);
13
14
  }
14
15
  //# sourceMappingURL=videoMessage.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"videoMessage.d.ts","sourceRoot":"","sources":["../../../entities/responses/videoMessage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAoB,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAEvE,qBAAa,YAAa,YAAW,aAAa,CAAC,SAAS,CAAC;IACzD,IAAI,UAA0B;IAE9B,OAAO,EAAE,SAAS,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5B,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,iBAAiB,UAAS;IAC1B,SAAS,EAAE,OAAO,CAAC;IACnB,eAAe,EAAE,MAAM,CAAC;gBAGpB,KAAK,EAAE,SAAS,EAChB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,GAAG,SAAS,EAC3B,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,MAAM,EAAE,OAAO,EACf,eAAe,EAAE,MAAM;CAS9B"}
1
+ {"version":3,"file":"videoMessage.d.ts","sourceRoot":"","sources":["../../../entities/responses/videoMessage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAoB,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACvE,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAE1E,qBAAa,YAAa,YAAW,aAAa,CAAC,SAAS,CAAC;IACzD,IAAI,UAA0B;IAE9B,OAAO,EAAE,SAAS,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5B,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,iBAAiB,UAAS;IAC1B,SAAS,EAAE,OAAO,CAAC;IACnB,eAAe,EAAE,MAAM,CAAC;gBAGpB,KAAK,EAAE,SAAS,EAChB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,GAAG,SAAS,EAC3B,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,eAAe,EAAE,MAAM,EACvB,OAAO,CAAC,EAAE,qBAAqB;CAStC"}
@@ -3,14 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VideoMessage = void 0;
4
4
  const response_1 = require("../../types/response");
5
5
  class VideoMessage {
6
- constructor(video, chatId, replyId, traceId, pinned, sourceActionKey) {
6
+ constructor(video, chatId, replyId, traceId, sourceActionKey, options) {
7
7
  this.kind = response_1.BotResponseTypes.video;
8
8
  this.disableWebPreview = false;
9
9
  this.content = video;
10
10
  this.chatId = chatId;
11
11
  this.replyId = replyId;
12
12
  this.traceId = traceId;
13
- this.shouldPin = pinned;
13
+ this.shouldPin = options?.pin ?? false;
14
14
  this.sourceActionKey = sourceActionKey;
15
15
  }
16
16
  }
@@ -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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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"}
@@ -0,0 +1,7 @@
1
+ export interface MessageSendingOptions {
2
+ pin?: boolean;
3
+ }
4
+ export interface TextMessageSendingOptions extends MessageSendingOptions {
5
+ disableWebPreview?: boolean;
6
+ }
7
+ //# sourceMappingURL=messageSendingOptions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"messageSendingOptions.d.ts","sourceRoot":"","sources":["../../types/messageSendingOptions.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,qBAAqB;IAClC,GAAG,CAAC,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,yBAA0B,SAAQ,qBAAqB;IACpE,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC/B"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -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;
@@ -55,52 +55,63 @@ export class BotInstance {
55
55
  this.chats
56
56
  );
57
57
 
58
- this.telegraf.on('message', async (ctx) => {
59
- const msg = new IncomingMessage(ctx.update.message);
60
- const messageContent = msg.text || '<non-text message>';
61
-
62
- const messageFromName = msg.from?.first_name ?? 'Unknown';
63
- const messageFromId = msg.from?.id ?? 'Unknown';
64
- Logger.logWithTraceId(
65
- this.name,
66
- msg.traceId,
67
- msg.chatName,
68
- `${messageFromName} (${messageFromId}): ${messageContent}`
69
- );
58
+ this.initializeMessageProcessing();
59
+ this.initializeScheduledProcessing();
70
60
 
71
- if (msg.text) {
72
- this.messageQueue.push(msg);
73
- }
74
- });
61
+ this.storage.saveMetadata(
62
+ [...this.commands, ...this.scheduled],
63
+ this.name
64
+ );
75
65
 
76
66
  this.telegraf.launch();
67
+ }
77
68
 
78
- Scheduler.createTask(
79
- 'MessageProcessing',
80
- async () => {
81
- while (this.messageQueue.length > 0) {
82
- await this.processMessages();
83
- }
84
- },
85
- secondsToMilliseconds(0.3 as Seconds),
86
- false,
87
- this.name
88
- );
69
+ private initializeScheduledProcessing() {
70
+ if (this.scheduled.length > 0) {
71
+ Scheduler.createTask(
72
+ 'ScheduledProcessing',
73
+ async () => {
74
+ await this.runScheduled();
75
+ },
76
+ hoursToMilliseconds(0.5 as Hours),
77
+ true,
78
+ this.name
79
+ );
80
+ }
81
+ }
89
82
 
90
- Scheduler.createTask(
91
- 'ScheduledProcessing',
92
- async () => {
93
- await this.runScheduled();
94
- },
95
- hoursToMilliseconds(0.5 as Hours),
96
- true,
97
- this.name
98
- );
83
+ private initializeMessageProcessing() {
84
+ if (this.commands.length > 0) {
85
+ this.telegraf.on('message', async (ctx) => {
86
+ const msg = new IncomingMessage(ctx.update.message);
87
+ const messageContent = msg.text || '<non-text message>';
88
+
89
+ const messageFromName = msg.from?.first_name ?? 'Unknown';
90
+ const messageFromId = msg.from?.id ?? 'Unknown';
91
+ Logger.logWithTraceId(
92
+ this.name,
93
+ msg.traceId,
94
+ msg.chatName,
95
+ `${messageFromName} (${messageFromId}): ${messageContent}`
96
+ );
99
97
 
100
- this.storage.saveMetadata(
101
- [...this.commands, ...this.scheduled],
102
- this.name
103
- );
98
+ if (msg.text) {
99
+ this.messageQueue.push(msg);
100
+ }
101
+ });
102
+
103
+ Scheduler.createTask(
104
+ 'MessageProcessing',
105
+ async () => {
106
+ while (this.messageQueue.length > 0) {
107
+ await this.processMessages();
108
+ }
109
+ },
110
+ secondsToMilliseconds(0.3 as Seconds),
111
+ false,
112
+ this.name
113
+ );
114
+ }
104
115
  }
105
116
 
106
117
  async stop(code: string) {
@@ -123,7 +134,6 @@ export class BotInstance {
123
134
  try {
124
135
  await trig.exec(ctx);
125
136
  } catch (error) {
126
- console.dir(error);
127
137
  Logger.errorWithTraceId(
128
138
  ctx.botName,
129
139
  ctx.traceId,
@@ -5,6 +5,10 @@ import { ImageMessage } from '../responses/imageMessage';
5
5
  import { TextMessage } from '../responses/textMessage';
6
6
  import { VideoMessage } from '../responses/videoMessage';
7
7
  import { UnpinResponse } from '../responses/unpin';
8
+ import {
9
+ MessageSendingOptions,
10
+ TextMessageSendingOptions
11
+ } from '../../types/messageSendingOptions';
8
12
 
9
13
  export class ChatContext {
10
14
  botName: string;
@@ -33,25 +37,20 @@ export class ChatContext {
33
37
  this.storage = storage;
34
38
  }
35
39
 
36
- sendTextToChat(
37
- text: string,
38
- disableWebPreview?: boolean,
39
- pinned?: boolean
40
- ) {
40
+ sendTextToChat(text: string, options?: TextMessageSendingOptions) {
41
41
  this.interactions.respond(
42
42
  new TextMessage(
43
43
  text,
44
44
  this.chatId,
45
45
  undefined,
46
46
  this.traceId,
47
- disableWebPreview ?? false,
48
- pinned ?? false,
49
- this.actionKey
47
+ this.actionKey,
48
+ options
50
49
  )
51
50
  );
52
51
  }
53
52
 
54
- sendImageToChat(name: string, pinned?: boolean) {
53
+ sendImageToChat(name: string, options?: MessageSendingOptions) {
55
54
  const filePath = `./content/${name}.png`;
56
55
  this.interactions.respond(
57
56
  new ImageMessage(
@@ -59,13 +58,13 @@ export class ChatContext {
59
58
  this.chatId,
60
59
  undefined,
61
60
  this.traceId,
62
- pinned ?? false,
63
- this.actionKey
61
+ this.actionKey,
62
+ options
64
63
  )
65
64
  );
66
65
  }
67
66
 
68
- sendVideoToChat(name: string, pinned?: boolean) {
67
+ sendVideoToChat(name: string, options?: MessageSendingOptions) {
69
68
  const filePath = `./content/${name}.mp4`;
70
69
  this.interactions.respond(
71
70
  new VideoMessage(
@@ -73,8 +72,8 @@ export class ChatContext {
73
72
  this.chatId,
74
73
  undefined,
75
74
  this.traceId,
76
- pinned ?? false,
77
- this.actionKey
75
+ this.actionKey,
76
+ options
78
77
  )
79
78
  );
80
79
  }
@@ -9,6 +9,10 @@ import { TextMessage } from '../responses/textMessage';
9
9
  import { VideoMessage } from '../responses/videoMessage';
10
10
  import { ActionStateBase } from '../states/actionStateBase';
11
11
  import { ChatContext } from './chatContext';
12
+ import {
13
+ MessageSendingOptions,
14
+ TextMessageSendingOptions
15
+ } from '../../types/messageSendingOptions';
12
16
 
13
17
  export class MessageContext<
14
18
  TActionState extends IActionState
@@ -68,21 +72,20 @@ export class MessageContext<
68
72
  );
69
73
  }
70
74
 
71
- replyWithText(text: string, disableWebPreview?: boolean, pinned?: boolean) {
75
+ replyWithText(text: string, options?: TextMessageSendingOptions) {
72
76
  this.interactions.respond(
73
77
  new TextMessage(
74
78
  text,
75
79
  this.chatId,
76
80
  this.messageId,
77
81
  this.traceId,
78
- disableWebPreview ?? false,
79
- pinned ?? false,
80
- this.actionKey
82
+ this.actionKey,
83
+ options
81
84
  )
82
85
  );
83
86
  }
84
87
 
85
- replyWithImage(name: string, pinned?: boolean) {
88
+ replyWithImage(name: string, options?: MessageSendingOptions) {
86
89
  const filePath = `./content/${name}.png`;
87
90
  this.interactions.respond(
88
91
  new ImageMessage(
@@ -90,13 +93,13 @@ export class MessageContext<
90
93
  this.chatId,
91
94
  this.messageId,
92
95
  this.traceId,
93
- pinned ?? false,
94
- this.actionKey
96
+ this.actionKey,
97
+ options
95
98
  )
96
99
  );
97
100
  }
98
101
 
99
- replyWithVideo(name: string, pinned?: boolean) {
102
+ replyWithVideo(name: string, options?: MessageSendingOptions) {
100
103
  const filePath = `./content/${name}.mp4`;
101
104
  this.interactions.respond(
102
105
  new VideoMessage(
@@ -104,8 +107,8 @@ export class MessageContext<
104
107
  this.chatId,
105
108
  this.messageId,
106
109
  this.traceId,
107
- pinned ?? false,
108
- this.actionKey
110
+ this.actionKey,
111
+ options
109
112
  )
110
113
  );
111
114
  }
@@ -1,5 +1,6 @@
1
1
  import { InputFile } from 'telegraf/types';
2
2
  import { BotResponseTypes, IReplyMessage } from '../../types/response';
3
+ import { MessageSendingOptions } from '../../types/messageSendingOptions';
3
4
 
4
5
  export class ImageMessage implements IReplyMessage<InputFile> {
5
6
  kind = BotResponseTypes.image;
@@ -17,14 +18,14 @@ export class ImageMessage implements IReplyMessage<InputFile> {
17
18
  chatId: number,
18
19
  replyId: number | undefined,
19
20
  traceId: number | string,
20
- pinned: boolean,
21
- sourceActionKey: string
21
+ sourceActionKey: string,
22
+ options?: MessageSendingOptions
22
23
  ) {
23
24
  this.content = image;
24
25
  this.chatId = chatId;
25
26
  this.replyId = replyId;
26
27
  this.traceId = traceId;
27
- this.shouldPin = pinned;
28
+ this.shouldPin = options?.pin ?? false;
28
29
  this.sourceActionKey = sourceActionKey;
29
30
  }
30
31
  }
@@ -1,3 +1,4 @@
1
+ import { TextMessageSendingOptions } from '../../types/messageSendingOptions';
1
2
  import { BotResponseTypes, IReplyMessage } from '../../types/response';
2
3
 
3
4
  export class TextMessage implements IReplyMessage<string> {
@@ -16,16 +17,15 @@ export class TextMessage implements IReplyMessage<string> {
16
17
  chatId: number,
17
18
  replyId: number | undefined,
18
19
  traceId: string | number,
19
- disableWebPreview: boolean,
20
- pinned: boolean,
21
- sourceActionKey: string
20
+ sourceActionKey: string,
21
+ options?: TextMessageSendingOptions
22
22
  ) {
23
23
  this.content = text;
24
24
  this.chatId = chatId;
25
25
  this.replyId = replyId;
26
26
  this.traceId = traceId;
27
- this.disableWebPreview = disableWebPreview;
28
- this.shouldPin = pinned;
27
+ this.disableWebPreview = options?.disableWebPreview ?? false;
28
+ this.shouldPin = options?.pin ?? false;
29
29
  this.sourceActionKey = sourceActionKey;
30
30
  }
31
31
  }
@@ -1,5 +1,6 @@
1
1
  import { InputFile } from 'telegraf/types';
2
2
  import { BotResponseTypes, IReplyMessage } from '../../types/response';
3
+ import { MessageSendingOptions } from '../../types/messageSendingOptions';
3
4
 
4
5
  export class VideoMessage implements IReplyMessage<InputFile> {
5
6
  kind = BotResponseTypes.video;
@@ -17,14 +18,14 @@ export class VideoMessage implements IReplyMessage<InputFile> {
17
18
  chatId: number,
18
19
  replyId: number | undefined,
19
20
  traceId: number | string,
20
- pinned: boolean,
21
- sourceActionKey: string
21
+ sourceActionKey: string,
22
+ options?: MessageSendingOptions
22
23
  ) {
23
24
  this.content = video;
24
25
  this.chatId = chatId;
25
26
  this.replyId = replyId;
26
27
  this.traceId = traceId;
27
- this.shouldPin = pinned;
28
+ this.shouldPin = options?.pin ?? false;
28
29
  this.sourceActionKey = sourceActionKey;
29
30
  }
30
31
  }
@@ -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.21",
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>;
@@ -0,0 +1,7 @@
1
+ export interface MessageSendingOptions {
2
+ pin?: boolean;
3
+ }
4
+
5
+ export interface TextMessageSendingOptions extends MessageSendingOptions {
6
+ disableWebPreview?: boolean;
7
+ }