chz-telegram-bot 0.5.4 → 0.6.8
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.
- package/README.md +41 -31
- package/dist/builtin/helpAction.d.ts +2 -0
- package/dist/builtin/helpAction.d.ts.map +1 -0
- package/dist/builtin/helpAction.js +14 -0
- package/dist/dtos/chatHistoryMessage.d.ts +35 -0
- package/dist/dtos/chatHistoryMessage.d.ts.map +1 -0
- package/dist/dtos/chatHistoryMessage.js +32 -0
- package/dist/dtos/chatInfo.d.ts +17 -0
- package/dist/dtos/chatInfo.d.ts.map +1 -0
- package/dist/dtos/chatInfo.js +16 -0
- package/dist/dtos/commandTriggerCheckResult.d.ts +24 -0
- package/dist/dtos/commandTriggerCheckResult.d.ts.map +1 -0
- package/dist/dtos/commandTriggerCheckResult.js +34 -0
- package/dist/dtos/cooldownInfo.d.ts +13 -0
- package/dist/dtos/cooldownInfo.d.ts.map +1 -0
- package/dist/dtos/cooldownInfo.js +12 -0
- package/dist/dtos/incomingMessage.d.ts +19 -0
- package/dist/dtos/incomingMessage.d.ts.map +1 -0
- package/dist/dtos/incomingMessage.js +63 -0
- package/dist/dtos/incomingQuery.d.ts +10 -0
- package/dist/dtos/incomingQuery.d.ts.map +1 -0
- package/dist/dtos/incomingQuery.js +14 -0
- package/dist/dtos/messageInfo.d.ts +22 -0
- package/dist/dtos/messageInfo.d.ts.map +1 -0
- package/dist/dtos/messageInfo.js +20 -0
- package/dist/dtos/propertyProviderSets.d.ts +16 -0
- package/dist/dtos/propertyProviderSets.d.ts.map +1 -0
- package/dist/dtos/propertyProviderSets.js +1 -0
- package/dist/dtos/replyInfo.d.ts +6 -0
- package/dist/dtos/replyInfo.d.ts.map +1 -0
- package/dist/dtos/replyInfo.js +8 -0
- package/dist/dtos/responses/delay.d.ts +16 -0
- package/dist/dtos/responses/delay.d.ts.map +1 -0
- package/dist/dtos/responses/delay.js +15 -0
- package/dist/dtos/responses/imageMessage.d.ts +22 -0
- package/dist/dtos/responses/imageMessage.d.ts.map +1 -0
- package/dist/dtos/responses/imageMessage.js +21 -0
- package/dist/dtos/responses/inlineQueryResponse.d.ts +13 -0
- package/dist/dtos/responses/inlineQueryResponse.d.ts.map +1 -0
- package/dist/dtos/responses/inlineQueryResponse.js +15 -0
- package/dist/dtos/responses/reaction.d.ts +16 -0
- package/dist/dtos/responses/reaction.d.ts.map +1 -0
- package/dist/dtos/responses/reaction.js +17 -0
- package/dist/dtos/responses/textMessage.d.ts +23 -0
- package/dist/dtos/responses/textMessage.d.ts.map +1 -0
- package/dist/dtos/responses/textMessage.js +24 -0
- package/dist/dtos/responses/unpin.d.ts +15 -0
- package/dist/dtos/responses/unpin.d.ts.map +1 -0
- package/dist/dtos/responses/unpin.js +15 -0
- package/dist/dtos/responses/videoMessage.d.ts +22 -0
- package/dist/dtos/responses/videoMessage.d.ts.map +1 -0
- package/dist/dtos/responses/videoMessage.js +21 -0
- package/dist/dtos/userInfo.d.ts +12 -0
- package/dist/dtos/userInfo.d.ts.map +1 -0
- package/dist/dtos/userInfo.js +12 -0
- package/dist/entities/actions/commandAction.d.ts +31 -0
- package/dist/entities/actions/commandAction.d.ts.map +1 -0
- package/dist/entities/actions/commandAction.js +149 -0
- package/dist/entities/actions/inlineQueryAction.d.ts +14 -0
- package/dist/entities/actions/inlineQueryAction.d.ts.map +1 -0
- package/dist/entities/actions/inlineQueryAction.js +51 -0
- package/dist/entities/actions/replyCaptureAction.d.ts +15 -0
- package/dist/entities/actions/replyCaptureAction.d.ts.map +1 -0
- package/dist/entities/actions/replyCaptureAction.js +66 -0
- package/dist/entities/actions/scheduledAction.d.ts +24 -0
- package/dist/entities/actions/scheduledAction.d.ts.map +1 -0
- package/dist/entities/actions/scheduledAction.js +92 -0
- package/dist/entities/botInstance.d.ts +36 -0
- package/dist/entities/botInstance.d.ts.map +1 -0
- package/dist/entities/botInstance.js +39 -0
- package/dist/entities/cachedStateFactory.d.ts +7 -0
- package/dist/entities/cachedStateFactory.d.ts.map +1 -0
- package/dist/entities/cachedStateFactory.js +8 -0
- package/dist/entities/context/baseContext.d.ts +39 -0
- package/dist/entities/context/baseContext.d.ts.map +1 -0
- package/dist/entities/context/baseContext.js +56 -0
- package/dist/entities/context/chatContext.d.ts +50 -0
- package/dist/entities/context/chatContext.d.ts.map +1 -0
- package/dist/entities/context/chatContext.js +65 -0
- package/dist/entities/context/inlineQueryContext.d.ts +27 -0
- package/dist/entities/context/inlineQueryContext.d.ts.map +1 -0
- package/dist/entities/context/inlineQueryContext.js +29 -0
- package/dist/entities/context/messageContext.d.ts +92 -0
- package/dist/entities/context/messageContext.d.ts.map +1 -0
- package/dist/entities/context/messageContext.js +116 -0
- package/dist/entities/context/replyContext.d.ts +89 -0
- package/dist/entities/context/replyContext.d.ts.map +1 -0
- package/dist/entities/context/replyContext.js +124 -0
- package/dist/entities/states/actionStateBase.d.ts +6 -0
- package/dist/entities/states/actionStateBase.d.ts.map +1 -0
- package/dist/entities/states/actionStateBase.js +4 -0
- package/dist/entities/taskRecord.d.ts +8 -0
- package/dist/entities/taskRecord.d.ts.map +1 -0
- package/dist/entities/taskRecord.js +10 -0
- package/dist/eslint.config.d.ts +3 -0
- package/dist/eslint.config.d.ts.map +1 -0
- package/dist/eslint.config.js +51 -0
- package/dist/helpers/builders/commandActionBuilder.d.ts +100 -0
- package/dist/helpers/builders/commandActionBuilder.d.ts.map +1 -0
- package/dist/helpers/builders/commandActionBuilder.js +146 -0
- package/dist/helpers/builders/inlineQueryActionBuilder.d.ts +37 -0
- package/dist/helpers/builders/inlineQueryActionBuilder.d.ts.map +1 -0
- package/dist/helpers/builders/inlineQueryActionBuilder.js +50 -0
- package/dist/helpers/builders/scheduledActionBuilder.d.ts +68 -0
- package/dist/helpers/builders/scheduledActionBuilder.d.ts.map +1 -0
- package/dist/helpers/builders/scheduledActionBuilder.js +95 -0
- package/dist/helpers/mapUtils.d.ts +10 -0
- package/dist/helpers/mapUtils.d.ts.map +1 -0
- package/dist/helpers/mapUtils.js +13 -0
- package/dist/helpers/noop.d.ts +9 -0
- package/dist/helpers/noop.d.ts.map +1 -0
- package/dist/helpers/noop.js +17 -0
- package/dist/helpers/objectFromEntries.d.ts +2 -0
- package/dist/helpers/objectFromEntries.d.ts.map +1 -0
- package/dist/helpers/objectFromEntries.js +3 -0
- package/dist/helpers/timeConvertions.d.ts +5 -0
- package/dist/helpers/timeConvertions.d.ts.map +1 -0
- package/dist/helpers/timeConvertions.js +9 -0
- package/dist/helpers/toArray.d.ts +2 -0
- package/dist/helpers/toArray.d.ts.map +1 -0
- package/dist/helpers/toArray.js +3 -0
- package/dist/helpers/traceFactory.d.ts +3 -0
- package/dist/helpers/traceFactory.d.ts.map +1 -0
- package/dist/helpers/traceFactory.js +3 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/main.d.ts +49 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +33 -0
- package/dist/services/actionProcessingService.d.ts +25 -0
- package/dist/services/actionProcessingService.d.ts.map +1 -0
- package/dist/services/actionProcessingService.js +50 -0
- package/dist/services/actionProcessors/baseProcessor.d.ts +18 -0
- package/dist/services/actionProcessors/baseProcessor.d.ts.map +1 -0
- package/dist/services/actionProcessors/baseProcessor.js +33 -0
- package/dist/services/actionProcessors/commandActionProcessor.d.ts +19 -0
- package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -0
- package/dist/services/actionProcessors/commandActionProcessor.js +130 -0
- package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts +11 -0
- package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts.map +1 -0
- package/dist/services/actionProcessors/inlineQueryActionProcessor.js +69 -0
- package/dist/services/actionProcessors/scheduledActionProcessor.d.ts +17 -0
- package/dist/services/actionProcessors/scheduledActionProcessor.d.ts.map +1 -0
- package/dist/services/actionProcessors/scheduledActionProcessor.js +55 -0
- package/dist/services/jsonFileStorage.d.ts +25 -0
- package/dist/services/jsonFileStorage.d.ts.map +1 -0
- package/dist/services/jsonFileStorage.js +133 -0
- package/dist/services/nodeTimeoutScheduler.d.ts +13 -0
- package/dist/services/nodeTimeoutScheduler.d.ts.map +1 -0
- package/dist/services/nodeTimeoutScheduler.js +50 -0
- package/dist/services/responseProcessingQueue.d.ts +12 -0
- package/dist/services/responseProcessingQueue.d.ts.map +1 -0
- package/dist/services/responseProcessingQueue.js +37 -0
- package/dist/services/telegramApi.d.ts +23 -0
- package/dist/services/telegramApi.d.ts.map +1 -0
- package/dist/services/telegramApi.js +163 -0
- package/dist/types/action.d.ts +14 -0
- package/dist/types/action.d.ts.map +1 -0
- package/dist/types/action.js +1 -0
- package/dist/types/actionState.d.ts +5 -0
- package/dist/types/actionState.d.ts.map +1 -0
- package/dist/types/actionState.js +1 -0
- package/dist/types/cachedValueAccessor.d.ts +2 -0
- package/dist/types/cachedValueAccessor.d.ts.map +1 -0
- package/dist/types/cachedValueAccessor.js +1 -0
- package/dist/types/capture.d.ts +24 -0
- package/dist/types/capture.d.ts.map +1 -0
- package/dist/types/capture.js +1 -0
- package/dist/types/commandCondition.d.ts +8 -0
- package/dist/types/commandCondition.d.ts.map +1 -0
- package/dist/types/commandCondition.js +1 -0
- package/dist/types/commandTrigger.d.ts +2 -0
- package/dist/types/commandTrigger.d.ts.map +1 -0
- package/dist/types/commandTrigger.js +1 -0
- package/dist/types/events.d.ts +193 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/events.js +69 -0
- package/dist/types/externalAliases.d.ts +11 -0
- package/dist/types/externalAliases.d.ts.map +1 -0
- package/dist/types/externalAliases.js +1 -0
- package/dist/types/handlers.d.ts +21 -0
- package/dist/types/handlers.d.ts.map +1 -0
- package/dist/types/handlers.js +1 -0
- package/dist/types/inputFile.d.ts +5 -0
- package/dist/types/inputFile.d.ts.map +1 -0
- package/dist/types/inputFile.js +1 -0
- package/dist/types/logger.d.ts +1 -0
- package/dist/types/logger.d.ts.map +1 -0
- package/dist/types/logger.js +1 -0
- package/dist/types/messageSendingOptions.d.ts +9 -0
- package/dist/types/messageSendingOptions.d.ts.map +1 -0
- package/dist/types/messageSendingOptions.js +1 -0
- package/dist/types/messageTypes.d.ts +20 -0
- package/dist/types/messageTypes.d.ts.map +1 -0
- package/dist/types/messageTypes.js +18 -0
- package/dist/types/propertyProvider.d.ts +8 -0
- package/dist/types/propertyProvider.d.ts.map +1 -0
- package/dist/types/propertyProvider.js +1 -0
- package/dist/types/response.d.ts +39 -0
- package/dist/types/response.d.ts.map +1 -0
- package/dist/types/response.js +9 -0
- package/dist/types/scheduler.d.ts +7 -0
- package/dist/types/scheduler.d.ts.map +1 -0
- package/dist/types/scheduler.js +1 -0
- package/dist/types/storage.d.ts +11 -0
- package/dist/types/storage.d.ts.map +1 -0
- package/dist/types/storage.js +1 -0
- package/dist/types/timeValues.d.ts +15 -0
- package/dist/types/timeValues.d.ts.map +1 -0
- package/dist/types/timeValues.js +1 -0
- package/dist/types/trace.d.ts +6 -0
- package/dist/types/trace.d.ts.map +1 -0
- package/dist/types/trace.js +1 -0
- package/entities/actions/commandAction.ts +11 -3
- package/entities/actions/inlineQueryAction.ts +9 -1
- package/entities/actions/replyCaptureAction.ts +9 -3
- package/entities/actions/scheduledAction.ts +31 -10
- package/entities/botInstance.ts +18 -25
- package/entities/context/baseContext.ts +9 -4
- package/index.ts +1 -1
- package/main.ts +1 -10
- package/package.json +38 -38
- package/services/actionProcessingService.ts +11 -15
- package/services/actionProcessors/baseProcessor.ts +9 -9
- package/services/actionProcessors/commandActionProcessor.ts +35 -46
- package/services/actionProcessors/inlineQueryActionProcessor.ts +24 -20
- package/services/actionProcessors/scheduledActionProcessor.ts +5 -10
- package/services/jsonFileStorage.ts +27 -1
- package/services/nodeTimeoutScheduler.ts +22 -22
- package/services/telegramApi.ts +53 -23
- package/types/events.ts +285 -0
- package/services/jsonLogger.ts +0 -112
- package/types/logger.ts +0 -39
package/types/events.ts
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { ChatInfo } from '../dtos/chatInfo';
|
|
2
|
+
import { IncomingMessage } from '../dtos/incomingMessage';
|
|
3
|
+
import { IncomingInlineQuery } from '../dtos/incomingQuery';
|
|
4
|
+
import { CommandAction } from '../entities/actions/commandAction';
|
|
5
|
+
import { ChatContext } from '../entities/context/chatContext';
|
|
6
|
+
import { InlineQueryContext } from '../entities/context/inlineQueryContext';
|
|
7
|
+
import { MessageContext } from '../entities/context/messageContext';
|
|
8
|
+
import { ReplyContext } from '../entities/context/replyContext';
|
|
9
|
+
import { ActionKey, IAction, IActionWithState } from './action';
|
|
10
|
+
import { IActionState } from './actionState';
|
|
11
|
+
import { BotInfo } from './externalAliases';
|
|
12
|
+
import { BotResponse } from './response';
|
|
13
|
+
import { Milliseconds } from './timeValues';
|
|
14
|
+
|
|
15
|
+
export const BotEventType = {
|
|
16
|
+
error: 'error.generic',
|
|
17
|
+
|
|
18
|
+
messageRecieved: 'message.recieved',
|
|
19
|
+
messageProcessingStarted: 'message.processingStarted',
|
|
20
|
+
messageProcessingFinished: 'message.processingFinished',
|
|
21
|
+
beforeActionsExecuting: 'message.beforeActionsExecuting',
|
|
22
|
+
|
|
23
|
+
commandActionExecuting: 'command.actionExecuting',
|
|
24
|
+
commandActionExecuted: 'command.actionExecuted',
|
|
25
|
+
commandActionCaptureStarted: 'command.captionStarted',
|
|
26
|
+
commandActionCaptureAborted: 'command.captionAborted',
|
|
27
|
+
|
|
28
|
+
replyActionExecuting: 'reply.actionExecuting',
|
|
29
|
+
replyActionExecuted: 'reply.actionExecuted',
|
|
30
|
+
|
|
31
|
+
inlineActionExecuting: 'inline.actionExecuting',
|
|
32
|
+
inlineActionExecuted: 'inline.actionExecuted',
|
|
33
|
+
inlineProcessingStarted: 'inline.processingStarted',
|
|
34
|
+
inlineProcessingAborting: 'inline.processingAborting',
|
|
35
|
+
inlineProcessingAborted: 'inline.processingAborted',
|
|
36
|
+
|
|
37
|
+
scheduledActionExecuting: 'scheduled.actionExecuting',
|
|
38
|
+
scheduledActionExecuted: 'scheduled.actionExecuted',
|
|
39
|
+
scheduledActionCacheValueReturned: 'scheduled.cachedValueReturned',
|
|
40
|
+
scheduledActionCacheValueCreating: 'scheduled.cachedValueCreating',
|
|
41
|
+
|
|
42
|
+
apiRequestSending: 'api.requestSending',
|
|
43
|
+
apiRequestSent: 'api.requestSent',
|
|
44
|
+
|
|
45
|
+
storageLockAcquiring: 'storage.lockAcquiring',
|
|
46
|
+
storageLockAcquired: 'storage.lockAcquired',
|
|
47
|
+
storageLockReleased: 'storage.lockReleased',
|
|
48
|
+
storageStateSaving: 'storage.stateSaving',
|
|
49
|
+
storageStateSaved: 'storage.stateSaved',
|
|
50
|
+
storageStateLoading: 'storage.stateLoading',
|
|
51
|
+
storageStateLoaded: 'storage.stateLoaded',
|
|
52
|
+
|
|
53
|
+
taskCreated: 'task.created',
|
|
54
|
+
taskRun: 'task.run',
|
|
55
|
+
|
|
56
|
+
botStarting: 'bot.starting',
|
|
57
|
+
botStopping: 'bot.stopping'
|
|
58
|
+
} as const;
|
|
59
|
+
|
|
60
|
+
type BotEventTypeKeys = (typeof BotEventType)[keyof typeof BotEventType];
|
|
61
|
+
// Exhaustiveness validation
|
|
62
|
+
const _checkBotEventMapExhaustive: Record<BotEventTypeKeys, unknown> =
|
|
63
|
+
null as unknown as BotEventMap;
|
|
64
|
+
|
|
65
|
+
export type BotEventMap = {
|
|
66
|
+
[BotEventType.error]: {
|
|
67
|
+
error: Error;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
[BotEventType.messageRecieved]: {
|
|
71
|
+
botInfo: BotInfo;
|
|
72
|
+
message: IncomingMessage;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
[BotEventType.messageProcessingStarted]: {
|
|
76
|
+
botInfo: BotInfo;
|
|
77
|
+
message: IncomingMessage;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
[BotEventType.messageProcessingFinished]: {
|
|
81
|
+
botInfo: BotInfo;
|
|
82
|
+
message: IncomingMessage;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
[BotEventType.beforeActionsExecuting]: {
|
|
86
|
+
botInfo: BotInfo;
|
|
87
|
+
message: IncomingMessage;
|
|
88
|
+
commands: Set<CommandAction<IActionState>>;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
[BotEventType.commandActionExecuting]: {
|
|
92
|
+
action: IActionWithState<IActionState>;
|
|
93
|
+
ctx: MessageContext<IActionState>;
|
|
94
|
+
state: IActionState;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
[BotEventType.commandActionExecuted]: {
|
|
98
|
+
action: IActionWithState<IActionState>;
|
|
99
|
+
ctx: MessageContext<IActionState>;
|
|
100
|
+
state: IActionState;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
[BotEventType.commandActionCaptureStarted]: {
|
|
104
|
+
parentMessageId: number;
|
|
105
|
+
chatInfo: ChatInfo;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
[BotEventType.commandActionCaptureAborted]: {
|
|
109
|
+
parentMessageId: number;
|
|
110
|
+
chatInfo: ChatInfo;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
[BotEventType.replyActionExecuting]: {
|
|
114
|
+
action: IAction;
|
|
115
|
+
ctx: ReplyContext<IActionState>;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
[BotEventType.replyActionExecuted]: {
|
|
119
|
+
action: IAction;
|
|
120
|
+
ctx: ReplyContext<IActionState>;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
[BotEventType.inlineActionExecuting]: {
|
|
124
|
+
action: IAction;
|
|
125
|
+
ctx: InlineQueryContext;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
[BotEventType.inlineActionExecuted]: {
|
|
129
|
+
action: IAction;
|
|
130
|
+
ctx: InlineQueryContext;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
[BotEventType.inlineProcessingStarted]: {
|
|
134
|
+
query: IncomingInlineQuery;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
[BotEventType.inlineProcessingAborting]: {
|
|
138
|
+
abortedQuery: IncomingInlineQuery;
|
|
139
|
+
newQuery: IncomingInlineQuery;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
[BotEventType.inlineProcessingAborted]: {
|
|
143
|
+
abortedQuery: IncomingInlineQuery;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
[BotEventType.scheduledActionExecuting]: {
|
|
147
|
+
action: IAction;
|
|
148
|
+
ctx: ChatContext<IActionState>;
|
|
149
|
+
state: IActionState;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
[BotEventType.scheduledActionExecuted]: {
|
|
153
|
+
action: IAction;
|
|
154
|
+
ctx: ChatContext<IActionState>;
|
|
155
|
+
state: IActionState;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
[BotEventType.scheduledActionCacheValueCreating]: {
|
|
159
|
+
action: IAction;
|
|
160
|
+
ctx: ChatContext<IActionState>;
|
|
161
|
+
key: string;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
[BotEventType.scheduledActionCacheValueReturned]: {
|
|
165
|
+
action: IAction;
|
|
166
|
+
ctx: ChatContext<IActionState>;
|
|
167
|
+
key: string;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
[BotEventType.apiRequestSending]: {
|
|
171
|
+
response: BotResponse | null;
|
|
172
|
+
telegramMethod: string | null;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
[BotEventType.apiRequestSent]: {
|
|
176
|
+
response: BotResponse | null;
|
|
177
|
+
telegramMethod: string | null;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
[BotEventType.storageLockAcquiring]: ActionKey;
|
|
181
|
+
[BotEventType.storageLockAcquired]: ActionKey;
|
|
182
|
+
[BotEventType.storageLockReleased]: ActionKey;
|
|
183
|
+
[BotEventType.storageStateSaved]: {
|
|
184
|
+
key: ActionKey;
|
|
185
|
+
data: Record<number, unknown>;
|
|
186
|
+
};
|
|
187
|
+
[BotEventType.storageStateSaving]: {
|
|
188
|
+
key: ActionKey;
|
|
189
|
+
data: Record<number, unknown>;
|
|
190
|
+
};
|
|
191
|
+
[BotEventType.storageStateLoading]: {
|
|
192
|
+
action: IActionWithState<IActionState>;
|
|
193
|
+
chatId: number;
|
|
194
|
+
};
|
|
195
|
+
[BotEventType.storageStateLoaded]: {
|
|
196
|
+
action: IActionWithState<IActionState>;
|
|
197
|
+
chatId: number;
|
|
198
|
+
state: IActionState;
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
[BotEventType.taskCreated]: {
|
|
202
|
+
name: string;
|
|
203
|
+
ownerName: string;
|
|
204
|
+
delay?: Milliseconds;
|
|
205
|
+
interval?: Milliseconds;
|
|
206
|
+
};
|
|
207
|
+
[BotEventType.taskRun]: {
|
|
208
|
+
name: string;
|
|
209
|
+
ownerName: string;
|
|
210
|
+
delay?: Milliseconds;
|
|
211
|
+
interval?: Milliseconds;
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
[BotEventType.botStarting]: {
|
|
215
|
+
botName: string;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
[BotEventType.botStopping]: {
|
|
219
|
+
botName: string;
|
|
220
|
+
};
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
type ListenerArgs<K extends keyof BotEventMap> =
|
|
224
|
+
BotEventMap[K] extends undefined ? [] : [BotEventMap[K]];
|
|
225
|
+
|
|
226
|
+
export type Listener<K extends keyof BotEventMap> = (
|
|
227
|
+
timestamp: number,
|
|
228
|
+
...args: ListenerArgs<K>
|
|
229
|
+
) => void;
|
|
230
|
+
|
|
231
|
+
export type EachListener = (
|
|
232
|
+
event: BotEventTypeKeys,
|
|
233
|
+
timestamp: number,
|
|
234
|
+
data: unknown
|
|
235
|
+
) => void;
|
|
236
|
+
|
|
237
|
+
export class TypedEventEmitter {
|
|
238
|
+
private readonly listeners = new Map<
|
|
239
|
+
keyof BotEventMap | '*',
|
|
240
|
+
Set<Listener<keyof BotEventMap> | EachListener>
|
|
241
|
+
>();
|
|
242
|
+
|
|
243
|
+
on<K extends keyof BotEventMap>(event: K, fn: Listener<K>) {
|
|
244
|
+
const set = this.listeners.get(event) ?? new Set();
|
|
245
|
+
set.add(fn as Listener<keyof BotEventMap>);
|
|
246
|
+
this.listeners.set(event, set);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
onEach(fn: EachListener) {
|
|
250
|
+
const event = '*';
|
|
251
|
+
const set = this.listeners.get(event) ?? new Set();
|
|
252
|
+
set.add(fn);
|
|
253
|
+
this.listeners.set(event, set);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
emit<K extends keyof BotEventMap>(
|
|
257
|
+
event: K,
|
|
258
|
+
...args: ListenerArgs<K>
|
|
259
|
+
): void {
|
|
260
|
+
const timestamp = Date.now();
|
|
261
|
+
const specific = this.listeners.get(event);
|
|
262
|
+
if (specific) {
|
|
263
|
+
for (const fn of specific) {
|
|
264
|
+
(fn as Listener<K>)(timestamp, ...args);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const anySet = this.listeners.get('*');
|
|
269
|
+
if (anySet) {
|
|
270
|
+
for (const fn of anySet) {
|
|
271
|
+
(
|
|
272
|
+
fn as unknown as (
|
|
273
|
+
e: K,
|
|
274
|
+
t: number,
|
|
275
|
+
...a: ListenerArgs<K>
|
|
276
|
+
) => void
|
|
277
|
+
)(event, timestamp, ...args);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
events(): (keyof BotEventMap | '*')[] {
|
|
283
|
+
return [...this.listeners.keys()];
|
|
284
|
+
}
|
|
285
|
+
}
|
package/services/jsonLogger.ts
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { ILogger, IScopedLogger } from '../types/logger';
|
|
2
|
-
import { TraceId } from '../types/trace';
|
|
3
|
-
|
|
4
|
-
export class JsonLogger implements ILogger {
|
|
5
|
-
private serializeError(error: unknown): string {
|
|
6
|
-
if (error instanceof Error) {
|
|
7
|
-
const plainObject: Record<string, unknown> = {
|
|
8
|
-
name: error.name,
|
|
9
|
-
message: error.message,
|
|
10
|
-
stack: error.stack
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
for (const [key, value] of Object.entries(error)) {
|
|
14
|
-
plainObject[key] = value;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return JSON.stringify(plainObject);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return JSON.stringify({ error });
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
private getCircularReplacer() {
|
|
24
|
-
const cache = new Set();
|
|
25
|
-
return <T>(_: string, value: T) => {
|
|
26
|
-
if (typeof value === 'object' && value !== null) {
|
|
27
|
-
if (cache.has(value)) {
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
cache.add(value);
|
|
32
|
-
}
|
|
33
|
-
return value;
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
createScope(botName: string, traceId: TraceId, chatName: string) {
|
|
38
|
-
return {
|
|
39
|
-
logObjectWithTraceId: (data: unknown) => {
|
|
40
|
-
this.logObjectWithTraceId(botName, traceId, chatName, data);
|
|
41
|
-
},
|
|
42
|
-
logWithTraceId: (text: string) => {
|
|
43
|
-
this.logWithTraceId(botName, traceId, chatName, text);
|
|
44
|
-
},
|
|
45
|
-
errorWithTraceId: (errorObj: unknown, extraData?: unknown) => {
|
|
46
|
-
this.errorWithTraceId(
|
|
47
|
-
botName,
|
|
48
|
-
traceId,
|
|
49
|
-
chatName,
|
|
50
|
-
errorObj,
|
|
51
|
-
extraData
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
} as IScopedLogger;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
logObjectWithTraceId(
|
|
58
|
-
botName: string,
|
|
59
|
-
traceId: TraceId,
|
|
60
|
-
chatName: string,
|
|
61
|
-
data: unknown
|
|
62
|
-
) {
|
|
63
|
-
const enrichedData =
|
|
64
|
-
typeof data == 'object'
|
|
65
|
-
? {
|
|
66
|
-
...data,
|
|
67
|
-
botName,
|
|
68
|
-
traceId,
|
|
69
|
-
chatName
|
|
70
|
-
}
|
|
71
|
-
: {
|
|
72
|
-
botName,
|
|
73
|
-
traceId,
|
|
74
|
-
chatName,
|
|
75
|
-
data
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
console.log(enrichedData);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
logWithTraceId(
|
|
82
|
-
botName: string,
|
|
83
|
-
traceId: TraceId,
|
|
84
|
-
chatName: string,
|
|
85
|
-
text: string
|
|
86
|
-
) {
|
|
87
|
-
console.log(
|
|
88
|
-
`{"botName":"${botName}","traceId":"${traceId}","chatName":"${chatName}","text":"${text}"}`
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
errorWithTraceId(
|
|
93
|
-
botName: string,
|
|
94
|
-
traceId: TraceId,
|
|
95
|
-
chatName: string,
|
|
96
|
-
errorObj: unknown,
|
|
97
|
-
extraData?: unknown
|
|
98
|
-
) {
|
|
99
|
-
const dataString = extraData
|
|
100
|
-
? `,"extraData":${JSON.stringify(
|
|
101
|
-
extraData,
|
|
102
|
-
this.getCircularReplacer()
|
|
103
|
-
)}`
|
|
104
|
-
: '';
|
|
105
|
-
|
|
106
|
-
console.error(
|
|
107
|
-
`{"botName":"${botName}","traceId":"${traceId}","chatName":"${chatName}","error":${this.serializeError(
|
|
108
|
-
errorObj
|
|
109
|
-
)}${dataString}}`
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
}
|
package/types/logger.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { TraceId } from './trace';
|
|
2
|
-
|
|
3
|
-
export interface IScopedLogger {
|
|
4
|
-
logObjectWithTraceId(data: unknown): void;
|
|
5
|
-
|
|
6
|
-
logWithTraceId(text: string): void;
|
|
7
|
-
|
|
8
|
-
errorWithTraceId(errorObj: unknown, extraData?: unknown): void;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface ILogger {
|
|
12
|
-
createScope(
|
|
13
|
-
botName: string,
|
|
14
|
-
traceId: TraceId,
|
|
15
|
-
chatName: string
|
|
16
|
-
): IScopedLogger;
|
|
17
|
-
|
|
18
|
-
logObjectWithTraceId(
|
|
19
|
-
botName: string,
|
|
20
|
-
traceId: TraceId,
|
|
21
|
-
chatName: string,
|
|
22
|
-
data: unknown
|
|
23
|
-
): void;
|
|
24
|
-
|
|
25
|
-
logWithTraceId(
|
|
26
|
-
botName: string,
|
|
27
|
-
traceId: TraceId,
|
|
28
|
-
chatName: string,
|
|
29
|
-
text: string
|
|
30
|
-
): void;
|
|
31
|
-
|
|
32
|
-
errorWithTraceId(
|
|
33
|
-
botName: string,
|
|
34
|
-
traceId: TraceId,
|
|
35
|
-
chatName: string,
|
|
36
|
-
errorObj: unknown,
|
|
37
|
-
extraData?: unknown
|
|
38
|
-
): void;
|
|
39
|
-
}
|