chz-telegram-bot 0.7.13 → 0.7.15

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 (98) hide show
  1. package/dist/dtos/propertyProviderSets.d.ts +0 -1
  2. package/dist/dtos/propertyProviderSets.d.ts.map +1 -1
  3. package/dist/entities/actions/commandAction.d.ts +1 -1
  4. package/dist/entities/actions/commandAction.d.ts.map +1 -1
  5. package/dist/entities/actions/commandAction.js +2 -2
  6. package/dist/helpers/builders/commandActionBuilder.d.ts.map +1 -1
  7. package/dist/helpers/builders/commandActionBuilder.js +2 -3
  8. package/package.json +4 -1
  9. package/eslint.config.ts +0 -62
  10. package/src/builtin/helpAction.ts +0 -17
  11. package/src/dtos/chatHistoryMessage.ts +0 -22
  12. package/src/dtos/chatInfo.ts +0 -12
  13. package/src/dtos/commandTriggerCheckResult.ts +0 -40
  14. package/src/dtos/cooldownInfo.ts +0 -10
  15. package/src/dtos/incomingMessage.ts +0 -71
  16. package/src/dtos/incomingQuery.ts +0 -14
  17. package/src/dtos/messageInfo.ts +0 -15
  18. package/src/dtos/propertyProviderSets.ts +0 -21
  19. package/src/dtos/replyInfo.ts +0 -9
  20. package/src/dtos/responses/delay.ts +0 -28
  21. package/src/dtos/responses/imageMessage.ts +0 -41
  22. package/src/dtos/responses/inlineQueryResponse.ts +0 -26
  23. package/src/dtos/responses/reaction.ts +0 -30
  24. package/src/dtos/responses/textMessage.ts +0 -44
  25. package/src/dtos/responses/unpin.ts +0 -27
  26. package/src/dtos/responses/videoMessage.ts +0 -41
  27. package/src/dtos/userInfo.ts +0 -8
  28. package/src/entities/actions/commandAction.ts +0 -275
  29. package/src/entities/actions/inlineQueryAction.ts +0 -83
  30. package/src/entities/actions/replyCaptureAction.ts +0 -110
  31. package/src/entities/actions/scheduledAction.ts +0 -182
  32. package/src/entities/botInstance.ts +0 -92
  33. package/src/entities/cachedStateFactory.ts +0 -14
  34. package/src/entities/context/baseContext.ts +0 -111
  35. package/src/entities/context/chatContext.ts +0 -135
  36. package/src/entities/context/inlineQueryContext.ts +0 -63
  37. package/src/entities/context/messageContext.ts +0 -250
  38. package/src/entities/context/replyContext.ts +0 -260
  39. package/src/entities/states/actionStateBase.ts +0 -6
  40. package/src/entities/taskRecord.ts +0 -11
  41. package/src/helpers/builders/commandActionBuilder.ts +0 -214
  42. package/src/helpers/builders/inlineQueryActionBuilder.ts +0 -71
  43. package/src/helpers/builders/scheduledActionBuilder.ts +0 -143
  44. package/src/helpers/mapUtils.ts +0 -28
  45. package/src/helpers/noop.ts +0 -20
  46. package/src/helpers/objectFromEntries.ts +0 -7
  47. package/src/helpers/timeConvertions.ts +0 -13
  48. package/src/helpers/toArray.ts +0 -3
  49. package/src/helpers/traceFactory.ts +0 -11
  50. package/src/index.ts +0 -33
  51. package/src/main.ts +0 -76
  52. package/src/services/actionProcessingService.ts +0 -125
  53. package/src/services/actionProcessors/baseProcessor.ts +0 -67
  54. package/src/services/actionProcessors/commandActionProcessor.ts +0 -231
  55. package/src/services/actionProcessors/inlineQueryActionProcessor.ts +0 -165
  56. package/src/services/actionProcessors/scheduledActionProcessor.ts +0 -136
  57. package/src/services/jsonFileStorage.ts +0 -181
  58. package/src/services/nodeTimeoutScheduler.ts +0 -79
  59. package/src/services/responseProcessingQueue.ts +0 -57
  60. package/src/services/telegramApi.ts +0 -278
  61. package/src/types/action.ts +0 -15
  62. package/src/types/actionState.ts +0 -4
  63. package/src/types/cachedValueAccessor.ts +0 -1
  64. package/src/types/capture.ts +0 -33
  65. package/src/types/commandCondition.ts +0 -9
  66. package/src/types/commandTrigger.ts +0 -1
  67. package/src/types/events.ts +0 -286
  68. package/src/types/externalAliases.ts +0 -18
  69. package/src/types/handlers.ts +0 -26
  70. package/src/types/inputFile.ts +0 -4
  71. package/src/types/messageSendingOptions.ts +0 -10
  72. package/src/types/messageTypes.ts +0 -21
  73. package/src/types/propertyProvider.ts +0 -14
  74. package/src/types/response.ts +0 -51
  75. package/src/types/scheduler.ts +0 -20
  76. package/src/types/storage.ts +0 -23
  77. package/src/types/timeValues.ts +0 -33
  78. package/src/types/trace.ts +0 -5
  79. package/tests/dtos/commandTriggerCheckResult.test.ts +0 -301
  80. package/tests/entities/actions/inlineQueryAction.test.ts +0 -359
  81. package/tests/entities/actions/replyCaptureAction.test.ts +0 -501
  82. package/tests/entities/cachedStateFactory.test.ts +0 -98
  83. package/tests/entities/context/chatContext.test.ts +0 -606
  84. package/tests/entities/context/messageContext.test.ts +0 -370
  85. package/tests/entities/states/actionStateBase.test.ts +0 -138
  86. package/tests/entities/taskRecord.test.ts +0 -195
  87. package/tests/helpers/mapUtils.test.ts +0 -163
  88. package/tests/helpers/timeConvertions.test.ts +0 -129
  89. package/tests/services/actionProcessors/baseActionProcessor.test.ts +0 -359
  90. package/tests/services/actionProcessors/commandActionProcessor.test.ts +0 -268
  91. package/tests/services/actionProcessors/inlineQueryActionProcessor.test.ts +0 -616
  92. package/tests/services/actionProcessors/processorTestHelpers.ts +0 -147
  93. package/tests/services/actionProcessors/scheduledActionProcessor.test.ts +0 -153
  94. package/tests/services/jsonFileStorage.test.ts +0 -927
  95. package/tests/services/nodeTimeoutScheduler.test.ts +0 -421
  96. package/tests/services/responseProcessingQueue.test.ts +0 -388
  97. package/tsconfig.build.json +0 -8
  98. package/tsconfig.json +0 -118
