chz-telegram-bot 0.3.14 → 0.3.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/dist/builtin/helpAction.d.ts +1 -1
- package/dist/builtin/helpAction.d.ts.map +1 -1
- package/dist/dtos/chatInfo.d.ts +10 -1
- package/dist/dtos/chatInfo.d.ts.map +1 -1
- package/dist/dtos/chatInfo.js +11 -5
- package/dist/dtos/commandTriggerCheckResult.d.ts +5 -5
- package/dist/dtos/commandTriggerCheckResult.d.ts.map +1 -1
- package/dist/dtos/commandTriggerCheckResult.js +4 -4
- package/dist/dtos/cooldownInfo.d.ts +9 -3
- package/dist/dtos/cooldownInfo.d.ts.map +1 -1
- package/dist/dtos/cooldownInfo.js +5 -1
- package/dist/dtos/incomingMessage.d.ts +1 -1
- package/dist/dtos/incomingMessage.d.ts.map +1 -1
- package/dist/dtos/incomingMessage.js +2 -2
- package/dist/dtos/incomingQuery.d.ts.map +1 -1
- package/dist/dtos/messageInfo.d.ts +21 -0
- package/dist/dtos/messageInfo.d.ts.map +1 -0
- package/dist/dtos/messageInfo.js +24 -0
- package/dist/dtos/userInfo.d.ts +12 -0
- package/dist/dtos/userInfo.d.ts.map +1 -0
- package/dist/dtos/userInfo.js +16 -0
- package/dist/entities/actions/commandAction.d.ts +2 -2
- package/dist/entities/actions/commandAction.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.js +7 -7
- package/dist/entities/actions/inlineQueryAction.d.ts +2 -2
- package/dist/entities/actions/inlineQueryAction.d.ts.map +1 -1
- package/dist/entities/actions/replyCaptureAction.d.ts +4 -4
- package/dist/entities/actions/replyCaptureAction.d.ts.map +1 -1
- package/dist/entities/actions/replyCaptureAction.js +4 -4
- package/dist/entities/actions/scheduledAction.d.ts +2 -2
- package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
- package/dist/entities/context/baseContext.d.ts +2 -1
- package/dist/entities/context/baseContext.d.ts.map +1 -1
- package/dist/entities/context/baseContext.js +3 -3
- package/dist/entities/context/chatContext.d.ts +3 -2
- package/dist/entities/context/chatContext.d.ts.map +1 -1
- package/dist/entities/context/chatContext.js +3 -3
- package/dist/entities/context/inlineQueryContext.d.ts +3 -2
- package/dist/entities/context/inlineQueryContext.d.ts.map +1 -1
- package/dist/entities/context/inlineQueryContext.js +3 -3
- package/dist/entities/context/messageContext.d.ts +11 -15
- package/dist/entities/context/messageContext.d.ts.map +1 -1
- package/dist/entities/context/messageContext.js +15 -20
- package/dist/entities/context/replyContext.d.ts +9 -15
- package/dist/entities/context/replyContext.d.ts.map +1 -1
- package/dist/entities/context/replyContext.js +12 -20
- package/dist/helpers/mapUtils.d.ts +9 -2
- package/dist/helpers/mapUtils.d.ts.map +1 -1
- package/dist/helpers/mapUtils.js +1 -1
- package/dist/index.d.ts +13 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -11
- package/dist/services/actionProcessors/baseProcessor.d.ts +2 -2
- package/dist/services/actionProcessors/baseProcessor.d.ts.map +1 -1
- package/dist/services/actionProcessors/commandActionProcessor.d.ts +1 -0
- package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -1
- package/dist/services/actionProcessors/commandActionProcessor.js +18 -19
- package/dist/services/actionProcessors/inlineQueryActionProcessor.js +1 -1
- package/dist/services/actionProcessors/scheduledActionProcessor.d.ts.map +1 -1
- package/dist/services/actionProcessors/scheduledActionProcessor.js +2 -2
- package/dist/types/action.d.ts +2 -2
- package/dist/types/action.d.ts.map +1 -1
- package/dist/types/capture.d.ts +3 -3
- package/dist/types/capture.d.ts.map +1 -1
- package/dtos/chatInfo.ts +10 -9
- package/dtos/commandTriggerCheckResult.ts +5 -15
- package/dtos/cooldownInfo.ts +6 -1
- package/dtos/incomingMessage.ts +7 -2
- package/dtos/incomingQuery.ts +4 -12
- package/dtos/messageInfo.ts +17 -0
- package/dtos/userInfo.ts +8 -0
- package/entities/actions/commandAction.ts +11 -11
- package/entities/actions/inlineQueryAction.ts +2 -2
- package/entities/actions/replyCaptureAction.ts +9 -9
- package/entities/actions/scheduledAction.ts +2 -2
- package/entities/context/baseContext.ts +11 -1
- package/entities/context/chatContext.ts +16 -5
- package/entities/context/inlineQueryContext.ts +10 -2
- package/entities/context/messageContext.ts +24 -25
- package/entities/context/replyContext.ts +26 -25
- package/helpers/mapUtils.ts +15 -2
- package/index.ts +13 -9
- package/package.json +15 -1
- package/services/actionProcessors/baseProcessor.ts +3 -3
- package/services/actionProcessors/commandActionProcessor.ts +43 -21
- package/services/actionProcessors/inlineQueryActionProcessor.ts +3 -3
- package/services/actionProcessors/scheduledActionProcessor.ts +7 -4
- package/types/action.ts +2 -2
- package/types/capture.ts +5 -3
- package/chz-telegram-bot-0.3.14.tgz +0 -0
package/index.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export { CommandActionBuilderWithState } from './helpers/builders/commandActionBuilder';
|
|
5
|
-
export { IStorageClient } from './types/storage';
|
|
6
|
-
export { ILogger } from './types/logger';
|
|
7
|
-
export { IScheduler } from './types/scheduler';
|
|
1
|
+
export * from './main';
|
|
2
|
+
export * from './helpers/builders/inlineQueryActionBuilder';
|
|
3
|
+
export * from './helpers/builders/commandActionBuilder';
|
|
8
4
|
export * from './helpers/builders/scheduledActionBuilder';
|
|
9
5
|
export * from './types/actionState';
|
|
10
6
|
export * from './entities/states/actionStateBase';
|
|
7
|
+
export * from './types/messageTypes';
|
|
8
|
+
export * from './helpers/timeConvertions';
|
|
9
|
+
export { CommandAction } from './entities/actions/commandAction';
|
|
10
|
+
export { InlineQueryAction } from './entities/actions/inlineQueryAction';
|
|
11
|
+
export { ReplyCaptureAction } from './entities/actions/replyCaptureAction';
|
|
12
|
+
export { ScheduledAction } from './entities/actions/scheduledAction';
|
|
11
13
|
export { Hours, Milliseconds, Seconds } from './types/timeValues';
|
|
12
|
-
export {
|
|
13
|
-
export {
|
|
14
|
+
export { ChatContext } from './entities/context/chatContext';
|
|
15
|
+
export { InlineQueryContext } from './entities/context/inlineQueryContext';
|
|
16
|
+
export { MessageContext } from './entities/context/messageContext';
|
|
17
|
+
export { ReplyContext } from './entities/context/replyContext';
|
package/package.json
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "chz-telegram-bot",
|
|
3
|
-
"
|
|
3
|
+
"description": "Opinionated TypeScript framework that provides a structured approach to building Telegram bots.",
|
|
4
|
+
"author": {
|
|
5
|
+
"name": "Alex Halanin",
|
|
6
|
+
"url": "https://github.com/AlexSolari"
|
|
7
|
+
},
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"telegram",
|
|
11
|
+
"telegram bot"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/AlexSolari/botFramework.git"
|
|
16
|
+
},
|
|
17
|
+
"version": "0.3.16",
|
|
4
18
|
"type": "module",
|
|
5
19
|
"dependencies": {
|
|
6
20
|
"async-sema": "^3.1.1",
|
|
@@ -3,7 +3,7 @@ import { IScheduler } from '../../types/scheduler';
|
|
|
3
3
|
import { IStorageClient } from '../../types/storage';
|
|
4
4
|
import { TelegramApiService } from '../telegramApi';
|
|
5
5
|
import { IAction } from '../../types/action';
|
|
6
|
-
import {
|
|
6
|
+
import { BaseContextInternal } from '../../entities/context/baseContext';
|
|
7
7
|
|
|
8
8
|
export abstract class BaseActionProcessor {
|
|
9
9
|
protected readonly storage: IStorageClient;
|
|
@@ -29,7 +29,7 @@ export abstract class BaseActionProcessor {
|
|
|
29
29
|
|
|
30
30
|
private defaultErrorHandler<TAction extends IAction>(
|
|
31
31
|
error: Error,
|
|
32
|
-
ctx:
|
|
32
|
+
ctx: BaseContextInternal<TAction>
|
|
33
33
|
) {
|
|
34
34
|
ctx.logger.errorWithTraceId(error, ctx);
|
|
35
35
|
}
|
|
@@ -40,7 +40,7 @@ export abstract class BaseActionProcessor {
|
|
|
40
40
|
|
|
41
41
|
async executeAction<
|
|
42
42
|
TAction extends IAction,
|
|
43
|
-
TActionContext extends
|
|
43
|
+
TActionContext extends BaseContextInternal<TAction>
|
|
44
44
|
>(
|
|
45
45
|
action: TAction,
|
|
46
46
|
ctx: TActionContext,
|
|
@@ -2,8 +2,8 @@ import { Telegraf } from 'telegraf';
|
|
|
2
2
|
import { IncomingMessage } from '../../dtos/incomingMessage';
|
|
3
3
|
import { CommandAction } from '../../entities/actions/commandAction';
|
|
4
4
|
import { ReplyCaptureAction } from '../../entities/actions/replyCaptureAction';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { MessageContextInternal } from '../../entities/context/messageContext';
|
|
6
|
+
import { ReplyContextInternal } from '../../entities/context/replyContext';
|
|
7
7
|
import { IActionState } from '../../types/actionState';
|
|
8
8
|
import { TelegramApiService } from '../telegramApi';
|
|
9
9
|
import { IReplyCapture } from '../../types/capture';
|
|
@@ -16,10 +16,16 @@ import {
|
|
|
16
16
|
import { typeSafeObjectFromEntries } from '../../helpers/objectFromEntries';
|
|
17
17
|
import { BaseActionProcessor } from './baseProcessor';
|
|
18
18
|
import { UserFromGetMe } from 'telegraf/types';
|
|
19
|
+
import { getOrSetIfNotExists } from '../../helpers/mapUtils';
|
|
20
|
+
import { MessageInfo } from '../../dtos/messageInfo';
|
|
21
|
+
import { UserInfo } from '../../dtos/userInfo';
|
|
22
|
+
|
|
23
|
+
const MESSAGE_HISTORY_LENGTH_LIMIT = 100;
|
|
19
24
|
|
|
20
25
|
export class CommandActionProcessor extends BaseActionProcessor {
|
|
21
26
|
private readonly replyCaptures: ReplyCaptureAction<IActionState>[] = [];
|
|
22
27
|
private botInfo!: UserFromGetMe;
|
|
28
|
+
private lastMessages = new Map<number, IncomingMessage[]>();
|
|
23
29
|
|
|
24
30
|
private commands = typeSafeObjectFromEntries(
|
|
25
31
|
Object.values(MessageType).map((x) => [
|
|
@@ -67,7 +73,8 @@ export class CommandActionProcessor extends BaseActionProcessor {
|
|
|
67
73
|
telegraf.on('message', (ctx) => {
|
|
68
74
|
const msg = new IncomingMessage(
|
|
69
75
|
ctx.update.message,
|
|
70
|
-
this.botName
|
|
76
|
+
this.botName,
|
|
77
|
+
getOrSetIfNotExists(this.lastMessages, ctx.chat.id, [])
|
|
71
78
|
);
|
|
72
79
|
|
|
73
80
|
const logger = this.logger.createScope(
|
|
@@ -127,7 +134,16 @@ export class CommandActionProcessor extends BaseActionProcessor {
|
|
|
127
134
|
});
|
|
128
135
|
}
|
|
129
136
|
private async processMessage(msg: IncomingMessage) {
|
|
130
|
-
const
|
|
137
|
+
const messageHistoryArray = getOrSetIfNotExists(
|
|
138
|
+
this.lastMessages,
|
|
139
|
+
msg.chatInfo.id,
|
|
140
|
+
[]
|
|
141
|
+
);
|
|
142
|
+
if (messageHistoryArray.length > MESSAGE_HISTORY_LENGTH_LIMIT)
|
|
143
|
+
messageHistoryArray.shift();
|
|
144
|
+
messageHistoryArray.push(msg);
|
|
145
|
+
|
|
146
|
+
const ctx = new MessageContextInternal<IActionState>(
|
|
131
147
|
this.storage,
|
|
132
148
|
this.scheduler
|
|
133
149
|
);
|
|
@@ -145,7 +161,7 @@ export class CommandActionProcessor extends BaseActionProcessor {
|
|
|
145
161
|
}
|
|
146
162
|
|
|
147
163
|
if (this.replyCaptures.length != 0) {
|
|
148
|
-
const replyCtx = new
|
|
164
|
+
const replyCtx = new ReplyContextInternal<IActionState>(
|
|
149
165
|
this.storage,
|
|
150
166
|
this.scheduler
|
|
151
167
|
);
|
|
@@ -160,19 +176,22 @@ export class CommandActionProcessor extends BaseActionProcessor {
|
|
|
160
176
|
}
|
|
161
177
|
|
|
162
178
|
private initializeReplyCaptureContext(
|
|
163
|
-
ctx:
|
|
179
|
+
ctx: ReplyContextInternal<IActionState>,
|
|
164
180
|
action: ReplyCaptureAction<IActionState>,
|
|
165
181
|
message: IncomingMessage
|
|
166
182
|
) {
|
|
167
183
|
ctx.replyMessageId = message.replyToMessageId;
|
|
168
|
-
ctx.
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
184
|
+
ctx.messageInfo = new MessageInfo(
|
|
185
|
+
message.messageId,
|
|
186
|
+
message.text,
|
|
187
|
+
message.type,
|
|
188
|
+
message.updateObject
|
|
189
|
+
);
|
|
190
|
+
ctx.userInfo = new UserInfo(
|
|
191
|
+
message.from?.id ?? -1,
|
|
173
192
|
(message.from?.first_name ?? 'Unknown user') +
|
|
174
|
-
|
|
175
|
-
|
|
193
|
+
(message.from?.last_name ? ` ${message.from.last_name}` : '')
|
|
194
|
+
);
|
|
176
195
|
ctx.botName = this.botName;
|
|
177
196
|
ctx.action = action;
|
|
178
197
|
ctx.chatInfo = message.chatInfo;
|
|
@@ -190,18 +209,21 @@ export class CommandActionProcessor extends BaseActionProcessor {
|
|
|
190
209
|
}
|
|
191
210
|
|
|
192
211
|
private initializeMessageContext(
|
|
193
|
-
ctx:
|
|
212
|
+
ctx: MessageContextInternal<IActionState>,
|
|
194
213
|
action: CommandAction<IActionState>,
|
|
195
214
|
message: IncomingMessage
|
|
196
215
|
) {
|
|
197
|
-
ctx.
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
216
|
+
ctx.messageInfo = new MessageInfo(
|
|
217
|
+
message.messageId,
|
|
218
|
+
message.text,
|
|
219
|
+
message.type,
|
|
220
|
+
message.updateObject
|
|
221
|
+
);
|
|
222
|
+
ctx.userInfo = new UserInfo(
|
|
223
|
+
message.from?.id ?? -1,
|
|
202
224
|
(message.from?.first_name ?? 'Unknown user') +
|
|
203
|
-
|
|
204
|
-
|
|
225
|
+
(message.from?.last_name ? ` ${message.from.last_name}` : '')
|
|
226
|
+
);
|
|
205
227
|
|
|
206
228
|
ctx.matchResults = [];
|
|
207
229
|
ctx.startCooldown = true;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Telegraf } from 'telegraf';
|
|
2
2
|
import { IncomingInlineQuery } from '../../dtos/incomingQuery';
|
|
3
3
|
import { InlineQueryAction } from '../../entities/actions/inlineQueryAction';
|
|
4
|
-
import {
|
|
4
|
+
import { InlineQueryContextInternal } from '../../entities/context/inlineQueryContext';
|
|
5
5
|
import { createTrace } from '../../helpers/traceFactory';
|
|
6
6
|
import { Milliseconds } from '../../types/timeValues';
|
|
7
7
|
import { TraceId } from '../../types/trace';
|
|
@@ -67,7 +67,7 @@ export class InlineQueryActionProcessor extends BaseActionProcessor {
|
|
|
67
67
|
this.scheduler.createTask(
|
|
68
68
|
'InlineQueryProcessing',
|
|
69
69
|
async () => {
|
|
70
|
-
const ctx = new
|
|
70
|
+
const ctx = new InlineQueryContextInternal(
|
|
71
71
|
this.storage,
|
|
72
72
|
this.scheduler
|
|
73
73
|
);
|
|
@@ -119,7 +119,7 @@ export class InlineQueryActionProcessor extends BaseActionProcessor {
|
|
|
119
119
|
}
|
|
120
120
|
|
|
121
121
|
private initializeInlineQueryContext(
|
|
122
|
-
ctx:
|
|
122
|
+
ctx: InlineQueryContextInternal,
|
|
123
123
|
queryText: string,
|
|
124
124
|
queryId: string,
|
|
125
125
|
action: InlineQueryAction,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import moment from 'moment';
|
|
2
2
|
import { ChatInfo } from '../../dtos/chatInfo';
|
|
3
3
|
import { ScheduledAction } from '../../entities/actions/scheduledAction';
|
|
4
|
-
import {
|
|
4
|
+
import { ChatContextInternal } from '../../entities/context/chatContext';
|
|
5
5
|
import { secondsToMilliseconds } from '../../helpers/timeConvertions';
|
|
6
6
|
import { createTrace } from '../../helpers/traceFactory';
|
|
7
7
|
import { IActionState } from '../../types/actionState';
|
|
@@ -81,14 +81,17 @@ export class ScheduledActionProcessor extends BaseActionProcessor {
|
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
private async runScheduled() {
|
|
84
|
-
const ctx = new
|
|
84
|
+
const ctx = new ChatContextInternal<IActionState>(
|
|
85
|
+
this.storage,
|
|
86
|
+
this.scheduler
|
|
87
|
+
);
|
|
85
88
|
|
|
86
89
|
for (const [chatName, chatId] of Object.entries(this.chats)) {
|
|
87
90
|
for (const scheduledAction of this.scheduled) {
|
|
88
91
|
this.initializeChatContext(
|
|
89
92
|
ctx,
|
|
90
93
|
scheduledAction,
|
|
91
|
-
new ChatInfo(chatId, chatName),
|
|
94
|
+
new ChatInfo(chatId, chatName, []),
|
|
92
95
|
createTrace(
|
|
93
96
|
scheduledAction,
|
|
94
97
|
this.botName,
|
|
@@ -104,7 +107,7 @@ export class ScheduledActionProcessor extends BaseActionProcessor {
|
|
|
104
107
|
}
|
|
105
108
|
|
|
106
109
|
private initializeChatContext(
|
|
107
|
-
ctx:
|
|
110
|
+
ctx: ChatContextInternal<IActionState>,
|
|
108
111
|
action: ScheduledAction<IActionState>,
|
|
109
112
|
chatInfo: ChatInfo,
|
|
110
113
|
traceId: TraceId
|
package/types/action.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BaseContextInternal } from '../entities/context/baseContext';
|
|
2
2
|
import { IActionState } from './actionState';
|
|
3
3
|
import { BotResponse } from './response';
|
|
4
4
|
|
|
@@ -11,5 +11,5 @@ export interface IActionWithState<TActionState extends IActionState>
|
|
|
11
11
|
|
|
12
12
|
export interface IAction {
|
|
13
13
|
readonly key: ActionKey;
|
|
14
|
-
exec(ctx:
|
|
14
|
+
exec(ctx: BaseContextInternal<IAction>): Promise<BotResponse[]>;
|
|
15
15
|
}
|
package/types/capture.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ReplyContextInternal } from '../entities/context/replyContext';
|
|
2
2
|
import { IActionState } from './actionState';
|
|
3
3
|
import { CommandTrigger } from './commandTrigger';
|
|
4
4
|
import { IActionWithState } from './action';
|
|
@@ -13,7 +13,7 @@ export interface ICaptureController {
|
|
|
13
13
|
trigger: CommandTrigger[],
|
|
14
14
|
/** Callback that will be called on trigger */
|
|
15
15
|
handler: (
|
|
16
|
-
replyContext:
|
|
16
|
+
replyContext: ReplyContextInternal<TParentActionState>
|
|
17
17
|
) => Promise<void>,
|
|
18
18
|
/** Abort controller to abort capturing manually */
|
|
19
19
|
abortController: AbortController
|
|
@@ -22,7 +22,9 @@ export interface ICaptureController {
|
|
|
22
22
|
|
|
23
23
|
export interface IReplyCapture {
|
|
24
24
|
trigger: CommandTrigger[];
|
|
25
|
-
handler: (
|
|
25
|
+
handler: (
|
|
26
|
+
replyContext: ReplyContextInternal<IActionState>
|
|
27
|
+
) => Promise<void>;
|
|
26
28
|
abortController: AbortController;
|
|
27
29
|
action: IActionWithState<IActionState>;
|
|
28
30
|
}
|
|
Binary file
|