chz-telegram-bot 0.0.15 → 0.0.17

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 (126) 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} +20 -22
  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 +7 -5
  20. package/dist/entities/context/chatContext.d.ts.map +1 -1
  21. package/dist/entities/context/chatContext.js +17 -14
  22. package/dist/entities/context/messageContext.d.ts +7 -7
  23. package/dist/entities/context/messageContext.d.ts.map +1 -1
  24. package/dist/entities/context/messageContext.js +19 -21
  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 +6 -3
  29. package/dist/entities/responses/imageMessage.d.ts.map +1 -1
  30. package/dist/entities/responses/imageMessage.js +7 -2
  31. package/dist/entities/responses/reaction.d.ts +5 -2
  32. package/dist/entities/responses/reaction.d.ts.map +1 -1
  33. package/dist/entities/responses/reaction.js +6 -2
  34. package/dist/entities/responses/textMessage.d.ts +6 -3
  35. package/dist/entities/responses/textMessage.d.ts.map +1 -1
  36. package/dist/entities/responses/textMessage.js +7 -2
  37. package/dist/entities/responses/unpin.d.ts +10 -0
  38. package/dist/entities/responses/unpin.d.ts.map +1 -0
  39. package/dist/entities/responses/unpin.js +14 -0
  40. package/dist/entities/responses/videoMessage.d.ts +6 -3
  41. package/dist/entities/responses/videoMessage.d.ts.map +1 -1
  42. package/dist/entities/responses/videoMessage.js +7 -2
  43. package/dist/entities/states/actionStateBase.d.ts +3 -2
  44. package/dist/entities/states/actionStateBase.d.ts.map +1 -1
  45. package/dist/entities/states/actionStateBase.js +3 -1
  46. package/dist/entities/taskRecord.d.ts +1 -1
  47. package/dist/entities/taskRecord.d.ts.map +1 -1
  48. package/dist/entities/taskRecord.js +2 -1
  49. package/dist/helpers/builders/commandActionBuilder.d.ts +3 -3
  50. package/dist/helpers/builders/commandActionBuilder.d.ts.map +1 -1
  51. package/dist/helpers/builders/commandActionBuilder.js +9 -12
  52. package/dist/helpers/builders/scheduledActionBuilder.d.ts +3 -3
  53. package/dist/helpers/builders/scheduledActionBuilder.d.ts.map +1 -1
  54. package/dist/helpers/builders/scheduledActionBuilder.js +8 -10
  55. package/dist/helpers/noop.d.ts +1 -2
  56. package/dist/helpers/noop.d.ts.map +1 -1
  57. package/dist/helpers/noop.js +2 -1
  58. package/dist/helpers/toArray.d.ts +1 -1
  59. package/dist/helpers/toArray.d.ts.map +1 -1
  60. package/dist/helpers/toArray.js +1 -1
  61. package/dist/main.d.ts +5 -5
  62. package/dist/main.d.ts.map +1 -1
  63. package/dist/main.js +6 -9
  64. package/dist/services/jsonFileStorage.d.ts +6 -5
  65. package/dist/services/jsonFileStorage.d.ts.map +1 -1
  66. package/dist/services/jsonFileStorage.js +10 -1
  67. package/dist/services/logger.d.ts +3 -3
  68. package/dist/services/logger.d.ts.map +1 -1
  69. package/dist/services/logger.js +3 -2
  70. package/dist/services/taskScheduler.d.ts +3 -3
  71. package/dist/services/taskScheduler.d.ts.map +1 -1
  72. package/dist/services/taskScheduler.js +8 -10
  73. package/dist/services/telegramApi.d.ts +20 -15
  74. package/dist/services/telegramApi.d.ts.map +1 -1
  75. package/dist/services/telegramApi.js +50 -43
  76. package/dist/types/actionState.d.ts +2 -1
  77. package/dist/types/actionState.d.ts.map +1 -1
  78. package/dist/types/actionWithState.d.ts +2 -2
  79. package/dist/types/actionWithState.d.ts.map +1 -1
  80. package/dist/types/commandCondition.d.ts +2 -2
  81. package/dist/types/commandCondition.d.ts.map +1 -1
  82. package/dist/types/handlers.d.ts +3 -3
  83. package/dist/types/handlers.d.ts.map +1 -1
  84. package/dist/types/replyMessage.d.ts +1 -1
  85. package/dist/types/replyMessage.d.ts.map +1 -1
  86. package/dist/types/response.d.ts +26 -0
  87. package/dist/types/response.d.ts.map +1 -0
  88. package/dist/types/response.js +10 -0
  89. package/dist/types/storage.d.ts +5 -4
  90. package/dist/types/storage.d.ts.map +1 -1
  91. package/entities/{transactionResult.ts → actionExecutionResult.ts} +2 -2
  92. package/entities/actions/commandAction.ts +10 -10
  93. package/entities/actions/scheduledAction.ts +13 -13
  94. package/entities/{bot.ts → botInstance.ts} +20 -20
  95. package/entities/cachedStateFactory.ts +1 -1
  96. package/entities/commandTriggerCheckResult.ts +1 -1
  97. package/entities/context/chatContext.ts +35 -10
  98. package/entities/context/messageContext.ts +37 -16
  99. package/entities/incomingMessage.ts +1 -1
  100. package/entities/responses/imageMessage.ts +11 -3
  101. package/entities/responses/reaction.ts +8 -2
  102. package/entities/responses/textMessage.ts +11 -3
  103. package/entities/responses/unpin.ts +22 -0
  104. package/entities/responses/videoMessage.ts +11 -3
  105. package/entities/states/actionStateBase.ts +3 -2
  106. package/entities/taskRecord.ts +1 -1
  107. package/helpers/builders/commandActionBuilder.ts +5 -5
  108. package/helpers/builders/scheduledActionBuilder.ts +4 -4
  109. package/helpers/noop.ts +1 -3
  110. package/helpers/toArray.ts +1 -1
  111. package/main.ts +9 -9
  112. package/package.json +1 -1
  113. package/services/jsonFileStorage.ts +21 -6
  114. package/services/logger.ts +2 -2
  115. package/services/taskScheduler.ts +6 -6
  116. package/services/telegramApi.ts +95 -59
  117. package/types/actionState.ts +2 -1
  118. package/types/actionWithState.ts +2 -2
  119. package/types/commandCondition.ts +2 -2
  120. package/types/handlers.ts +3 -3
  121. package/types/response.ts +34 -0
  122. package/types/storage.ts +9 -5
  123. package/dist/entities/bot.d.ts.map +0 -1
  124. package/dist/entities/transactionResult.d.ts +0 -7
  125. package/dist/entities/transactionResult.d.ts.map +0 -1
  126. package/types/replyMessage.ts +0 -7