@@ -1,231 +0,0 @@
1
- import { IncomingMessage } from '../../dtos/incomingMessage';
2
- import { CommandAction } from '../../entities/actions/commandAction';
3
- import { ReplyCaptureAction } from '../../entities/actions/replyCaptureAction';
4
- import { MessageContextInternal } from '../../entities/context/messageContext';
5
- import { ReplyContextInternal } from '../../entities/context/replyContext';
6
- import { IActionState } from '../../types/actionState';
7
- import { TelegramApiService } from '../telegramApi';
8
- import { IReplyCapture } from '../../types/capture';
9
- import { ChatInfo } from '../../dtos/chatInfo';
10
- import {
11
- INTERNAL_MESSAGE_TYPE_PREFIX,
12
- MessageType
13
- } from '../../types/messageTypes';
14
- import { typeSafeObjectFromEntries } from '../../helpers/objectFromEntries';
15
- import { BaseActionProcessor } from './baseProcessor';
16
- import { getOrSetIfNotExists } from '../../helpers/mapUtils';
17
- import { ChatHistoryMessage } from '../../dtos/chatHistoryMessage';
18
- import { BotInfo, TelegramBot } from '../../types/externalAliases';
19
- import { BotEventType } from '../../types/events';
20
- import { TraceId } from '../../types/trace';
21
-
22
- const MESSAGE_HISTORY_LENGTH_LIMIT = 100;
23
-
24
- export class CommandActionProcessor extends BaseActionProcessor {
25
- private readonly replyCaptures: ReplyCaptureAction<IActionState>[] = [];
26
- private readonly chatHistory = new Map<number, ChatHistoryMessage[]>();
27
- private botInfo!: BotInfo;
28
-
29
- private commands = typeSafeObjectFromEntries(
30
- Object.values(MessageType).map((x) => [
31
- x,
32
- [] as CommandAction<IActionState>[]
33
- ])
34
- );
35
-
36
- initialize(
37
- api: TelegramApiService,
38
- telegram: TelegramBot,
39
- commands: CommandAction<IActionState>[],
40
- botInfo: BotInfo
41
- ) {
42
- this.botInfo = botInfo;
43
- this.initializeDependencies(api);
44
-
45
- for (const msgType of Object.values(MessageType)) {
46
- if (msgType == MessageType.Text) {
47
- this.commands[msgType] = commands.filter(
48
- (cmd) =>
49
- cmd.triggers.some((x) => typeof x != 'string') ||
50
- cmd.triggers.some(
51
- (x) =>
52
- typeof x == 'string' &&
53
- !x.startsWith(INTERNAL_MESSAGE_TYPE_PREFIX)
54
- ) ||
55
- cmd.triggers.includes(MessageType.Text) ||
56
- cmd.triggers.includes(MessageType.Any)
57
- );
58
-
59
- continue;
60
- }
61
-
62
- this.commands[msgType] = commands.filter(
63
- (cmd) =>
64
- cmd.triggers.includes(msgType) ||
65
- cmd.triggers.includes(MessageType.Any)
66
- );
67
- }
68
-
69
- if (commands.length > 0) {
70
- telegram.on('message', ({ message }) => {
71
- const internalMessage = new IncomingMessage(
72
- message,
73
- this.botName,
74
- getOrSetIfNotExists(this.chatHistory, message.chat.id, [])
75
- );
76
-
77
- this.eventEmitter.emit(BotEventType.messageRecieved, {
78
- botInfo: this.botInfo,
79
- message: internalMessage,
80
- traceId: internalMessage.traceId
81
- });
82
-
83
- this.startMessageProcessing(internalMessage);
84
- });
85
- }
86
- }
87
-
88
- captureRegistrationCallback(
89
- capture: IReplyCapture,
90
- parentMessageId: number,
91
- chatInfo: ChatInfo,
92
- traceId: TraceId
93
- ) {
94
- const replyAction = new ReplyCaptureAction(
95
- parentMessageId,
96
- capture.action,
97
- capture.handler,
98
- capture.trigger,
99
- capture.abortController
100
- );
101
-
102
- this.eventEmitter.emit(BotEventType.commandActionCaptureStarted, {
103
- parentMessageId,
104
- chatInfo,
105
- traceId
106
- });
107
-
108
- this.replyCaptures.push(replyAction);
109
-
110
- capture.abortController.signal.addEventListener(
111
- 'abort',
112
- () => {
113
- const capturesWithController = this.replyCaptures.filter(
114
- (x) => x.abortController == capture.abortController
115
- );
116
-
117
- for (const captureToCancel of capturesWithController) {
118
- const index = this.replyCaptures.indexOf(captureToCancel);
119
- this.replyCaptures.splice(index, 1);
120
-
121
- this.eventEmitter.emit(
122
- BotEventType.commandActionCaptureAborted,
123
- {
124
- parentMessageId,
125
- chatInfo,
126
- traceId
127
- }
128
- );
129
- }
130
- },
131
- { once: true }
132
- );
133
- }
134
-
135
- private startMessageProcessing(msg: IncomingMessage) {
136
- this.eventEmitter.emit(BotEventType.messageProcessingStarted, {
137
- botInfo: this.botInfo,
138
- message: msg,
139
- traceId: msg.traceId
140
- });
141
-
142
- const chatHistoryArray = getOrSetIfNotExists(
143
- this.chatHistory,
144
- msg.chatInfo.id,
145
- []
146
- );
147
-
148
- while (chatHistoryArray.length > MESSAGE_HISTORY_LENGTH_LIMIT)
149
- chatHistoryArray.shift();
150
-
151
- chatHistoryArray.push(
152
- new ChatHistoryMessage(
153
- msg.messageId,
154
- msg.from,
155
- msg.text,
156
- msg.type,
157
- msg.traceId,
158
- msg.replyToMessageId,
159
- msg.updateObject.date
160
- )
161
- );
162
-
163
- const commandsToCheck = new Set(this.commands[msg.type]);
164
- if (msg.type != MessageType.Text && msg.text != '') {
165
- for (const command of this.commands[MessageType.Text]) {
166
- commandsToCheck.add(command);
167
- }
168
- }
169
-
170
- this.eventEmitter.emit(BotEventType.beforeActionsExecuting, {
171
- botInfo: this.botInfo,
172
- message: msg,
173
- commands: commandsToCheck,
174
- traceId: msg.traceId
175
- });
176
-
177
- const promises = [...commandsToCheck].map((command) => {
178
- const ctx = new MessageContextInternal<IActionState>(
179
- this.storage,
180
- this.scheduler,
181
- this.eventEmitter,
182
- command,
183
- msg,
184
- this.botName,
185
- this.botInfo
186
- );
187
-
188
- const { proxy, revoke } = Proxy.revocable(ctx, {});
189
-
190
- const executePromise = this.executeAction(command, proxy);
191
-
192
- return executePromise.finally(() => {
193
- revoke();
194
- this.api.flushResponses();
195
- });
196
- });
197
-
198
- if (this.replyCaptures.length != 0) {
199
- const replyPromises = this.replyCaptures.map((capture) => {
200
- const replyCtx = new ReplyContextInternal<IActionState>(
201
- this.storage,
202
- this.scheduler,
203
- this.eventEmitter,
204
- capture,
205
- msg,
206
- this.botName,
207
- this.botInfo
208
- );
209
-
210
- const { proxy, revoke } = Proxy.revocable(replyCtx, {});
211
-
212
- const executePromise = this.executeAction(capture, proxy);
213
-
214
- return executePromise.finally(() => {
215
- revoke();
216
- this.api.flushResponses();
217
- });
218
- });
219
-
220
- promises.push(...replyPromises);
221
- }
222
-
223
- void Promise.allSettled(promises).then(() => {
224
- this.eventEmitter.emit(BotEventType.messageProcessingFinished, {
225
- botInfo: this.botInfo,
226
- message: msg,
227
- traceId: msg.traceId
228
- });
229
- });
230
- }
231
- }
@@ -1,165 +0,0 @@
1
- import { ChatInfo } from '../../dtos/chatInfo';
2
- import { IncomingInlineQuery } from '../../dtos/incomingQuery';
3
- import { InlineQueryAction } from '../../entities/actions/inlineQueryAction';
4
- import { InlineQueryContextInternal } from '../../entities/context/inlineQueryContext';
5
- import { createTrace } from '../../helpers/traceFactory';
6
- import { BotEventType } from '../../types/events';
7
- import { TelegramBot } from '../../types/externalAliases';
8
- import { Milliseconds } from '../../types/timeValues';
9
- import { TelegramApiService } from '../telegramApi';
10
- import { BaseActionProcessor } from './baseProcessor';
11
-
12
- export class InlineQueryActionProcessor extends BaseActionProcessor {
13
- private inlineQueries!: InlineQueryAction[];
14
- /** Fake chat info, since inline queries are chat-less */
15
- private readonly fakeChatInfo = new ChatInfo(
16
- Math.random(),
17
- 'Inline Query',
18
- []
19
- );
20
-
21
- initialize(
22
- api: TelegramApiService,
23
- telegram: TelegramBot,
24
- inlineQueries: InlineQueryAction[],
25
- period: Milliseconds
26
- ) {
27
- this.initializeDependencies(api);
28
- this.inlineQueries = inlineQueries;
29
-
30
- let pendingInlineQueries: IncomingInlineQuery[] = [];
31
-
32
- const queriesInProcessing = new Map<number, IncomingInlineQuery>();
33
-
34
- if (this.inlineQueries.length > 0) {
35
- telegram.on('inline_query', ({ inlineQuery }) => {
36
- const query = new IncomingInlineQuery(
37
- inlineQuery.id,
38
- inlineQuery.query,
39
- inlineQuery.from.id,
40
- createTrace('InlineQuery', this.botName, inlineQuery.id)
41
- );
42
-
43
- this.eventEmitter.emit(BotEventType.inlineQueryRecieved, {
44
- query,
45
- traceId: query.traceId
46
- });
47
-
48
- const queryBeingProcessed = queriesInProcessing.get(
49
- query.userId
50
- );
51
- if (queryBeingProcessed) {
52
- this.eventEmitter.emit(
53
- BotEventType.inlineProcessingAborting,
54
- {
55
- newQuery: query,
56
- abortedQuery: queryBeingProcessed,
57
- traceId: query.traceId
58
- }
59
- );
60
-
61
- queryBeingProcessed.abortController.abort();
62
- queriesInProcessing.delete(query.userId);
63
- }
64
-
65
- pendingInlineQueries = pendingInlineQueries.filter(
66
- (q) => q.userId != query.userId
67
- );
68
-
69
- pendingInlineQueries.push(query);
70
- });
71
-
72
- this.scheduler.createTask(
73
- 'InlineQueryProcessing',
74
- () => {
75
- const queriesToProcess = [...pendingInlineQueries];
76
- pendingInlineQueries = [];
77
- const promises = [];
78
-
79
- for (const inlineQuery of queriesToProcess) {
80
- this.eventEmitter.emit(
81
- BotEventType.inlineProcessingStarted,
82
- {
83
- botName: this.botName,
84
- traceId: inlineQuery.traceId
85
- }
86
- );
87
-
88
- queriesInProcessing.set(
89
- inlineQuery.userId,
90
- inlineQuery
91
- );
92
-
93
- const actionPromises = this.inlineQueries.map(
94
- (inlineQueryAction) => {
95
- const ctx = new InlineQueryContextInternal(
96
- this.storage,
97
- this.scheduler,
98
- this.eventEmitter,
99
- inlineQueryAction,
100
- inlineQuery,
101
- this.fakeChatInfo,
102
- this.botName
103
- );
104
-
105
- const { proxy, revoke } = Proxy.revocable(
106
- ctx,
107
- {}
108
- );
109
-
110
- const executePromise = this.executeAction(
111
- inlineQueryAction,
112
- proxy,
113
- (error, _) => {
114
- if (error.name == 'AbortError') {
115
- this.eventEmitter.emit(
116
- BotEventType.inlineProcessingAborted,
117
- {
118
- abortedQuery: inlineQuery,
119
- traceId: inlineQuery.traceId
120
- }
121
- );
122
- } else {
123
- this.eventEmitter.emit(
124
- BotEventType.error,
125
- {
126
- error,
127
- traceId: inlineQuery.traceId
128
- }
129
- );
130
- }
131
- }
132
- );
133
-
134
- return executePromise.finally(() => {
135
- revoke();
136
- this.api.flushResponses();
137
- });
138
- }
139
- );
140
-
141
- const queryPromise = Promise.allSettled(
142
- actionPromises
143
- ).then(() => {
144
- queriesInProcessing.delete(inlineQuery.userId);
145
- this.eventEmitter.emit(
146
- BotEventType.inlineProcessingFinished,
147
- {
148
- botName: this.botName,
149
- traceId: inlineQuery.traceId
150
- }
151
- );
152
- });
153
-
154
- promises.push(queryPromise);
155
- }
156
-
157
- void Promise.allSettled(promises);
158
- },
159
- period,
160
- false,
161
- this.botName
162
- );
163
- }
164
- }
165
- }
@@ -1,136 +0,0 @@
1
- import moment from 'moment';
2
- import { ChatInfo } from '../../dtos/chatInfo';
3
- import { ScheduledAction } from '../../entities/actions/scheduledAction';
4
- import { ChatContextInternal } from '../../entities/context/chatContext';
5
- import { secondsToMilliseconds } from '../../helpers/timeConvertions';
6
- import { createTrace } from '../../helpers/traceFactory';
7
- import { IActionState } from '../../types/actionState';
8
- import { IScheduler } from '../../types/scheduler';
9
- import { IStorageClient } from '../../types/storage';
10
- import { Seconds, Milliseconds } from '../../types/timeValues';
11
- import { TelegramApiService } from '../telegramApi';
12
- import { BaseActionProcessor } from './baseProcessor';
13
- import { BotEventType, TypedEventEmitter } from '../../types/events';
14
-
15
- export class ScheduledActionProcessor extends BaseActionProcessor {
16
- private readonly chats: Record<string, number>;
17
- private readonly taskTrace = createTrace(
18
- this,
19
- this.botName,
20
- 'ScheduledActionsTaskRun'
21
- );
22
-
23
- private scheduled!: ScheduledAction<IActionState>[];
24
-
25
- constructor(
26
- botName: string,
27
- chats: Record<string, number>,
28
- storage: IStorageClient,
29
- scheduler: IScheduler,
30
- eventEmitter: TypedEventEmitter
31
- ) {
32
- super(botName, storage, scheduler, eventEmitter);
33
- this.chats = chats;
34
- }
35
-
36
- initialize(
37
- api: TelegramApiService,
38
- scheduled: ScheduledAction<IActionState>[],
39
- period: Seconds
40
- ) {
41
- this.initializeDependencies(api);
42
- this.scheduled = scheduled;
43
-
44
- if (this.scheduled.length > 0) {
45
- const now = moment();
46
-
47
- if (now.minute() == 0 && now.second() == 0) {
48
- this.scheduler.createTask(
49
- 'ScheduledProcessing',
50
- () => {
51
- this.runScheduled();
52
- },
53
- secondsToMilliseconds(period),
54
- true,
55
- this.botName
56
- );
57
-
58
- return;
59
- }
60
-
61
- let nextExecutionTime = now.clone().startOf('hour');
62
- if (now.minute() > 0 || now.second() > 0) {
63
- nextExecutionTime = nextExecutionTime.add(1, 'hour');
64
- }
65
-
66
- const delay = nextExecutionTime.diff(now);
67
-
68
- this.scheduler.createOnetimeTask(
69
- 'ScheduledProcessing_OneTime',
70
- () => {
71
- this.scheduler.createTask(
72
- 'ScheduledProcessing',
73
- () => {
74
- this.runScheduled();
75
- },
76
- secondsToMilliseconds(period),
77
- true,
78
- this.botName
79
- );
80
- },
81
- delay as Milliseconds,
82
- this.botName
83
- );
84
-
85
- this.runScheduled();
86
- }
87
- }
88
-
89
- private runScheduled() {
90
- this.eventEmitter.emit(BotEventType.scheduledProcessingStarted, {
91
- botName: this.botName,
92
- traceId: this.taskTrace
93
- });
94
-
95
- const promises = Object.entries(this.chats).flatMap(
96
- ([chatName, chatId]) => {
97
- const chatInfo = new ChatInfo(chatId, chatName, []);
98
-
99
- return this.scheduled.map((scheduledAction) => {
100
- const ctx = new ChatContextInternal<IActionState>(
101
- this.storage,
102
- this.scheduler,
103
- this.eventEmitter,
104
- scheduledAction,
105
- chatInfo,
106
- createTrace(
107
- scheduledAction,
108
- this.botName,
109
- `${scheduledAction.key}-${chatId}`
110
- ),
111
- this.botName
112
- );
113
-
114
- const { proxy, revoke } = Proxy.revocable(ctx, {});
115
-
116
- const executePromise = this.executeAction(
117
- scheduledAction,
118
- proxy
119
- );
120
-
121
- return executePromise.finally(() => {
122
- revoke();
123
- this.api.flushResponses();
124
- });
125
- });
126
- }
127
- );
128
-
129
- void Promise.allSettled(promises).then(() => {
130
- this.eventEmitter.emit(BotEventType.scheduledProcessingStarted, {
131
- botName: this.botName,
132
- traceId: this.taskTrace
133
- });
134
- });
135
- }
136
- }