chz-telegram-bot 0.0.49 → 0.0.51

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 (87) hide show
  1. package/dist/dtos/actionExecutionResult.d.ts +7 -0
  2. package/dist/dtos/actionExecutionResult.d.ts.map +1 -0
  3. package/dist/dtos/actionExecutionResult.js +10 -0
  4. package/dist/dtos/chatInfo.d.ts +8 -0
  5. package/dist/dtos/chatInfo.d.ts.map +1 -0
  6. package/dist/dtos/chatInfo.js +10 -0
  7. package/dist/dtos/commandTriggerCheckResult.d.ts +10 -0
  8. package/dist/dtos/commandTriggerCheckResult.d.ts.map +1 -0
  9. package/dist/dtos/commandTriggerCheckResult.js +23 -0
  10. package/dist/dtos/incomingMessage.d.ts +14 -0
  11. package/dist/dtos/incomingMessage.d.ts.map +1 -0
  12. package/dist/dtos/incomingMessage.js +44 -0
  13. package/dist/dtos/responses/delay.d.ts +14 -0
  14. package/dist/dtos/responses/delay.d.ts.map +1 -0
  15. package/dist/dtos/responses/delay.js +14 -0
  16. package/dist/dtos/responses/imageMessage.d.ts +18 -0
  17. package/dist/dtos/responses/imageMessage.d.ts.map +1 -0
  18. package/dist/dtos/responses/imageMessage.js +17 -0
  19. package/dist/dtos/responses/reaction.d.ts +15 -0
  20. package/dist/dtos/responses/reaction.d.ts.map +1 -0
  21. package/dist/dtos/responses/reaction.js +15 -0
  22. package/dist/dtos/responses/textMessage.d.ts +17 -0
  23. package/dist/dtos/responses/textMessage.d.ts.map +1 -0
  24. package/dist/dtos/responses/textMessage.js +17 -0
  25. package/dist/dtos/responses/unpin.d.ts +13 -0
  26. package/dist/dtos/responses/unpin.d.ts.map +1 -0
  27. package/dist/dtos/responses/unpin.js +14 -0
  28. package/dist/dtos/responses/videoMessage.d.ts +18 -0
  29. package/dist/dtos/responses/videoMessage.d.ts.map +1 -0
  30. package/dist/dtos/responses/videoMessage.js +17 -0
  31. package/dist/entities/actions/commandAction.d.ts +10 -10
  32. package/dist/entities/actions/commandAction.d.ts.map +1 -1
  33. package/dist/entities/actions/commandAction.js +6 -6
  34. package/dist/entities/actions/scheduledAction.d.ts +10 -10
  35. package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
  36. package/dist/entities/actions/scheduledAction.js +5 -5
  37. package/dist/entities/botInstance.d.ts +7 -7
  38. package/dist/entities/botInstance.d.ts.map +1 -1
  39. package/dist/entities/botInstance.js +8 -7
  40. package/dist/entities/cachedStateFactory.d.ts +2 -2
  41. package/dist/entities/cachedStateFactory.d.ts.map +1 -1
  42. package/dist/entities/context/chatContext.d.ts +4 -5
  43. package/dist/entities/context/chatContext.d.ts.map +1 -1
  44. package/dist/entities/context/chatContext.js +12 -13
  45. package/dist/entities/context/messageContext.d.ts +1 -1
  46. package/dist/entities/context/messageContext.d.ts.map +1 -1
  47. package/dist/entities/context/messageContext.js +10 -10
  48. package/dist/entities/taskRecord.d.ts +3 -3
  49. package/dist/entities/taskRecord.d.ts.map +1 -1
  50. package/dist/services/jsonFileStorage.d.ts +5 -5
  51. package/dist/services/jsonFileStorage.d.ts.map +1 -1
  52. package/dist/services/taskScheduler.d.ts +1 -1
  53. package/dist/services/taskScheduler.d.ts.map +1 -1
  54. package/dist/services/telegramApi.d.ts +5 -14
  55. package/dist/services/telegramApi.d.ts.map +1 -1
  56. package/dist/services/telegramApi.js +11 -21
  57. package/dist/types/actionWithState.d.ts +2 -2
  58. package/dist/types/actionWithState.d.ts.map +1 -1
  59. package/dist/types/response.d.ts +15 -14
  60. package/dist/types/response.d.ts.map +1 -1
  61. package/dist/types/storage.d.ts +1 -1
  62. package/dist/types/storage.d.ts.map +1 -1
  63. package/{entities → dtos}/actionExecutionResult.ts +2 -2
  64. package/dtos/chatInfo.ts +11 -0
  65. package/{entities → dtos}/incomingMessage.ts +8 -7
  66. package/{entities → dtos}/responses/delay.ts +8 -7
  67. package/{entities → dtos}/responses/imageMessage.ts +11 -10
  68. package/{entities → dtos}/responses/reaction.ts +9 -8
  69. package/{entities → dtos}/responses/textMessage.ts +11 -10
  70. package/{entities → dtos}/responses/unpin.ts +8 -7
  71. package/{entities → dtos}/responses/videoMessage.ts +11 -10
  72. package/entities/actions/commandAction.ts +18 -17
  73. package/entities/actions/scheduledAction.ts +16 -16
  74. package/entities/botInstance.ts +26 -15
  75. package/entities/cachedStateFactory.ts +2 -2
  76. package/entities/context/chatContext.ts +20 -18
  77. package/entities/context/messageContext.ts +11 -12
  78. package/entities/taskRecord.ts +3 -3
  79. package/package.json +1 -1
  80. package/services/jsonFileStorage.ts +5 -5
  81. package/services/taskScheduler.ts +1 -1
  82. package/services/telegramApi.ts +17 -65
  83. package/types/actionWithState.ts +2 -2
  84. package/types/response.ts +15 -14
  85. package/types/storage.ts +1 -1
  86. package/helpers/inverseRecord.ts +0 -7
  87. /package/{entities → dtos}/commandTriggerCheckResult.ts +0 -0
