chz-telegram-bot 0.0.15 → 0.0.16

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 (118) hide show
  1. package/dist/entities/actionExecutionResult.d.ts +7 -0
  2. package/dist/entities/actionExecutionResult.d.ts.map +1 -0
  3. package/dist/entities/{transactionResult.js → actionExecutionResult.js} +3 -2
  4. package/dist/entities/actions/commandAction.d.ts +4 -4
  5. package/dist/entities/actions/commandAction.d.ts.map +1 -1
  6. package/dist/entities/actions/commandAction.js +12 -11
  7. package/dist/entities/actions/scheduledAction.d.ts +5 -5
  8. package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
  9. package/dist/entities/actions/scheduledAction.js +10 -9
  10. package/dist/entities/{bot.d.ts → botInstance.d.ts} +5 -5
  11. package/dist/entities/botInstance.d.ts.map +1 -0
  12. package/dist/entities/{bot.js → botInstance.js} +18 -20
  13. package/dist/entities/cachedStateFactory.d.ts +1 -1
  14. package/dist/entities/cachedStateFactory.d.ts.map +1 -1
  15. package/dist/entities/cachedStateFactory.js +2 -1
  16. package/dist/entities/commandTriggerCheckResult.d.ts +1 -1
  17. package/dist/entities/commandTriggerCheckResult.d.ts.map +1 -1
  18. package/dist/entities/commandTriggerCheckResult.js +2 -1
  19. package/dist/entities/context/chatContext.d.ts +1 -1
  20. package/dist/entities/context/chatContext.d.ts.map +1 -1
  21. package/dist/entities/context/chatContext.js +8 -10
  22. package/dist/entities/context/messageContext.d.ts +3 -3
  23. package/dist/entities/context/messageContext.d.ts.map +1 -1
  24. package/dist/entities/context/messageContext.js +14 -16
  25. package/dist/entities/incomingMessage.d.ts +1 -1
  26. package/dist/entities/incomingMessage.d.ts.map +1 -1
  27. package/dist/entities/incomingMessage.js +2 -1
  28. package/dist/entities/responses/imageMessage.d.ts +2 -2
  29. package/dist/entities/responses/imageMessage.d.ts.map +1 -1
  30. package/dist/entities/responses/imageMessage.js +2 -1
  31. package/dist/entities/responses/reaction.d.ts +1 -1
  32. package/dist/entities/responses/reaction.d.ts.map +1 -1
  33. package/dist/entities/responses/reaction.js +2 -1
  34. package/dist/entities/responses/textMessage.d.ts +2 -2
  35. package/dist/entities/responses/textMessage.d.ts.map +1 -1
  36. package/dist/entities/responses/textMessage.js +2 -1
  37. package/dist/entities/responses/videoMessage.d.ts +2 -2
  38. package/dist/entities/responses/videoMessage.d.ts.map +1 -1
  39. package/dist/entities/responses/videoMessage.js +2 -1
  40. package/dist/entities/states/actionStateBase.d.ts +2 -2
  41. package/dist/entities/states/actionStateBase.d.ts.map +1 -1
  42. package/dist/entities/states/actionStateBase.js +2 -1
  43. package/dist/entities/taskRecord.d.ts +1 -1
  44. package/dist/entities/taskRecord.d.ts.map +1 -1
  45. package/dist/entities/taskRecord.js +2 -1
  46. package/dist/helpers/builders/commandActionBuilder.d.ts +3 -3
  47. package/dist/helpers/builders/commandActionBuilder.d.ts.map +1 -1
  48. package/dist/helpers/builders/commandActionBuilder.js +9 -12
  49. package/dist/helpers/builders/scheduledActionBuilder.d.ts +3 -3
  50. package/dist/helpers/builders/scheduledActionBuilder.d.ts.map +1 -1
  51. package/dist/helpers/builders/scheduledActionBuilder.js +8 -10
  52. package/dist/helpers/noop.d.ts +1 -2
  53. package/dist/helpers/noop.d.ts.map +1 -1
  54. package/dist/helpers/noop.js +2 -1
  55. package/dist/helpers/toArray.d.ts +1 -1
  56. package/dist/helpers/toArray.d.ts.map +1 -1
  57. package/dist/helpers/toArray.js +1 -1
  58. package/dist/main.d.ts +5 -5
  59. package/dist/main.d.ts.map +1 -1
  60. package/dist/main.js +6 -9
  61. package/dist/services/jsonFileStorage.d.ts +5 -5
  62. package/dist/services/jsonFileStorage.d.ts.map +1 -1
  63. package/dist/services/jsonFileStorage.js +2 -1
  64. package/dist/services/logger.d.ts +3 -3
  65. package/dist/services/logger.d.ts.map +1 -1
  66. package/dist/services/logger.js +3 -2
  67. package/dist/services/taskScheduler.d.ts +3 -3
  68. package/dist/services/taskScheduler.d.ts.map +1 -1
  69. package/dist/services/taskScheduler.js +8 -10
  70. package/dist/services/telegramApi.d.ts +7 -7
  71. package/dist/services/telegramApi.d.ts.map +1 -1
  72. package/dist/services/telegramApi.js +17 -19
  73. package/dist/types/actionState.d.ts +1 -1
  74. package/dist/types/actionState.d.ts.map +1 -1
  75. package/dist/types/actionWithState.d.ts +2 -2
  76. package/dist/types/actionWithState.d.ts.map +1 -1
  77. package/dist/types/commandCondition.d.ts +2 -2
  78. package/dist/types/commandCondition.d.ts.map +1 -1
  79. package/dist/types/handlers.d.ts +3 -3
  80. package/dist/types/handlers.d.ts.map +1 -1
  81. package/dist/types/replyMessage.d.ts +1 -1
  82. package/dist/types/replyMessage.d.ts.map +1 -1
  83. package/dist/types/storage.d.ts +4 -4
  84. package/dist/types/storage.d.ts.map +1 -1
  85. package/entities/{transactionResult.ts → actionExecutionResult.ts} +2 -2
  86. package/entities/actions/commandAction.ts +10 -10
  87. package/entities/actions/scheduledAction.ts +13 -13
  88. package/entities/{bot.ts → botInstance.ts} +17 -17
  89. package/entities/cachedStateFactory.ts +1 -1
  90. package/entities/commandTriggerCheckResult.ts +1 -1
  91. package/entities/context/chatContext.ts +4 -4
  92. package/entities/context/messageContext.ts +8 -8
  93. package/entities/incomingMessage.ts +1 -1
  94. package/entities/responses/imageMessage.ts +2 -2
  95. package/entities/responses/reaction.ts +1 -1
  96. package/entities/responses/textMessage.ts +2 -2
  97. package/entities/responses/videoMessage.ts +2 -2
  98. package/entities/states/actionStateBase.ts +2 -2
  99. package/entities/taskRecord.ts +1 -1
  100. package/helpers/builders/commandActionBuilder.ts +5 -5
  101. package/helpers/builders/scheduledActionBuilder.ts +4 -4
  102. package/helpers/noop.ts +1 -3
  103. package/helpers/toArray.ts +1 -1
  104. package/main.ts +9 -9
  105. package/package.json +1 -1
  106. package/services/jsonFileStorage.ts +6 -6
  107. package/services/logger.ts +2 -2
  108. package/services/taskScheduler.ts +6 -6
  109. package/services/telegramApi.ts +15 -15
  110. package/types/actionState.ts +1 -1
  111. package/types/actionWithState.ts +2 -2
  112. package/types/commandCondition.ts +2 -2
  113. package/types/handlers.ts +3 -3
  114. package/types/replyMessage.ts +1 -1
  115. package/types/storage.ts +4 -5
  116. package/dist/entities/bot.d.ts.map +0 -1
  117. package/dist/entities/transactionResult.d.ts +0 -7
  118. package/dist/entities/transactionResult.d.ts.map +0 -1