@@ -1,12 +1,14 @@
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
+ import { UnpinResponse } from '../responses/unpin';
7
8
 
8
- export default class ChatContext {
9
+ export class ChatContext {
9
10
  botName: string;
11
+ actionKey: string;
10
12
  interactions: IBotApiInteractions;
11
13
  chatId: number;
12
14
  chatName: string;
@@ -15,6 +17,7 @@ export default class ChatContext {
15
17
 
16
18
  constructor(
17
19
  botName: string,
20
+ actionKey: string,
18
21
  interactions: IBotApiInteractions,
19
22
  chatId: number,
20
23
  chatName: string,
@@ -22,6 +25,7 @@ export default class ChatContext {
22
25
  storage: IStorageClient
23
26
  ) {
24
27
  this.botName = botName;
28
+ this.actionKey = actionKey;
25
29
  this.interactions = interactions;
26
30
  this.chatId = chatId;
27
31
  this.chatName = chatName;
@@ -29,38 +33,59 @@ export default class ChatContext {
29
33
  this.storage = storage;
30
34
  }
31
35
 
32
- sendTextToChat(text: string, disableWebPreview?: boolean) {
36
+ sendTextToChat(
37
+ text: string,
38
+ disableWebPreview?: boolean,
39
+ pinned?: boolean
40
+ ) {
33
41
  this.interactions.respond(
34
42
  new TextMessage(
35
43
  text,
36
44
  this.chatId,
37
45
  undefined,
38
46
  this.traceId,
39
- disableWebPreview ?? false
47
+ disableWebPreview ?? false,
48
+ pinned ?? false,
49
+ this.actionKey
40
50
  )
41
51
  );
42
52
  }
43
53
 
44
- sendImageToChat(name: string) {
54
+ sendImageToChat(name: string, pinned?: boolean) {
45
55
  const filePath = `./content/${name}.png`;
46
56
  this.interactions.respond(
47
57
  new ImageMessage(
48
58
  { source: resolve(filePath) },
49
59
  this.chatId,
50
60
  undefined,
51
- this.traceId
61
+ this.traceId,
62
+ pinned ?? false,
63
+ this.actionKey
52
64
  )
53
65
  );
54
66
  }
55
67
 
56
- sendVideoToChat(name: string) {
68
+ sendVideoToChat(name: string, pinned?: boolean) {
57
69
  const filePath = `./content/${name}.mp4`;
58
70
  this.interactions.respond(
59
71
  new VideoMessage(
60
72
  { source: resolve(filePath) },
61
73
  this.chatId,
62
74
  undefined,
63
- this.traceId
75
+ this.traceId,
76
+ pinned ?? false,
77
+ this.actionKey
78
+ )
79
+ );
80
+ }
81
+
82
+ unpinMessage(messageId: number) {
83
+ this.interactions.unpin(
84
+ new UnpinResponse(
85
+ messageId,
86
+ this.chatId,
87
+ this.traceId,
88
+ this.actionKey
64
89
  )
65
90
  );
66
91
  }
@@ -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;
@@ -23,6 +23,7 @@ export default class MessageContext<
23
23
 
24
24
  constructor(
25
25
  botName: string,
26
+ scheduledKey: string,
26
27
  interactions: IBotApiInteractions,
27
28
  chatId: number,
28
29
  chatName: string,
@@ -33,7 +34,15 @@ export default class MessageContext<
33
34
  fromUserName: string,
34
35
  storage: IStorageClient
35
36
  ) {
36
- super(botName, interactions, chatId, chatName, traceId, storage);
37
+ super(
38
+ botName,
39
+ scheduledKey,
40
+ interactions,
41
+ chatId,
42
+ chatName,
43
+ traceId,
44
+ storage
45
+ );
37
46
 
38
47
  this.messageId = messageId;
39
48
  this.messageText = messageText;
@@ -59,45 +68,57 @@ export default class MessageContext<
59
68
  );
60
69
  }
61
70
 
62
- replyWithText(text: string, disableWebPreview?: boolean) {
71
+ replyWithText(text: string, disableWebPreview?: boolean, pinned?: boolean) {
63
72
  this.interactions.respond(
64
73
  new TextMessage(
65
74
  text,
66
75
  this.chatId,
67
76
  this.messageId,
68
77
  this.traceId,
69
- disableWebPreview ?? false
78
+ disableWebPreview ?? false,
79
+ pinned ?? false,
80
+ this.actionKey
70
81
  )
71
82
  );
72
83
  }
73
84
 
74
- replyWithImage(name: string) {
85
+ replyWithImage(name: string, pinned?: boolean) {
75
86
  const filePath = `./content/${name}.png`;
76
87
  this.interactions.respond(
77
88
  new ImageMessage(
78
89
  { source: resolve(filePath) },
79
90
  this.chatId,
80
91
  this.messageId,
81
- this.traceId
92
+ this.traceId,
93
+ pinned ?? false,
94
+ this.actionKey
82
95
  )
83
96
  );
84
97
  }
85
98
 
86
- replyWithVideo(name: string) {
99
+ replyWithVideo(name: string, pinned?: boolean) {
87
100
  const filePath = `./content/${name}.mp4`;
88
101
  this.interactions.respond(
89
102
  new VideoMessage(
90
103
  { source: resolve(filePath) },
91
104
  this.chatId,
92
105
  this.messageId,
93
- this.traceId
106
+ this.traceId,
107
+ pinned ?? false,
108
+ this.actionKey
94
109
  )
95
110
  );
96
111
  }
97
112
 
98
113
  react(emoji: TelegramEmoji) {
99
114
  this.interactions.react(
100
- new Reaction(this.traceId, this.chatId, this.messageId, emoji)
115
+ new Reaction(
116
+ this.traceId,
117
+ this.chatId,
118
+ this.messageId,
119
+ emoji,
120
+ this.actionKey
121
+ )
101
122
  );
102
123
  }
103
124
  }
@@ -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,22 +1,30 @@
1
1
  import { InputFile } from 'telegraf/types';
2
- import IReplyMessage from '../../types/replyMessage';
2
+ import { BotResponseTypes, IReplyMessage } from '../../types/response';
3
+
4
+ export class ImageMessage implements IReplyMessage<InputFile> {
5
+ kind = BotResponseTypes.image;
3
6
 
4
- export default class ImageMessage implements IReplyMessage<InputFile> {
5
7
  content: InputFile;
6
8
  chatId: number;
7
9
  replyId: number | undefined;
8
10
  traceId: string | number;
9
11
  disableWebPreview = false;
12
+ shouldPin: boolean;
13
+ sourceActionKey: string;
10
14
 
11
15
  constructor(
12
16
  image: InputFile,
13
17
  chatId: number,
14
18
  replyId: number | undefined,
15
- traceId: number | string
19
+ traceId: number | string,
20
+ pinned: boolean,
21
+ sourceActionKey: string
16
22
  ) {
17
23
  this.content = image;
18
24
  this.chatId = chatId;
19
25
  this.replyId = replyId;
20
26
  this.traceId = traceId;
27
+ this.shouldPin = pinned;
28
+ this.sourceActionKey = sourceActionKey;
21
29
  }
22
30
  }
@@ -1,20 +1,26 @@
1
1
  import { TelegramEmoji } from 'telegraf/types';
2
+ import { BotResponseTypes, IChatResponse } from '../../types/response';
3
+
4
+ export class Reaction implements IChatResponse {
5
+ kind = BotResponseTypes.react;
2
6
 
3
- export default class Reaction {
4
7
  chatId: number;
5
8
  messageId: number;
6
9
  traceId: number | string;
7
10
  emoji: TelegramEmoji;
11
+ sourceActionKey: string;
8
12
 
9
13
  constructor(
10
14
  traceId: number | string,
11
15
  chatId: number,
12
16
  messageId: number,
13
- emoji: TelegramEmoji
17
+ emoji: TelegramEmoji,
18
+ sourceActionKey: string
14
19
  ) {
15
20
  this.chatId = chatId;
16
21
  this.messageId = messageId;
17
22
  this.emoji = emoji;
18
23
  this.traceId = traceId;
24
+ this.sourceActionKey = sourceActionKey;
19
25
  }
20
26
  }
@@ -1,23 +1,31 @@
1
- import IReplyMessage from '../../types/replyMessage';
1
+ import { BotResponseTypes, IReplyMessage } from '../../types/response';
2
+
3
+ export class TextMessage implements IReplyMessage<string> {
4
+ kind = BotResponseTypes.text;
2
5
 
3
- export default class TextMessage implements IReplyMessage<string> {
4
6
  content: string;
5
7
  chatId: number;
6
8
  replyId: number | undefined;
7
9
  traceId: string | number;
8
10
  disableWebPreview: boolean;
11
+ shouldPin: boolean;
12
+ sourceActionKey: string;
9
13
 
10
14
  constructor(
11
15
  text: string,
12
16
  chatId: number,
13
17
  replyId: number | undefined,
14
18
  traceId: string | number,
15
- disableWebPreview: boolean
19
+ disableWebPreview: boolean,
20
+ pinned: boolean,
21
+ sourceActionKey: string
16
22
  ) {
17
23
  this.content = text;
18
24
  this.chatId = chatId;
19
25
  this.replyId = replyId;
20
26
  this.traceId = traceId;
21
27
  this.disableWebPreview = disableWebPreview;
28
+ this.shouldPin = pinned;
29
+ this.sourceActionKey = sourceActionKey;
22
30
  }
23
31
  }
@@ -0,0 +1,22 @@
1
+ import { BotResponseTypes, IChatResponse } from '../../types/response';
2
+
3
+ export class UnpinResponse implements IChatResponse {
4
+ kind = BotResponseTypes.unpin;
5
+
6
+ messageId: number;
7
+ chatId: number;
8
+ traceId: number | string;
9
+ sourceActionKey: string;
10
+
11
+ constructor(
12
+ messageId: number,
13
+ chatId: number,
14
+ traceId: number | string,
15
+ sourceActionKey: string
16
+ ) {
17
+ this.messageId = messageId;
18
+ this.chatId = chatId;
19
+ this.traceId = traceId;
20
+ this.sourceActionKey = sourceActionKey;
21
+ }
22
+ }
@@ -1,22 +1,30 @@
1
1
  import { InputFile } from 'telegraf/types';
2
- import IReplyMessage from '../../types/replyMessage';
2
+ import { BotResponseTypes, IReplyMessage } from '../../types/response';
3
+
4
+ export class VideoMessage implements IReplyMessage<InputFile> {
5
+ kind = BotResponseTypes.video;
3
6
 
4
- export default class VideoMessage implements IReplyMessage<InputFile> {
5
7
  content: InputFile;
6
8
  chatId: number;
7
9
  replyId: number | undefined;
8
10
  traceId: string | number;
9
11
  disableWebPreview = false;
12
+ shouldPin: boolean;
13
+ sourceActionKey: string;
10
14
 
11
15
  constructor(
12
16
  video: InputFile,
13
17
  chatId: number,
14
18
  replyId: number | undefined,
15
- traceId: number | string
19
+ traceId: number | string,
20
+ pinned: boolean,
21
+ sourceActionKey: string
16
22
  ) {
17
23
  this.content = video;
18
24
  this.chatId = chatId;
19
25
  this.replyId = replyId;
20
26
  this.traceId = traceId;
27
+ this.shouldPin = pinned;
28
+ this.sourceActionKey = sourceActionKey;
21
29
  }
22
30
  }
@@ -1,5 +1,6 @@
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
+ pinnedMessages: number[] = [];
4
5
  lastExecutedDate = 0;
5
6
  }
@@ -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.17",
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);
@@ -120,4 +120,19 @@ export default class JsonFileStorage implements IStorageClient {
120
120
  async close(): Promise<void> {
121
121
  await this.semaphore.acquire();
122
122
  }
123
+
124
+ async updateStateFor<TActionState extends IActionState>(
125
+ sourceActionKey: string,
126
+ chatId: number,
127
+ update: (state: TActionState) => Promise<void>
128
+ ) {
129
+ await this.lock(async () => {
130
+ const data = await this.loadInternal(sourceActionKey);
131
+ const state = data[chatId] as TActionState;
132
+
133
+ await update(state);
134
+
135
+ await this.save(data, sourceActionKey);
136
+ });
137
+ }
123
138
  }
@@ -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();