@@ -1,13 +1,13 @@
1
- import { Chat, Message, Update, User } from 'telegraf/types';
1
+ import { Message, Update, User } from 'telegraf/types';
2
2
  import { randomInt } from 'crypto';
3
3
  import { MessageType, MessageTypeValue } from '../types/messageTypes';
4
+ import { ChatInfo } from './chatInfo';
4
5
 
5
6
  export class IncomingMessage {
6
7
  message_id: number;
7
- chat: Chat;
8
+ chatInfo: ChatInfo;
8
9
  from: User | undefined;
9
10
  text: string;
10
- chatName: string;
11
11
  type: MessageTypeValue;
12
12
  traceId = randomInt(10000, 99999);
13
13
 
@@ -31,13 +31,14 @@ export class IncomingMessage {
31
31
 
32
32
  constructor(ctxMessage: Update.New & (Update.NonChannel & Message)) {
33
33
  this.message_id = ctxMessage.message_id;
34
- this.chat = ctxMessage.chat;
35
34
  this.from = ctxMessage.from;
36
35
  this.text = 'text' in ctxMessage ? ctxMessage.text : '';
37
- this.type = this.detectMessageType(ctxMessage);
38
- this.chatName =
36
+ this.chatInfo = new ChatInfo(
37
+ ctxMessage.chat.id,
39
38
  'title' in ctxMessage.chat
40
39
  ? ctxMessage.chat.title + ' ' + ctxMessage.chat.id
41
- : 'DM';
40
+ : 'DM'
41
+ );
42
+ this.type = this.detectMessageType(ctxMessage);
42
43
  }
43
44
  }
@@ -2,22 +2,23 @@ import { IActionState } from '../../types/actionState';
2
2
  import { IActionWithState } from '../../types/actionWithState';
3
3
  import { BotResponseTypes, IChatResponse } from '../../types/response';
4
4
  import { Milliseconds } from '../../types/timeValues';
5
+ import { ChatInfo } from '../chatInfo';
5
6
 
6
7
  export class DelayResponse implements IChatResponse {
7
- kind = BotResponseTypes.delay;
8
+ readonly kind = BotResponseTypes.delay;
8
9
 
9
- chatId: number;
10
- traceId: number | string;
11
- delay: Milliseconds;
12
- action: IActionWithState<IActionState>;
10
+ readonly chatInfo: ChatInfo;
11
+ readonly traceId: number | string;
12
+ readonly delay: Milliseconds;
13
+ readonly action: IActionWithState<IActionState>;
13
14
 
14
15
  constructor(
15
16
  delay: Milliseconds,
16
- chatId: number,
17
+ chatInfo: ChatInfo,
17
18
  traceId: number | string,
18
19
  action: IActionWithState<IActionState>
19
20
  ) {
20
- this.chatId = chatId;
21
+ this.chatInfo = chatInfo;
21
22
  this.delay = delay;
22
23
  this.traceId = traceId;
23
24
  this.action = action;
@@ -3,28 +3,29 @@ import { BotResponseTypes, IReplyMessage } from '../../types/response';
3
3
  import { MessageSendingOptions } from '../../types/messageSendingOptions';
4
4
  import { IActionWithState } from '../../types/actionWithState';
5
5
  import { IActionState } from '../../types/actionState';
6
+ import { ChatInfo } from '../chatInfo';
6
7
 
7
8
  export class ImageMessage implements IReplyMessage<InputFile> {
8
- kind = BotResponseTypes.image;
9
+ readonly kind = BotResponseTypes.image;
9
10
 
10
- content: InputFile;
11
- chatId: number;
12
- replyId: number | undefined;
13
- traceId: string | number;
14
- disableWebPreview = false;
15
- shouldPin: boolean;
16
- action: IActionWithState<IActionState>;
11
+ readonly content: InputFile;
12
+ readonly chatInfo: ChatInfo;
13
+ readonly replyId: number | undefined;
14
+ readonly traceId: string | number;
15
+ readonly disableWebPreview = false;
16
+ readonly shouldPin: boolean;
17
+ readonly action: IActionWithState<IActionState>;
17
18
 
18
19
  constructor(
19
20
  image: InputFile,
20
- chatId: number,
21
+ chatInfo: ChatInfo,
21
22
  replyId: number | undefined,
22
23
  traceId: number | string,
23
24
  action: IActionWithState<IActionState>,
24
25
  options?: MessageSendingOptions
25
26
  ) {
26
27
  this.content = image;
27
- this.chatId = chatId;
28
+ this.chatInfo = chatInfo;
28
29
  this.replyId = replyId;
29
30
  this.traceId = traceId;
30
31
  this.shouldPin = options?.pin ?? false;
@@ -2,24 +2,25 @@ import { TelegramEmoji } from 'telegraf/types';
2
2
  import { BotResponseTypes, IChatResponse } from '../../types/response';
3
3
  import { IActionWithState } from '../../types/actionWithState';
4
4
  import { IActionState } from '../../types/actionState';
5
+ import { ChatInfo } from '../chatInfo';
5
6
 
6
7
  export class Reaction implements IChatResponse {
7
- kind = BotResponseTypes.react;
8
+ readonly kind = BotResponseTypes.react;
8
9
 
9
- chatId: number;
10
- messageId: number;
11
- traceId: number | string;
12
- emoji: TelegramEmoji;
13
- action: IActionWithState<IActionState>;
10
+ readonly chatInfo: ChatInfo;
11
+ readonly messageId: number;
12
+ readonly traceId: number | string;
13
+ readonly emoji: TelegramEmoji;
14
+ readonly action: IActionWithState<IActionState>;
14
15
 
15
16
  constructor(
16
17
  traceId: number | string,
17
- chatId: number,
18
+ chatInfo: ChatInfo,
18
19
  messageId: number,
19
20
  emoji: TelegramEmoji,
20
21
  action: IActionWithState<IActionState>
21
22
  ) {
22
- this.chatId = chatId;
23
+ this.chatInfo = chatInfo;
23
24
  this.messageId = messageId;
24
25
  this.emoji = emoji;
25
26
  this.traceId = traceId;
@@ -2,28 +2,29 @@ import { TextMessageSendingOptions } from '../../types/messageSendingOptions';
2
2
  import { BotResponseTypes, IReplyMessage } from '../../types/response';
3
3
  import { IActionWithState } from '../../types/actionWithState';
4
4
  import { IActionState } from '../../types/actionState';
5
+ import { ChatInfo } from '../chatInfo';
5
6
 
6
7
  export class TextMessage implements IReplyMessage<string> {
7
- kind = BotResponseTypes.text;
8
+ readonly kind = BotResponseTypes.text;
8
9
 
9
- content: string;
10
- chatId: number;
11
- replyId: number | undefined;
12
- traceId: string | number;
13
- disableWebPreview: boolean;
14
- shouldPin: boolean;
15
- action: IActionWithState<IActionState>;
10
+ readonly content: string;
11
+ readonly chatInfo: ChatInfo;
12
+ readonly replyId: number | undefined;
13
+ readonly traceId: string | number;
14
+ readonly disableWebPreview: boolean;
15
+ readonly shouldPin: boolean;
16
+ readonly action: IActionWithState<IActionState>;
16
17
 
17
18
  constructor(
18
19
  text: string,
19
- chatId: number,
20
+ chatInfo: ChatInfo,
20
21
  replyId: number | undefined,
21
22
  traceId: string | number,
22
23
  action: IActionWithState<IActionState>,
23
24
  options?: TextMessageSendingOptions
24
25
  ) {
25
26
  this.content = text;
26
- this.chatId = chatId;
27
+ this.chatInfo = chatInfo;
27
28
  this.replyId = replyId;
28
29
  this.traceId = traceId;
29
30
  this.disableWebPreview = options?.disableWebPreview ?? false;
@@ -1,23 +1,24 @@
1
1
  import { BotResponseTypes, IChatResponse } from '../../types/response';
2
2
  import { IActionWithState } from '../../types/actionWithState';
3
3
  import { IActionState } from '../../types/actionState';
4
+ import { ChatInfo } from '../chatInfo';
4
5
 
5
6
  export class UnpinResponse implements IChatResponse {
6
- kind = BotResponseTypes.unpin;
7
+ readonly kind = BotResponseTypes.unpin;
7
8
 
8
- messageId: number;
9
- chatId: number;
10
- traceId: number | string;
11
- action: IActionWithState<IActionState>;
9
+ readonly messageId: number;
10
+ readonly chatInfo: ChatInfo;
11
+ readonly traceId: number | string;
12
+ readonly action: IActionWithState<IActionState>;
12
13
 
13
14
  constructor(
14
15
  messageId: number,
15
- chatId: number,
16
+ chatInfo: ChatInfo,
16
17
  traceId: number | string,
17
18
  action: IActionWithState<IActionState>
18
19
  ) {
19
20
  this.messageId = messageId;
20
- this.chatId = chatId;
21
+ this.chatInfo = chatInfo;
21
22
  this.traceId = traceId;
22
23
  this.action = action;
23
24
  }
@@ -3,28 +3,29 @@ import { BotResponseTypes, IReplyMessage } from '../../types/response';
3
3
  import { MessageSendingOptions } from '../../types/messageSendingOptions';
4
4
  import { IActionWithState } from '../../types/actionWithState';
5
5
  import { IActionState } from '../../types/actionState';
6
+ import { ChatInfo } from '../chatInfo';
6
7
 
7
8
  export class VideoMessage implements IReplyMessage<InputFile> {
8
- kind = BotResponseTypes.video;
9
+ readonly kind = BotResponseTypes.video;
9
10
 
10
- content: InputFile;
11
- chatId: number;
12
- replyId: number | undefined;
13
- traceId: string | number;
14
- disableWebPreview = false;
15
- shouldPin: boolean;
16
- action: IActionWithState<IActionState>;
11
+ readonly content: InputFile;
12
+ readonly chatInfo: ChatInfo;
13
+ readonly replyId: number | undefined;
14
+ readonly traceId: string | number;
15
+ readonly disableWebPreview = false;
16
+ readonly shouldPin: boolean;
17
+ readonly action: IActionWithState<IActionState>;
17
18
 
18
19
  constructor(
19
20
  video: InputFile,
20
- chatId: number,
21
+ chatInfo: ChatInfo,
21
22
  replyId: number | undefined,
22
23
  traceId: number | string,
23
24
  action: IActionWithState<IActionState>,
24
25
  options?: MessageSendingOptions
25
26
  ) {
26
27
  this.content = video;
27
- this.chatId = chatId;
28
+ this.chatInfo = chatInfo;
28
29
  this.replyId = replyId;
29
30
  this.traceId = traceId;
30
31
  this.shouldPin = options?.pin ?? false;
@@ -6,25 +6,25 @@ import { secondsToMilliseconds } from '../../helpers/timeConvertions';
6
6
  import { toArray } from '../../helpers/toArray';
7
7
  import { IActionState } from '../../types/actionState';
8
8
  import { IActionWithState, ActionKey } from '../../types/actionWithState';
9
- import { CommandTriggerCheckResult } from '../commandTriggerCheckResult';
9
+ import { CommandTriggerCheckResult } from '../../dtos/commandTriggerCheckResult';
10
10
  import { MessageContext } from '../context/messageContext';
11
11
  import { Logger } from '../../services/logger';
12
- import { ActionExecutionResult } from '../actionExecutionResult';
12
+ import { ActionExecutionResult } from '../../dtos/actionExecutionResult';
13
13
  import { CommandTrigger } from '../../types/commandTrigger';
14
14
 
15
15
  export class CommandAction<TActionState extends IActionState>
16
16
  implements IActionWithState<TActionState>
17
17
  {
18
- triggers: CommandTrigger[];
19
- handler: CommandHandler<TActionState>;
20
- name: string;
21
- cooldownInSeconds: Seconds;
22
- active: boolean;
23
- chatsBlacklist: number[];
24
- allowedUsers: number[];
25
- condition: CommandCondition<TActionState>;
26
- stateConstructor: () => TActionState;
27
- key: ActionKey;
18
+ readonly triggers: CommandTrigger[];
19
+ readonly handler: CommandHandler<TActionState>;
20
+ readonly name: string;
21
+ readonly cooldownInSeconds: Seconds;
22
+ readonly active: boolean;
23
+ readonly chatsBlacklist: number[];
24
+ readonly allowedUsers: number[];
25
+ readonly condition: CommandCondition<TActionState>;
26
+ readonly stateConstructor: () => TActionState;
27
+ readonly key: ActionKey;
28
28
 
29
29
  constructor(
30
30
  trigger: CommandTrigger | CommandTrigger[],
@@ -56,7 +56,8 @@ export class CommandAction<TActionState extends IActionState>
56
56
  `Context for ${this.key} is not initialized or already consumed`
57
57
  );
58
58
 
59
- if (!this.active || this.chatsBlacklist.includes(ctx.chatId)) return [];
59
+ if (!this.active || this.chatsBlacklist.includes(ctx.chatInfo.id))
60
+ return [];
60
61
 
61
62
  const isConditionMet = await this.condition(ctx);
62
63
 
@@ -64,7 +65,7 @@ export class CommandAction<TActionState extends IActionState>
64
65
 
65
66
  const state = await ctx.storage.getActionState<TActionState>(
66
67
  this,
67
- ctx.chatId
68
+ ctx.chatInfo.id
68
69
  );
69
70
 
70
71
  const { shouldTrigger, matchResults, skipCooldown } = this.triggers
@@ -79,8 +80,8 @@ export class CommandAction<TActionState extends IActionState>
79
80
  Logger.logWithTraceId(
80
81
  ctx.botName,
81
82
  ctx.traceId,
82
- ctx.chatName,
83
- ` - Executing [${this.name}] in ${ctx.chatId}`
83
+ ctx.chatInfo.name,
84
+ ` - Executing [${this.name}] in ${ctx.chatInfo.id}`
84
85
  );
85
86
  ctx.matchResults = matchResults;
86
87
 
@@ -98,7 +99,7 @@ export class CommandAction<TActionState extends IActionState>
98
99
 
99
100
  await ctx.storage.saveActionExecutionResult(
100
101
  this,
101
- ctx.chatId,
102
+ ctx.chatInfo.id,
102
103
  new ActionExecutionResult(state, ctx.startCooldown && shouldTrigger)
103
104
  );
104
105
 
@@ -7,25 +7,25 @@ import { IActionState } from '../../types/actionState';
7
7
  import { IActionWithState, ActionKey } from '../../types/actionWithState';
8
8
  import { CachedStateFactory } from '../cachedStateFactory';
9
9
  import { ChatContext } from '../context/chatContext';
10
- import { ActionExecutionResult } from '../actionExecutionResult';
10
+ import { ActionExecutionResult } from '../../dtos/actionExecutionResult';
11
11
  import { Logger } from '../../services/logger';
12
12
  import { Scheduler } from '../../services/taskScheduler';
13
13
 
14
14
  export class ScheduledAction<TActionState extends IActionState>
15
15
  implements IActionWithState<TActionState>
16
16
  {
17
- static locks = new Map<string, Semaphore>();
17
+ static readonly locks = new Map<string, Semaphore>();
18
18
 
19
- name: string;
20
- timeinHours: HoursOfDay;
21
- active: boolean;
22
- chatsWhitelist: number[];
23
- key: ActionKey;
19
+ readonly name: string;
20
+ readonly timeinHours: HoursOfDay;
21
+ readonly active: boolean;
22
+ readonly chatsWhitelist: number[];
23
+ readonly key: ActionKey;
24
24
 
25
- cachedState = new Map<string, unknown>();
26
- stateConstructor: () => TActionState;
27
- cachedStateFactories: Map<string, CachedStateFactory>;
28
- handler: ScheduledHandler<TActionState>;
25
+ readonly cachedState = new Map<string, unknown>();
26
+ readonly stateConstructor: () => TActionState;
27
+ readonly cachedStateFactories: Map<string, CachedStateFactory>;
28
+ readonly handler: ScheduledHandler<TActionState>;
29
29
 
30
30
  constructor(
31
31
  name: string,
@@ -52,12 +52,12 @@ export class ScheduledAction<TActionState extends IActionState>
52
52
  `Context for ${this.key} is not initialized or already consumed`
53
53
  );
54
54
 
55
- if (!this.active || !this.chatsWhitelist.includes(ctx.chatId))
55
+ if (!this.active || !this.chatsWhitelist.includes(ctx.chatInfo.id))
56
56
  return [];
57
57
 
58
58
  const state = await ctx.storage.getActionState<TActionState>(
59
59
  this,
60
- ctx.chatId
60
+ ctx.chatInfo.id
61
61
  );
62
62
  const isAllowedToTrigger = this.shouldTrigger(state);
63
63
 
@@ -65,8 +65,8 @@ export class ScheduledAction<TActionState extends IActionState>
65
65
  Logger.logWithTraceId(
66
66
  ctx.botName,
67
67
  ctx.traceId,
68
- ctx.chatName,
69
- ` - Executing [${this.name}] in ${ctx.chatId}`
68
+ ctx.chatInfo.name,
69
+ ` - Executing [${this.name}] in ${ctx.chatInfo.id}`
70
70
  );
71
71
 
72
72
  await this.handler(
@@ -81,7 +81,7 @@ export class ScheduledAction<TActionState extends IActionState>
81
81
  ctx.updateActions.forEach((action) => action(state));
82
82
  await ctx.storage.saveActionExecutionResult(
83
83
  this,
84
- ctx.chatId,
84
+ ctx.chatInfo.id,
85
85
  new ActionExecutionResult(state, isAllowedToTrigger)
86
86
  );
87
87
  }
@@ -12,19 +12,20 @@ import { CommandAction } from './actions/commandAction';
12
12
  import { ScheduledAction } from './actions/scheduledAction';
13
13
  import { Logger } from '../services/logger';
14
14
  import { Scheduler } from '../services/taskScheduler';
15
- import { IncomingMessage } from './incomingMessage';
15
+ import { IncomingMessage } from '../dtos/incomingMessage';
16
16
  import moment from 'moment';
17
17
  import { ChatContext } from './context/chatContext';
18
18
  import { MessageContext } from './context/messageContext';
19
+ import { ChatInfo } from '../dtos/chatInfo';
19
20
 
20
21
  export class BotInstance {
21
- name: string;
22
- private api: TelegramApiService;
23
- private telegraf: Telegraf;
24
- private commands: CommandAction<IActionState>[];
25
- private scheduled: ScheduledAction<IActionState>[];
26
- private chats: Record<string, number>;
27
- storage: IStorageClient;
22
+ readonly name: string;
23
+ private readonly api: TelegramApiService;
24
+ private readonly telegraf: Telegraf;
25
+ private readonly commands: CommandAction<IActionState>[];
26
+ private readonly scheduled: ScheduledAction<IActionState>[];
27
+ private readonly chats: Record<string, number>;
28
+ readonly storage: IStorageClient;
28
29
 
29
30
  constructor(options: {
30
31
  name: string;
@@ -57,8 +58,7 @@ export class BotInstance {
57
58
  this.api = new TelegramApiService(
58
59
  this.name,
59
60
  this.telegraf.telegram,
60
- this.storage,
61
- this.chats
61
+ this.storage
62
62
  );
63
63
 
64
64
  this.initializeMessageProcessing(
@@ -132,14 +132,14 @@ export class BotInstance {
132
132
  Logger.logObjectWithTraceId(
133
133
  this.name,
134
134
  msg.traceId,
135
- msg.chatName,
135
+ msg.chatInfo.name,
136
136
  ctx.update.message
137
137
  );
138
138
  } else {
139
139
  Logger.logWithTraceId(
140
140
  this.name,
141
141
  msg.traceId,
142
- msg.chatName,
142
+ msg.chatInfo.name,
143
143
  `${messageFromName} (${messageFromId}): ${messageContent}`
144
144
  );
145
145
  }
@@ -166,7 +166,13 @@ export class BotInstance {
166
166
 
167
167
  for (const [chatName, chatId] of Object.entries(this.chats)) {
168
168
  for (const scheduledAction of this.scheduled) {
169
- this.api.initializeContextForChat(ctx, chatId, scheduledAction);
169
+ ctx.initializeChatContext(
170
+ this.name,
171
+ scheduledAction,
172
+ new ChatInfo(chatId, chatName),
173
+ `Scheduled:${scheduledAction.key}:${chatId}`,
174
+ this.storage
175
+ );
170
176
 
171
177
  try {
172
178
  const responses = await scheduledAction.exec(ctx);
@@ -190,7 +196,12 @@ export class BotInstance {
190
196
  const ctx = new MessageContext<IActionState>();
191
197
 
192
198
  for (const commandAction of this.commands) {
193
- this.api.initializeContextForMessage(ctx, msg, commandAction);
199
+ ctx.initializeMessageContext(
200
+ this.name,
201
+ commandAction,
202
+ msg,
203
+ this.storage
204
+ );
194
205
 
195
206
  try {
196
207
  const responses = await commandAction.exec(ctx);
@@ -199,7 +210,7 @@ export class BotInstance {
199
210
  Logger.errorWithTraceId(
200
211
  ctx.botName,
201
212
  ctx.traceId,
202
- ctx.chatName,
213
+ ctx.chatInfo.name,
203
214
  error,
204
215
  ctx
205
216
  );
@@ -1,8 +1,8 @@
1
1
  import { Hours } from '../types/timeValues';
2
2
 
3
3
  export class CachedStateFactory {
4
- getValue: () => Promise<unknown>;
5
- invalidationTimeoutInHours: Hours;
4
+ readonly getValue: () => Promise<unknown>;
5
+ readonly invalidationTimeoutInHours: Hours;
6
6
 
7
7
  constructor(
8
8
  itemFactory: () => Promise<unknown>,
@@ -1,9 +1,9 @@
1
1
  import { resolve } from 'path';
2
2
  import { IStorageClient } from '../../types/storage';
3
- import { ImageMessage } from '../responses/imageMessage';
4
- import { TextMessage } from '../responses/textMessage';
5
- import { VideoMessage } from '../responses/videoMessage';
6
- import { UnpinResponse } from '../responses/unpin';
3
+ import { ImageMessage } from '../../dtos/responses/imageMessage';
4
+ import { TextMessage } from '../../dtos/responses/textMessage';
5
+ import { VideoMessage } from '../../dtos/responses/videoMessage';
6
+ import { UnpinResponse } from '../../dtos/responses/unpin';
7
7
  import {
8
8
  MessageSendingOptions,
9
9
  TextMessageSendingOptions
@@ -12,7 +12,8 @@ import { IActionWithState } from '../../types/actionWithState';
12
12
  import { IActionState } from '../../types/actionState';
13
13
  import { BotResponse } from '../../types/response';
14
14
  import { Milliseconds } from '../../types/timeValues';
15
- import { DelayResponse } from '../responses/delay';
15
+ import { DelayResponse } from '../../dtos/responses/delay';
16
+ import { ChatInfo } from '../../dtos/chatInfo';
16
17
 
17
18
  /**
18
19
  * Context of action executed in chat.
@@ -24,10 +25,8 @@ export class ChatContext<TActionState extends IActionState> {
24
25
  traceId!: number | string;
25
26
  /** Name of a bot that executes this action. */
26
27
  botName!: string;
27
- /** Id of a chat that action is executed in. */
28
- chatId!: number;
29
- /** Name of a chat that action is executed in. */
30
- chatName!: string;
28
+ /** Chat information. */
29
+ chatInfo!: ChatInfo;
31
30
  /** Storage client instance for this bot. */
32
31
  storage!: IStorageClient;
33
32
  /** Ordered collection of responses to be processed */
@@ -40,15 +39,13 @@ export class ChatContext<TActionState extends IActionState> {
40
39
  initializeChatContext(
41
40
  botName: string,
42
41
  action: IActionWithState<TActionState>,
43
- chatId: number,
44
- chatName: string,
42
+ chatInfo: ChatInfo,
45
43
  traceId: number | string,
46
44
  storage: IStorageClient
47
45
  ) {
48
46
  this.botName = botName;
49
47
  this.action = action;
50
- this.chatId = chatId;
51
- this.chatName = chatName;
48
+ this.chatInfo = chatInfo;
52
49
  this.traceId = traceId;
53
50
  this.storage = storage;
54
51
 
@@ -77,7 +74,7 @@ export class ChatContext<TActionState extends IActionState> {
77
74
  this.responses.push(
78
75
  new TextMessage(
79
76
  text,
80
- this.chatId,
77
+ this.chatInfo,
81
78
  undefined,
82
79
  this.traceId,
83
80
  this.action,
@@ -97,7 +94,7 @@ export class ChatContext<TActionState extends IActionState> {
97
94
  this.responses.push(
98
95
  new ImageMessage(
99
96
  { source: resolve(filePath) },
100
- this.chatId,
97
+ this.chatInfo,
101
98
  undefined,
102
99
  this.traceId,
103
100
  this.action,
@@ -117,7 +114,7 @@ export class ChatContext<TActionState extends IActionState> {
117
114
  this.responses.push(
118
115
  new VideoMessage(
119
116
  { source: resolve(filePath) },
120
- this.chatId,
117
+ this.chatInfo,
121
118
  undefined,
122
119
  this.traceId,
123
120
  this.action,
@@ -133,7 +130,12 @@ export class ChatContext<TActionState extends IActionState> {
133
130
  */
134
131
  unpinMessage(messageId: number) {
135
132
  this.responses.push(
136
- new UnpinResponse(messageId, this.chatId, this.traceId, this.action)
133
+ new UnpinResponse(
134
+ messageId,
135
+ this.chatInfo,
136
+ this.traceId,
137
+ this.action
138
+ )
137
139
  );
138
140
  }
139
141
 
@@ -143,7 +145,7 @@ export class ChatContext<TActionState extends IActionState> {
143
145
  */
144
146
  delayNextResponse(delay: Milliseconds) {
145
147
  this.responses.push(
146
- new DelayResponse(delay, this.chatId, this.traceId, this.action)
148
+ new DelayResponse(delay, this.chatInfo, this.traceId, this.action)
147
149
  );
148
150
  }
149
151
  }