@@ -1,5 +1,5 @@
1
- import IActionState from './actionState';
2
- export default interface IActionWithState {
1
+ import { IActionState } from './actionState';
2
+ export interface IActionWithState {
3
3
  key: string;
4
4
  stateConstructor: () => IActionState;
5
5
  }
@@ -1 +1 @@
1
- {"version":3,"file":"actionWithState.d.ts","sourceRoot":"","sources":["../../types/actionWithState.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,eAAe,CAAC;AAEzC,MAAM,CAAC,OAAO,WAAW,gBAAgB;IACrC,GAAG,EAAE,MAAM,CAAC;IACZ,gBAAgB,EAAE,MAAM,YAAY,CAAC;CACxC"}
1
+ {"version":3,"file":"actionWithState.d.ts","sourceRoot":"","sources":["../../types/actionWithState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,MAAM,WAAW,gBAAgB;IAC7B,GAAG,EAAE,MAAM,CAAC;IACZ,gBAAgB,EAAE,MAAM,YAAY,CAAC;CACxC"}
@@ -1,4 +1,4 @@
1
- import MessageContext from '../entities/context/messageContext';
2
- import IActionState from './actionState';
1
+ import { MessageContext } from '../entities/context/messageContext';
2
+ import { IActionState } from './actionState';
3
3
  export type CommandCondition<TActionState extends IActionState> = (ctx: MessageContext<TActionState>) => Promise<boolean>;
4
4
  //# sourceMappingURL=commandCondition.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"commandCondition.d.ts","sourceRoot":"","sources":["../../types/commandCondition.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,oCAAoC,CAAC;AAChE,OAAO,YAAY,MAAM,eAAe,CAAC;AAEzC,MAAM,MAAM,gBAAgB,CAAC,YAAY,SAAS,YAAY,IAAI,CAC9D,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC,KAChC,OAAO,CAAC,OAAO,CAAC,CAAC"}
1
+ {"version":3,"file":"commandCondition.d.ts","sourceRoot":"","sources":["../../types/commandCondition.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,MAAM,MAAM,gBAAgB,CAAC,YAAY,SAAS,YAAY,IAAI,CAC9D,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC,KAChC,OAAO,CAAC,OAAO,CAAC,CAAC"}
@@ -1,6 +1,6 @@
1
- import ChatContext from '../entities/context/chatContext';
2
- import MessageContext from '../entities/context/messageContext';
3
- import IActionState from './actionState';
1
+ import { ChatContext } from '../entities/context/chatContext';
2
+ import { MessageContext } from '../entities/context/messageContext';
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
6
  export type ScheduledHandler = (ctx: ChatContext, getCached: CachedValueAccessor) => Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"handlers.d.ts","sourceRoot":"","sources":["../../types/handlers.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,MAAM,iCAAiC,CAAC;AAC1D,OAAO,cAAc,MAAM,oCAAoC,CAAC;AAChE,OAAO,YAAY,MAAM,eAAe,CAAC;AACzC,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,GAAG,CAC3B,GAAG,EAAE,WAAW,EAChB,SAAS,EAAE,mBAAmB,KAC7B,OAAO,CAAC,IAAI,CAAC,CAAC"}
@@ -1,4 +1,4 @@
1
- export default interface IReplyMessage<TType> {
1
+ export interface IReplyMessage<TType> {
2
2
  content: TType;
3
3
  chatId: number;
4
4
  replyId: number | undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"replyMessage.d.ts","sourceRoot":"","sources":["../../types/replyMessage.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,WAAW,aAAa,CAAC,KAAK;IACxC,OAAO,EAAE,KAAK,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5B,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,iBAAiB,EAAE,OAAO,CAAC;CAC9B"}
1
+ {"version":3,"file":"replyMessage.d.ts","sourceRoot":"","sources":["../../types/replyMessage.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa,CAAC,KAAK;IAChC,OAAO,EAAE,KAAK,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5B,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,iBAAiB,EAAE,OAAO,CAAC;CAC9B"}
@@ -1,11 +1,11 @@
1
- import TransactionResult from '../entities/transactionResult';
2
- import IActionState from './actionState';
3
- import IActionWithState from './actionWithState';
1
+ import { ActionExecutionResult } from '../entities/actionExecutionResult';
2
+ import { IActionState } from './actionState';
3
+ import { IActionWithState } from './actionWithState';
4
4
  export interface IStorageClient {
5
5
  close(): Promise<void>;
6
6
  load<TActionState extends IActionState>(key: string): Promise<Record<number, TActionState>>;
7
7
  saveMetadata(actions: IActionWithState[], botName: string): Promise<void>;
8
8
  getActionState<TActionState extends IActionState>(entity: IActionWithState, chatId: number): Promise<TActionState>;
9
- saveActionExecutionResult(action: IActionWithState, chatId: number, transactionResult: TransactionResult): Promise<void>;
9
+ saveActionExecutionResult(action: IActionWithState, chatId: number, transactionResult: ActionExecutionResult): Promise<void>;
10
10
  }
11
11
  //# sourceMappingURL=storage.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../types/storage.ts"],"names":[],"mappings":"AACA,OAAO,iBAAiB,MAAM,+BAA+B,CAAC;AAC9D,OAAO,YAAY,MAAM,eAAe,CAAC;AACzC,OAAO,gBAAgB,MAAM,mBAAmB,CAAC;AAEjD,MAAM,WAAW,cAAc;IAC3B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,IAAI,CAAC,YAAY,SAAS,YAAY,EAClC,GAAG,EAAE,MAAM,GACZ,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC;IACzC,YAAY,CAAC,OAAO,EAAE,gBAAgB,EAAE,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1E,cAAc,CAAC,YAAY,SAAS,YAAY,EAC5C,MAAM,EAAE,gBAAgB,EACxB,MAAM,EAAE,MAAM,GACf,OAAO,CAAC,YAAY,CAAC,CAAC;IACzB,yBAAyB,CACrB,MAAM,EAAE,gBAAgB,EACxB,MAAM,EAAE,MAAM,EACd,iBAAiB,EAAE,iBAAiB,GACrC,OAAO,CAAC,IAAI,CAAC,CAAC;CACpB"}
1
+ {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../types/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAErD,MAAM,WAAW,cAAc;IAC3B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,IAAI,CAAC,YAAY,SAAS,YAAY,EAClC,GAAG,EAAE,MAAM,GACZ,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC;IACzC,YAAY,CAAC,OAAO,EAAE,gBAAgB,EAAE,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1E,cAAc,CAAC,YAAY,SAAS,YAAY,EAC5C,MAAM,EAAE,gBAAgB,EACxB,MAAM,EAAE,MAAM,GACf,OAAO,CAAC,YAAY,CAAC,CAAC;IACzB,yBAAyB,CACrB,MAAM,EAAE,gBAAgB,EACxB,MAAM,EAAE,MAAM,EACd,iBAAiB,EAAE,qBAAqB,GACzC,OAAO,CAAC,IAAI,CAAC,CAAC;CACpB"}
@@ -1,6 +1,6 @@
1
- import IActionState from '../types/actionState';
1
+ import { IActionState } from '../types/actionState';
2
2
 
3
- export default class TransactionResult {
3
+ export class ActionExecutionResult {
4
4
  data: IActionState;
5
5
  shouldUpdate: boolean;
6
6
 
@@ -1,17 +1,17 @@
1
- import ActionExecutionResult from '../transactionResult';
2
1
  import moment from 'moment';
3
- import logger from '../../services/logger';
4
- import MessageContext from '../context/messageContext';
5
- import IActionWithState from '../../types/actionWithState';
6
- import toArray from '../../helpers/toArray';
7
- import IActionState from '../../types/actionState';
8
- import CommandTriggerCheckResult from '../commandTriggerCheckResult';
9
2
  import { CommandHandler } from '../../types/handlers';
10
3
  import { CommandCondition } from '../../types/commandCondition';
11
4
  import { Seconds } from '../../types/timeValues';
12
5
  import { secondsToMilliseconds } from '../../helpers/timeConvertions';
13
-
14
- export default class CommandAction<TActionState extends IActionState>
6
+ import { toArray } from '../../helpers/toArray';
7
+ import { IActionState } from '../../types/actionState';
8
+ import { IActionWithState } from '../../types/actionWithState';
9
+ import { CommandTriggerCheckResult } from '../commandTriggerCheckResult';
10
+ import { MessageContext } from '../context/messageContext';
11
+ import { Logger } from '../../services/logger';
12
+ import { ActionExecutionResult } from '../actionExecutionResult';
13
+
14
+ export class CommandAction<TActionState extends IActionState>
15
15
  implements IActionWithState
16
16
  {
17
17
  triggers: (string | RegExp)[];
@@ -70,7 +70,7 @@ export default class CommandAction<TActionState extends IActionState>
70
70
 
71
71
  if (!shouldTrigger) return;
72
72
 
73
- logger.logWithTraceId(
73
+ Logger.logWithTraceId(
74
74
  ctx.botName,
75
75
  ctx.traceId,
76
76
  ctx.chatName,
@@ -1,18 +1,18 @@
1
- import TransactionResult from '../transactionResult';
2
1
  import moment from 'moment';
3
- import logger from '../../services/logger';
4
- import taskScheduler from '../../services/taskScheduler';
5
2
  import { Sema as Semaphore } from 'async-sema';
6
- import ChatContext from '../context/chatContext';
7
- import IActionWithState from '../../types/actionWithState';
8
- import ActionStateBase from '../states/actionStateBase';
9
- import IActionState from '../../types/actionState';
10
3
  import { ScheduledHandler } from '../../types/handlers';
11
- import CachedStateFactory from '../cachedStateFactory';
12
4
  import { hoursToMilliseconds } from '../../helpers/timeConvertions';
13
5
  import { HoursOfDay } from '../../types/timeValues';
14
-
15
- export default class ScheduledAction implements IActionWithState {
6
+ import { IActionState } from '../../types/actionState';
7
+ import { IActionWithState } from '../../types/actionWithState';
8
+ import { CachedStateFactory } from '../cachedStateFactory';
9
+ import { ChatContext } from '../context/chatContext';
10
+ import { ActionStateBase } from '../states/actionStateBase';
11
+ import { ActionExecutionResult } from '../actionExecutionResult';
12
+ import { Logger } from '../../services/logger';
13
+ import { Scheduler } from '../../services/taskScheduler';
14
+
15
+ export class ScheduledAction implements IActionWithState {
16
16
  static semaphore = new Semaphore(1);
17
17
 
18
18
  name: string;
@@ -50,7 +50,7 @@ export default class ScheduledAction implements IActionWithState {
50
50
  const isAllowedToTrigger = this.shouldTrigger(state);
51
51
 
52
52
  if (isAllowedToTrigger) {
53
- logger.logWithTraceId(
53
+ Logger.logWithTraceId(
54
54
  ctx.botName,
55
55
  ctx.traceId,
56
56
  ctx.chatName,
@@ -66,7 +66,7 @@ export default class ScheduledAction implements IActionWithState {
66
66
  await ctx.storage.saveActionExecutionResult(
67
67
  this,
68
68
  ctx.chatId,
69
- new TransactionResult(state, isAllowedToTrigger)
69
+ new ActionExecutionResult(state, isAllowedToTrigger)
70
70
  );
71
71
  }
72
72
  }
@@ -93,7 +93,7 @@ export default class ScheduledAction implements IActionWithState {
93
93
 
94
94
  this.cachedState.set(key, value);
95
95
 
96
- taskScheduler.createOnetimeTask(
96
+ Scheduler.createOnetimeTask(
97
97
  `Drop cached value [${this.name} : ${key}]`,
98
98
  () => this.cachedState.delete(key),
99
99
  hoursToMilliseconds(
@@ -1,20 +1,20 @@
1
1
  import { Telegraf } from 'telegraf';
2
- import TelegramApiService from '../services/telegramApi';
3
- import IncomingMessage from './incomingMessage';
4
- import taskScheduler from '../services/taskScheduler';
5
- import logger from '../services/logger';
6
- import CommandAction from './actions/commandAction';
7
- import ScheduledAction from './actions/scheduledAction';
8
- import IActionState from '../types/actionState';
9
2
  import {
10
3
  hoursToMilliseconds,
11
4
  secondsToMilliseconds
12
5
  } from '../helpers/timeConvertions';
13
6
  import { Hours, Seconds } from '../types/timeValues';
14
- import JsonFileStorage from '../services/jsonFileStorage';
15
7
  import { IStorageClient } from '../types/storage';
16
-
17
- export default class Bot {
8
+ import { JsonFileStorage } from '../services/jsonFileStorage';
9
+ import { TelegramApiService } from '../services/telegramApi';
10
+ import { IActionState } from '../types/actionState';
11
+ import { CommandAction } from './actions/commandAction';
12
+ import { ScheduledAction } from './actions/scheduledAction';
13
+ import { Logger } from '../services/logger';
14
+ import { Scheduler } from '../services/taskScheduler';
15
+ import { IncomingMessage } from './incomingMessage';
16
+
17
+ export class BotInstance {
18
18
  name: string;
19
19
  private api: TelegramApiService;
20
20
  private telegraf: Telegraf;
@@ -38,7 +38,7 @@ export default class Bot {
38
38
  this.scheduled = options.scheduled;
39
39
  this.chats = options.chats;
40
40
 
41
- logger.logWithTraceId(
41
+ Logger.logWithTraceId(
42
42
  this.name,
43
43
  `System:Bot-${this.name}-Start`,
44
44
  'System',
@@ -61,7 +61,7 @@ export default class Bot {
61
61
 
62
62
  const messageFromName = msg.from?.first_name ?? 'Unknown';
63
63
  const messageFromId = msg.from?.id ?? 'Unknown';
64
- logger.logWithTraceId(
64
+ Logger.logWithTraceId(
65
65
  this.name,
66
66
  msg.traceId,
67
67
  msg.chatName,
@@ -75,7 +75,7 @@ export default class Bot {
75
75
 
76
76
  this.telegraf.launch();
77
77
 
78
- taskScheduler.createTask(
78
+ Scheduler.createTask(
79
79
  'MessageProcessing',
80
80
  async () => {
81
81
  while (this.messageQueue.length > 0) {
@@ -87,7 +87,7 @@ export default class Bot {
87
87
  this.name
88
88
  );
89
89
 
90
- taskScheduler.createTask(
90
+ Scheduler.createTask(
91
91
  'ScheduledProcessing',
92
92
  async () => {
93
93
  await this.runScheduled();
@@ -104,7 +104,7 @@ export default class Bot {
104
104
  }
105
105
 
106
106
  async stop(code: string) {
107
- logger.logWithTraceId(
107
+ Logger.logWithTraceId(
108
108
  this.name,
109
109
  `System:Bot-${this.name}-Stop`,
110
110
  'System',
@@ -124,7 +124,7 @@ export default class Bot {
124
124
  await trig.exec(ctx);
125
125
  } catch (error) {
126
126
  console.dir(error);
127
- logger.errorWithTraceId(
127
+ Logger.errorWithTraceId(
128
128
  ctx.botName,
129
129
  ctx.traceId,
130
130
  chatName,
@@ -145,7 +145,7 @@ export default class Bot {
145
145
  try {
146
146
  await cmd.exec(ctx);
147
147
  } catch (error) {
148
- logger.errorWithTraceId(
148
+ Logger.errorWithTraceId(
149
149
  ctx.botName,
150
150
  ctx.traceId,
151
151
  ctx.chatName,
@@ -1,6 +1,6 @@
1
1
  import { Hours } from '../types/timeValues';
2
2
 
3
- export default class CachedStateFactory {
3
+ export class CachedStateFactory {
4
4
  getValue: () => Promise<unknown>;
5
5
  invalidationTimeoutInHours: Hours;
6
6
 
@@ -1,4 +1,4 @@
1
- export default class CommandTriggerCheckResult {
1
+ export class CommandTriggerCheckResult {
2
2
  static get DontTriggerAndSkipCooldown() {
3
3
  return new CommandTriggerCheckResult(false, [], true);
4
4
  }
@@ -1,11 +1,11 @@
1
- import ImageMessage from '../responses/imageMessage';
2
- import TextMessage from '../responses/textMessage';
3
- import VideoMessage from '../responses/videoMessage';
4
1
  import { resolve } from 'path';
5
2
  import { IBotApiInteractions } from '../../services/telegramApi';
6
3
  import { IStorageClient } from '../../types/storage';
4
+ import { ImageMessage } from '../responses/imageMessage';
5
+ import { TextMessage } from '../responses/textMessage';
6
+ import { VideoMessage } from '../responses/videoMessage';
7
7
 
8
- export default class ChatContext {
8
+ export class ChatContext {
9
9
  botName: string;
10
10
  interactions: IBotApiInteractions;
11
11
  chatId: number;
@@ -1,16 +1,16 @@
1
- import ActionStateBase from '../states/actionStateBase';
2
- import ImageMessage from '../responses/imageMessage';
3
- import TextMessage from '../responses/textMessage';
4
- import VideoMessage from '../responses/videoMessage';
5
- import ChatContext from './chatContext';
6
1
  import { resolve } from 'path';
7
- import IActionState from '../../types/actionState';
8
2
  import { IBotApiInteractions } from '../../services/telegramApi';
9
3
  import { TelegramEmoji } from 'telegraf/types';
10
- import Reaction from '../responses/reaction';
11
4
  import { IStorageClient } from '../../types/storage';
5
+ import { IActionState } from '../../types/actionState';
6
+ import { ImageMessage } from '../responses/imageMessage';
7
+ import { Reaction } from '../responses/reaction';
8
+ import { TextMessage } from '../responses/textMessage';
9
+ import { VideoMessage } from '../responses/videoMessage';
10
+ import { ActionStateBase } from '../states/actionStateBase';
11
+ import { ChatContext } from './chatContext';
12
12
 
13
- export default class MessageContext<
13
+ export class MessageContext<
14
14
  TActionState extends IActionState
15
15
  > extends ChatContext {
16
16
  messageId: number;
@@ -1,7 +1,7 @@
1
1
  import { Chat, User } from 'telegraf/types';
2
2
  import { randomInt } from 'crypto';
3
3
 
4
- export default class IncomingMessage {
4
+ export class IncomingMessage {
5
5
  message_id: number;
6
6
  chat: Chat;
7
7
  from: User | undefined;
@@ -1,7 +1,7 @@
1
1
  import { InputFile } from 'telegraf/types';
2
- import IReplyMessage from '../../types/replyMessage';
2
+ import { IReplyMessage } from '../../types/replyMessage';
3
3
 
4
- export default class ImageMessage implements IReplyMessage<InputFile> {
4
+ export class ImageMessage implements IReplyMessage<InputFile> {
5
5
  content: InputFile;
6
6
  chatId: number;
7
7
  replyId: number | undefined;
@@ -1,6 +1,6 @@
1
1
  import { TelegramEmoji } from 'telegraf/types';
2
2
 
3
- export default class Reaction {
3
+ export class Reaction {
4
4
  chatId: number;
5
5
  messageId: number;
6
6
  traceId: number | string;
@@ -1,6 +1,6 @@
1
- import IReplyMessage from '../../types/replyMessage';
1
+ import { IReplyMessage } from '../../types/replyMessage';
2
2
 
3
- export default class TextMessage implements IReplyMessage<string> {
3
+ export class TextMessage implements IReplyMessage<string> {
4
4
  content: string;
5
5
  chatId: number;
6
6
  replyId: number | undefined;
@@ -1,7 +1,7 @@
1
1
  import { InputFile } from 'telegraf/types';
2
- import IReplyMessage from '../../types/replyMessage';
2
+ import { IReplyMessage } from '../../types/replyMessage';
3
3
 
4
- export default class VideoMessage implements IReplyMessage<InputFile> {
4
+ export class VideoMessage implements IReplyMessage<InputFile> {
5
5
  content: InputFile;
6
6
  chatId: number;
7
7
  replyId: number | undefined;
@@ -1,5 +1,5 @@
1
- import IActionState from '../../types/actionState';
1
+ import { IActionState } from '../../types/actionState';
2
2
 
3
- export default class ActionStateBase implements IActionState {
3
+ export class ActionStateBase implements IActionState {
4
4
  lastExecutedDate = 0;
5
5
  }
@@ -1,6 +1,6 @@
1
1
  import { Milliseconds } from '../types/timeValues';
2
2
 
3
- export default class TaskRecord {
3
+ export class TaskRecord {
4
4
  name: string;
5
5
  taskId: NodeJS.Timeout;
6
6
  interval: Milliseconds;
@@ -1,11 +1,11 @@
1
- import CommandAction from '../../entities/actions/commandAction';
2
- import ActionStateBase from '../../entities/states/actionStateBase';
3
- import toArray from '../toArray';
4
- import IActionState from '../../types/actionState';
5
1
  import { CommandHandler } from '../../types/handlers';
6
2
  import { CommandCondition } from '../../types/commandCondition';
7
3
  import { Seconds } from '../../types/timeValues';
8
- import Noop from '../noop';
4
+ import { CommandAction } from '../../entities/actions/commandAction';
5
+ import { ActionStateBase } from '../../entities/states/actionStateBase';
6
+ import { IActionState } from '../../types/actionState';
7
+ import { toArray } from '../toArray';
8
+ import { Noop } from '../noop';
9
9
 
10
10
  export class CommandActionBuilderWithState<TActionState extends IActionState> {
11
11
  name: string;
@@ -1,10 +1,10 @@
1
- import ScheduledAction from '../../entities/actions/scheduledAction';
2
- import CachedStateFactory from '../../entities/cachedStateFactory';
1
+ import { ScheduledAction } from '../../entities/actions/scheduledAction';
2
+ import { CachedStateFactory } from '../../entities/cachedStateFactory';
3
3
  import { ScheduledHandler } from '../../types/handlers';
4
4
  import { Hours, HoursOfDay } from '../../types/timeValues';
5
- import Noop from '../noop';
5
+ import { Noop } from '../noop';
6
6
 
7
- export default class ScheduledActionBuilder {
7
+ export class ScheduledActionBuilder {
8
8
  active = true;
9
9
  time: HoursOfDay = 0;
10
10
  cachedStateFactories = new Map<string, CachedStateFactory>();
package/helpers/noop.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable @typescript-eslint/no-unused-vars */
2
- class Noop {
2
+ export class Noop {
3
3
  static async true<T1>(arg1: T1) {
4
4
  return true;
5
5
  }
@@ -9,5 +9,3 @@ class Noop {
9
9
  static async call<T1, T2>(arg1: T1, arg2: T2): Promise<void>;
10
10
  static async call<T1>(arg1: T1) {}
11
11
  }
12
-
13
- export default Noop;
@@ -1,3 +1,3 @@
1
- export default function toArray<TType>(value: TType | TType[]) {
1
+ export function toArray<TType>(value: TType | TType[]) {
2
2
  return Array.isArray(value) ? value : [value];
3
3
  }
package/main.ts CHANGED
@@ -1,16 +1,16 @@
1
1
  import { readFile } from 'fs/promises';
2
- import BotInstance from './entities/bot.js';
3
- import taskScheduler from './services/taskScheduler.js';
4
- import logger from './services/logger.js';
5
- import CommandAction from './entities/actions/commandAction.js';
6
- import IActionState from './types/actionState.js';
7
- import ScheduledAction from './entities/actions/scheduledAction.js';
8
- import { IStorageClient } from './types/storage.js';
2
+ import { IStorageClient } from './types/storage';
3
+ import { Logger } from './services/logger';
4
+ import { CommandAction } from './entities/actions/commandAction';
5
+ import { ScheduledAction } from './entities/actions/scheduledAction';
6
+ import { IActionState } from './types/actionState';
7
+ import { Scheduler } from './services/taskScheduler';
8
+ import { BotInstance } from './entities/botInstance';
9
9
 
10
10
  const bots: BotInstance[] = [];
11
11
 
12
12
  function log(text: string) {
13
- logger.logWithTraceId('ALL BOTS', 'System:Bot', 'System', text);
13
+ Logger.logWithTraceId('ALL BOTS', 'System:Bot', 'System', text);
14
14
  }
15
15
 
16
16
  async function startBot(options: {
@@ -39,7 +39,7 @@ async function startBot(options: {
39
39
 
40
40
  async function stopBots(reason: string) {
41
41
  log(`Recieved termination code: ${reason}`);
42
- taskScheduler.stopAll();
42
+ Scheduler.stopAll();
43
43
  log('Acquiring storage semaphore...');
44
44
 
45
45
  log('Stopping bots...');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chz-telegram-bot",
3
- "version": "0.0.15",
3
+ "version": "0.0.16",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "async-sema": "^3.1.1",
@@ -1,14 +1,14 @@
1
1
  import { existsSync, mkdirSync } from 'fs';
2
2
  import { dirname } from 'path';
3
- import TransactionResult from '../entities/transactionResult';
4
3
  import { mkdir, readFile, writeFile } from 'fs/promises';
5
- import ActionStateBase from '../entities/states/actionStateBase';
6
- import IActionState from '../types/actionState';
7
- import IActionWithState from '../types/actionWithState';
8
4
  import { Sema as Semaphore } from 'async-sema';
9
5
  import { IStorageClient } from '../types/storage';
6
+ import { ActionStateBase } from '../entities/states/actionStateBase';
7
+ import { ActionExecutionResult } from '../entities/actionExecutionResult';
8
+ import { IActionState } from '../types/actionState';
9
+ import { IActionWithState } from '../types/actionWithState';
10
10
 
11
- export default class JsonFileStorage implements IStorageClient {
11
+ export class JsonFileStorage implements IStorageClient {
12
12
  semaphore = new Semaphore(1);
13
13
  private cache: Map<string, Record<number, IActionState>>;
14
14
  private storagePath: string;
@@ -105,7 +105,7 @@ export default class JsonFileStorage implements IStorageClient {
105
105
  async saveActionExecutionResult(
106
106
  action: IActionWithState,
107
107
  chatId: number,
108
- transactionResult: TransactionResult
108
+ transactionResult: ActionExecutionResult
109
109
  ) {
110
110
  await this.lock(async () => {
111
111
  const data = await this.loadInternal(action.key);
@@ -1,4 +1,4 @@
1
- class Logger {
1
+ class JsonLogger {
2
2
  logWithTraceId(
3
3
  botName: string,
4
4
  traceId: string | number,
@@ -27,4 +27,4 @@ class Logger {
27
27
  }
28
28
  }
29
29
 
30
- export default new Logger();
30
+ export const Logger = new JsonLogger();
@@ -1,7 +1,7 @@
1
- import TaskRecord from '../entities/taskRecord';
1
+ import { TaskRecord } from '../entities/taskRecord';
2
2
  import { secondsToMilliseconds } from '../helpers/timeConvertions';
3
3
  import { Milliseconds, Seconds } from '../types/timeValues';
4
- import logger from './logger';
4
+ import { Logger } from './logger';
5
5
 
6
6
  class TaskScheduler {
7
7
  activeTasks: TaskRecord[] = [];
@@ -27,7 +27,7 @@ class TaskScheduler {
27
27
  setTimeout(action, secondsToMilliseconds(1 as Seconds));
28
28
  }
29
29
 
30
- logger.logWithTraceId(
30
+ Logger.logWithTraceId(
31
31
  ownerName,
32
32
  `System:TaskScheduler-${ownerName}-${name}`,
33
33
  'System',
@@ -44,7 +44,7 @@ class TaskScheduler {
44
44
  ownerName: string
45
45
  ) {
46
46
  const actionWrapper = () => {
47
- logger.logWithTraceId(
47
+ Logger.logWithTraceId(
48
48
  ownerName,
49
49
  `System:TaskScheduler-${ownerName}-${name}`,
50
50
  'System',
@@ -54,7 +54,7 @@ class TaskScheduler {
54
54
  };
55
55
  const taskId = setTimeout(actionWrapper, delay);
56
56
 
57
- logger.logWithTraceId(
57
+ Logger.logWithTraceId(
58
58
  ownerName,
59
59
  `System:TaskScheduler-${ownerName}-${name}`,
60
60
  'System',
@@ -63,4 +63,4 @@ class TaskScheduler {
63
63
  }
64
64
  }
65
65
 
66
- export default new TaskScheduler();
66
+ export const Scheduler = new TaskScheduler();