chz-telegram-bot 0.0.14 → 0.0.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/dist/entities/actionExecutionResult.d.ts +7 -0
- package/dist/entities/actionExecutionResult.d.ts.map +1 -0
- package/dist/entities/{transactionResult.js → actionExecutionResult.js} +3 -2
- package/dist/entities/actions/commandAction.d.ts +4 -4
- package/dist/entities/actions/commandAction.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.js +12 -11
- package/dist/entities/actions/scheduledAction.d.ts +5 -5
- package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
- package/dist/entities/actions/scheduledAction.js +10 -9
- package/dist/entities/{bot.d.ts → botInstance.d.ts} +5 -5
- package/dist/entities/botInstance.d.ts.map +1 -0
- package/dist/entities/{bot.js → botInstance.js} +18 -20
- package/dist/entities/cachedStateFactory.d.ts +1 -1
- package/dist/entities/cachedStateFactory.d.ts.map +1 -1
- package/dist/entities/cachedStateFactory.js +2 -1
- package/dist/entities/commandTriggerCheckResult.d.ts +1 -1
- package/dist/entities/commandTriggerCheckResult.d.ts.map +1 -1
- package/dist/entities/commandTriggerCheckResult.js +2 -1
- package/dist/entities/context/chatContext.d.ts +1 -1
- package/dist/entities/context/chatContext.d.ts.map +1 -1
- package/dist/entities/context/chatContext.js +8 -10
- package/dist/entities/context/messageContext.d.ts +3 -3
- package/dist/entities/context/messageContext.d.ts.map +1 -1
- package/dist/entities/context/messageContext.js +14 -16
- package/dist/entities/incomingMessage.d.ts +1 -1
- package/dist/entities/incomingMessage.d.ts.map +1 -1
- package/dist/entities/incomingMessage.js +2 -1
- package/dist/entities/responses/imageMessage.d.ts +2 -2
- package/dist/entities/responses/imageMessage.d.ts.map +1 -1
- package/dist/entities/responses/imageMessage.js +2 -1
- package/dist/entities/responses/reaction.d.ts +1 -1
- package/dist/entities/responses/reaction.d.ts.map +1 -1
- package/dist/entities/responses/reaction.js +2 -1
- package/dist/entities/responses/textMessage.d.ts +2 -2
- package/dist/entities/responses/textMessage.d.ts.map +1 -1
- package/dist/entities/responses/textMessage.js +2 -1
- package/dist/entities/responses/videoMessage.d.ts +2 -2
- package/dist/entities/responses/videoMessage.d.ts.map +1 -1
- package/dist/entities/responses/videoMessage.js +2 -1
- package/dist/entities/states/actionStateBase.d.ts +2 -2
- package/dist/entities/states/actionStateBase.d.ts.map +1 -1
- package/dist/entities/states/actionStateBase.js +2 -1
- package/dist/entities/taskRecord.d.ts +1 -1
- package/dist/entities/taskRecord.d.ts.map +1 -1
- package/dist/entities/taskRecord.js +2 -1
- package/dist/helpers/builders/commandActionBuilder.d.ts +3 -3
- package/dist/helpers/builders/commandActionBuilder.d.ts.map +1 -1
- package/dist/helpers/builders/commandActionBuilder.js +9 -12
- package/dist/helpers/builders/scheduledActionBuilder.d.ts +3 -3
- package/dist/helpers/builders/scheduledActionBuilder.d.ts.map +1 -1
- package/dist/helpers/builders/scheduledActionBuilder.js +8 -10
- package/dist/helpers/noop.d.ts +1 -2
- package/dist/helpers/noop.d.ts.map +1 -1
- package/dist/helpers/noop.js +2 -1
- package/dist/helpers/toArray.d.ts +1 -1
- package/dist/helpers/toArray.d.ts.map +1 -1
- package/dist/helpers/toArray.js +1 -1
- package/dist/index.d.ts +7 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -15
- package/dist/main.d.ts +5 -5
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +6 -9
- package/dist/services/jsonFileStorage.d.ts +5 -5
- package/dist/services/jsonFileStorage.d.ts.map +1 -1
- package/dist/services/jsonFileStorage.js +2 -1
- package/dist/services/logger.d.ts +3 -3
- package/dist/services/logger.d.ts.map +1 -1
- package/dist/services/logger.js +3 -2
- package/dist/services/taskScheduler.d.ts +3 -3
- package/dist/services/taskScheduler.d.ts.map +1 -1
- package/dist/services/taskScheduler.js +8 -10
- package/dist/services/telegramApi.d.ts +6 -6
- package/dist/services/telegramApi.d.ts.map +1 -1
- package/dist/services/telegramApi.js +17 -19
- package/dist/types/actionState.d.ts +1 -1
- package/dist/types/actionState.d.ts.map +1 -1
- package/dist/types/actionWithState.d.ts +2 -2
- package/dist/types/actionWithState.d.ts.map +1 -1
- package/dist/types/commandCondition.d.ts +2 -2
- package/dist/types/commandCondition.d.ts.map +1 -1
- package/dist/types/handlers.d.ts +3 -3
- package/dist/types/handlers.d.ts.map +1 -1
- package/dist/types/replyMessage.d.ts +1 -1
- package/dist/types/replyMessage.d.ts.map +1 -1
- package/dist/types/storage.d.ts +4 -4
- package/dist/types/storage.d.ts.map +1 -1
- package/entities/{transactionResult.ts → actionExecutionResult.ts} +2 -2
- package/entities/actions/commandAction.ts +10 -10
- package/entities/actions/scheduledAction.ts +13 -13
- package/entities/{bot.ts → botInstance.ts} +17 -17
- package/entities/cachedStateFactory.ts +1 -1
- package/entities/commandTriggerCheckResult.ts +1 -1
- package/entities/context/chatContext.ts +4 -4
- package/entities/context/messageContext.ts +8 -8
- package/entities/incomingMessage.ts +1 -1
- package/entities/responses/imageMessage.ts +2 -2
- package/entities/responses/reaction.ts +1 -1
- package/entities/responses/textMessage.ts +2 -2
- package/entities/responses/videoMessage.ts +2 -2
- package/entities/states/actionStateBase.ts +2 -2
- package/entities/taskRecord.ts +1 -1
- package/helpers/builders/commandActionBuilder.ts +5 -5
- package/helpers/builders/scheduledActionBuilder.ts +4 -4
- package/helpers/noop.ts +1 -3
- package/helpers/toArray.ts +1 -1
- package/index.ts +7 -30
- package/main.ts +9 -9
- package/package.json +1 -1
- package/services/jsonFileStorage.ts +6 -6
- package/services/logger.ts +2 -2
- package/services/taskScheduler.ts +6 -6
- package/services/telegramApi.ts +15 -15
- package/types/actionState.ts +1 -1
- package/types/actionWithState.ts +2 -2
- package/types/commandCondition.ts +2 -2
- package/types/handlers.ts +3 -3
- package/types/replyMessage.ts +1 -1
- package/types/storage.ts +4 -5
- package/dist/entities/bot.d.ts.map +0 -1
- package/dist/entities/transactionResult.d.ts +0 -7
- package/dist/entities/transactionResult.d.ts.map +0 -1
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
|
|
7
|
-
const chatContext_1 =
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const logger_1 = __importDefault(require("./logger"));
|
|
3
|
+
exports.TelegramApiService = void 0;
|
|
4
|
+
const chatContext_1 = require("../entities/context/chatContext");
|
|
5
|
+
const messageContext_1 = require("../entities/context/messageContext");
|
|
6
|
+
const imageMessage_1 = require("../entities/responses/imageMessage");
|
|
7
|
+
const textMessage_1 = require("../entities/responses/textMessage");
|
|
8
|
+
const videoMessage_1 = require("../entities/responses/videoMessage");
|
|
13
9
|
const reverseMap_1 = require("../helpers/reverseMap");
|
|
10
|
+
const taskScheduler_1 = require("./taskScheduler");
|
|
11
|
+
const logger_1 = require("./logger");
|
|
14
12
|
class TelegramApiService {
|
|
15
13
|
constructor(botName, telegraf, storage, chats) {
|
|
16
14
|
this.messageQueue = [];
|
|
@@ -18,7 +16,7 @@ class TelegramApiService {
|
|
|
18
16
|
this.botName = botName;
|
|
19
17
|
this.chats = (0, reverseMap_1.reverseMap)(chats);
|
|
20
18
|
this.storage = storage;
|
|
21
|
-
taskScheduler_1.
|
|
19
|
+
taskScheduler_1.Scheduler.createTask('MessageSending', () => {
|
|
22
20
|
this.dequeueResponse();
|
|
23
21
|
}, 100, false, this.botName);
|
|
24
22
|
}
|
|
@@ -30,7 +28,7 @@ class TelegramApiService {
|
|
|
30
28
|
await this.processResponse(message);
|
|
31
29
|
}
|
|
32
30
|
catch (error) {
|
|
33
|
-
logger_1.
|
|
31
|
+
logger_1.Logger.errorWithTraceId(this.botName, message.traceId, this.chats.get(message.chatId), error, message);
|
|
34
32
|
}
|
|
35
33
|
}
|
|
36
34
|
async processResponse(response) {
|
|
@@ -44,7 +42,7 @@ class TelegramApiService {
|
|
|
44
42
|
return;
|
|
45
43
|
}
|
|
46
44
|
switch (response.constructor) {
|
|
47
|
-
case textMessage_1.
|
|
45
|
+
case textMessage_1.TextMessage:
|
|
48
46
|
await this.telegraf.telegram.sendMessage(response.chatId, response.content, {
|
|
49
47
|
reply_to_message_id: response.replyId,
|
|
50
48
|
parse_mode: 'MarkdownV2',
|
|
@@ -52,20 +50,20 @@ class TelegramApiService {
|
|
|
52
50
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
53
51
|
});
|
|
54
52
|
break;
|
|
55
|
-
case imageMessage_1.
|
|
53
|
+
case imageMessage_1.ImageMessage:
|
|
56
54
|
await this.telegraf.telegram.sendPhoto(response.chatId, response.content, response.replyId
|
|
57
55
|
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
58
56
|
{ reply_to_message_id: response.replyId }
|
|
59
57
|
: undefined);
|
|
60
58
|
break;
|
|
61
|
-
case videoMessage_1.
|
|
59
|
+
case videoMessage_1.VideoMessage:
|
|
62
60
|
await this.telegraf.telegram.sendVideo(response.chatId, response.content, response.replyId
|
|
63
61
|
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
64
62
|
{ reply_to_message_id: response.replyId }
|
|
65
63
|
: undefined);
|
|
66
64
|
break;
|
|
67
65
|
default:
|
|
68
|
-
logger_1.
|
|
66
|
+
logger_1.Logger.errorWithTraceId(this.botName, response.traceId, this.chats.get(response.chatId), `Unknown message type: ${response.constructor}`, response);
|
|
69
67
|
break;
|
|
70
68
|
}
|
|
71
69
|
}
|
|
@@ -86,10 +84,10 @@ class TelegramApiService {
|
|
|
86
84
|
const lastName = incomingMessage.from?.last_name
|
|
87
85
|
? ` ${incomingMessage.from?.last_name}`
|
|
88
86
|
: '';
|
|
89
|
-
return new messageContext_1.
|
|
87
|
+
return new messageContext_1.MessageContext(this.botName, this.getInteractions(), incomingMessage.chat.id, incomingMessage.chatName, incomingMessage.message_id, incomingMessage.text, incomingMessage.from?.id, incomingMessage.traceId, firstName + lastName, this.storage);
|
|
90
88
|
}
|
|
91
89
|
createContextForChat(chatId, scheduledName) {
|
|
92
|
-
return new chatContext_1.
|
|
90
|
+
return new chatContext_1.ChatContext(this.botName, this.getInteractions(), chatId, this.chats.get(chatId), `Scheduled:${scheduledName}:${chatId}`, this.storage);
|
|
93
91
|
}
|
|
94
92
|
}
|
|
95
|
-
exports.
|
|
93
|
+
exports.TelegramApiService = TelegramApiService;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"actionState.d.ts","sourceRoot":"","sources":["../../types/actionState.ts"],"names":[],"mappings":"AAAA,MAAM,
|
|
1
|
+
{"version":3,"file":"actionState.d.ts","sourceRoot":"","sources":["../../types/actionState.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,YAAY;IACzB,gBAAgB,EAAE,MAAM,CAAC;CAC5B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"actionWithState.d.ts","sourceRoot":"","sources":["../../types/actionWithState.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"actionWithState.d.ts","sourceRoot":"","sources":["../../types/actionWithState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,MAAM,WAAW,gBAAgB;IAC7B,GAAG,EAAE,MAAM,CAAC;IACZ,gBAAgB,EAAE,MAAM,YAAY,CAAC;CACxC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import MessageContext from '../entities/context/messageContext';
|
|
2
|
-
import IActionState from './actionState';
|
|
1
|
+
import { MessageContext } from '../entities/context/messageContext';
|
|
2
|
+
import { IActionState } from './actionState';
|
|
3
3
|
export type CommandCondition<TActionState extends IActionState> = (ctx: MessageContext<TActionState>) => Promise<boolean>;
|
|
4
4
|
//# sourceMappingURL=commandCondition.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commandCondition.d.ts","sourceRoot":"","sources":["../../types/commandCondition.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,oCAAoC,CAAC;
|
|
1
|
+
{"version":3,"file":"commandCondition.d.ts","sourceRoot":"","sources":["../../types/commandCondition.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,MAAM,MAAM,gBAAgB,CAAC,YAAY,SAAS,YAAY,IAAI,CAC9D,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC,KAChC,OAAO,CAAC,OAAO,CAAC,CAAC"}
|
package/dist/types/handlers.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import ChatContext from '../entities/context/chatContext';
|
|
2
|
-
import MessageContext from '../entities/context/messageContext';
|
|
3
|
-
import IActionState from './actionState';
|
|
1
|
+
import { ChatContext } from '../entities/context/chatContext';
|
|
2
|
+
import { MessageContext } from '../entities/context/messageContext';
|
|
3
|
+
import { IActionState } from './actionState';
|
|
4
4
|
import { CachedValueAccessor } from './cachedValueAccessor';
|
|
5
5
|
export type CommandHandler<TActionState extends IActionState> = (ctx: MessageContext<TActionState>, state: TActionState) => Promise<void>;
|
|
6
6
|
export type ScheduledHandler = (ctx: ChatContext, getCached: CachedValueAccessor) => Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handlers.d.ts","sourceRoot":"","sources":["../../types/handlers.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,MAAM,iCAAiC,CAAC;
|
|
1
|
+
{"version":3,"file":"handlers.d.ts","sourceRoot":"","sources":["../../types/handlers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D,MAAM,MAAM,cAAc,CAAC,YAAY,SAAS,YAAY,IAAI,CAC5D,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC,EACjC,KAAK,EAAE,YAAY,KAClB,OAAO,CAAC,IAAI,CAAC,CAAC;AAEnB,MAAM,MAAM,gBAAgB,GAAG,CAC3B,GAAG,EAAE,WAAW,EAChB,SAAS,EAAE,mBAAmB,KAC7B,OAAO,CAAC,IAAI,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"replyMessage.d.ts","sourceRoot":"","sources":["../../types/replyMessage.ts"],"names":[],"mappings":"AAAA,MAAM,
|
|
1
|
+
{"version":3,"file":"replyMessage.d.ts","sourceRoot":"","sources":["../../types/replyMessage.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa,CAAC,KAAK;IAChC,OAAO,EAAE,KAAK,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5B,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,iBAAiB,EAAE,OAAO,CAAC;CAC9B"}
|
package/dist/types/storage.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import IActionState from './actionState';
|
|
3
|
-
import IActionWithState from './actionWithState';
|
|
1
|
+
import { ActionExecutionResult } from '../entities/actionExecutionResult';
|
|
2
|
+
import { IActionState } from './actionState';
|
|
3
|
+
import { IActionWithState } from './actionWithState';
|
|
4
4
|
export interface IStorageClient {
|
|
5
5
|
close(): Promise<void>;
|
|
6
6
|
load<TActionState extends IActionState>(key: string): Promise<Record<number, TActionState>>;
|
|
7
7
|
saveMetadata(actions: IActionWithState[], botName: string): Promise<void>;
|
|
8
8
|
getActionState<TActionState extends IActionState>(entity: IActionWithState, chatId: number): Promise<TActionState>;
|
|
9
|
-
saveActionExecutionResult(action: IActionWithState, chatId: number, transactionResult:
|
|
9
|
+
saveActionExecutionResult(action: IActionWithState, chatId: number, transactionResult: ActionExecutionResult): Promise<void>;
|
|
10
10
|
}
|
|
11
11
|
//# sourceMappingURL=storage.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../types/storage.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../types/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAErD,MAAM,WAAW,cAAc;IAC3B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,IAAI,CAAC,YAAY,SAAS,YAAY,EAClC,GAAG,EAAE,MAAM,GACZ,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC;IACzC,YAAY,CAAC,OAAO,EAAE,gBAAgB,EAAE,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1E,cAAc,CAAC,YAAY,SAAS,YAAY,EAC5C,MAAM,EAAE,gBAAgB,EACxB,MAAM,EAAE,MAAM,GACf,OAAO,CAAC,YAAY,CAAC,CAAC;IACzB,yBAAyB,CACrB,MAAM,EAAE,gBAAgB,EACxB,MAAM,EAAE,MAAM,EACd,iBAAiB,EAAE,qBAAqB,GACzC,OAAO,CAAC,IAAI,CAAC,CAAC;CACpB"}
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import ActionExecutionResult from '../transactionResult';
|
|
2
1
|
import moment from 'moment';
|
|
3
|
-
import logger from '../../services/logger';
|
|
4
|
-
import MessageContext from '../context/messageContext';
|
|
5
|
-
import IActionWithState from '../../types/actionWithState';
|
|
6
|
-
import toArray from '../../helpers/toArray';
|
|
7
|
-
import IActionState from '../../types/actionState';
|
|
8
|
-
import CommandTriggerCheckResult from '../commandTriggerCheckResult';
|
|
9
2
|
import { CommandHandler } from '../../types/handlers';
|
|
10
3
|
import { CommandCondition } from '../../types/commandCondition';
|
|
11
4
|
import { Seconds } from '../../types/timeValues';
|
|
12
5
|
import { secondsToMilliseconds } from '../../helpers/timeConvertions';
|
|
13
|
-
|
|
14
|
-
|
|
6
|
+
import { toArray } from '../../helpers/toArray';
|
|
7
|
+
import { IActionState } from '../../types/actionState';
|
|
8
|
+
import { IActionWithState } from '../../types/actionWithState';
|
|
9
|
+
import { CommandTriggerCheckResult } from '../commandTriggerCheckResult';
|
|
10
|
+
import { MessageContext } from '../context/messageContext';
|
|
11
|
+
import { Logger } from '../../services/logger';
|
|
12
|
+
import { ActionExecutionResult } from '../actionExecutionResult';
|
|
13
|
+
|
|
14
|
+
export class CommandAction<TActionState extends IActionState>
|
|
15
15
|
implements IActionWithState
|
|
16
16
|
{
|
|
17
17
|
triggers: (string | RegExp)[];
|
|
@@ -70,7 +70,7 @@ export default class CommandAction<TActionState extends IActionState>
|
|
|
70
70
|
|
|
71
71
|
if (!shouldTrigger) return;
|
|
72
72
|
|
|
73
|
-
|
|
73
|
+
Logger.logWithTraceId(
|
|
74
74
|
ctx.botName,
|
|
75
75
|
ctx.traceId,
|
|
76
76
|
ctx.chatName,
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import TransactionResult from '../transactionResult';
|
|
2
1
|
import moment from 'moment';
|
|
3
|
-
import logger from '../../services/logger';
|
|
4
|
-
import taskScheduler from '../../services/taskScheduler';
|
|
5
2
|
import { Sema as Semaphore } from 'async-sema';
|
|
6
|
-
import ChatContext from '../context/chatContext';
|
|
7
|
-
import IActionWithState from '../../types/actionWithState';
|
|
8
|
-
import ActionStateBase from '../states/actionStateBase';
|
|
9
|
-
import IActionState from '../../types/actionState';
|
|
10
3
|
import { ScheduledHandler } from '../../types/handlers';
|
|
11
|
-
import CachedStateFactory from '../cachedStateFactory';
|
|
12
4
|
import { hoursToMilliseconds } from '../../helpers/timeConvertions';
|
|
13
5
|
import { HoursOfDay } from '../../types/timeValues';
|
|
14
|
-
|
|
15
|
-
|
|
6
|
+
import { IActionState } from '../../types/actionState';
|
|
7
|
+
import { IActionWithState } from '../../types/actionWithState';
|
|
8
|
+
import { CachedStateFactory } from '../cachedStateFactory';
|
|
9
|
+
import { ChatContext } from '../context/chatContext';
|
|
10
|
+
import { ActionStateBase } from '../states/actionStateBase';
|
|
11
|
+
import { ActionExecutionResult } from '../actionExecutionResult';
|
|
12
|
+
import { Logger } from '../../services/logger';
|
|
13
|
+
import { Scheduler } from '../../services/taskScheduler';
|
|
14
|
+
|
|
15
|
+
export class ScheduledAction implements IActionWithState {
|
|
16
16
|
static semaphore = new Semaphore(1);
|
|
17
17
|
|
|
18
18
|
name: string;
|
|
@@ -50,7 +50,7 @@ export default class ScheduledAction implements IActionWithState {
|
|
|
50
50
|
const isAllowedToTrigger = this.shouldTrigger(state);
|
|
51
51
|
|
|
52
52
|
if (isAllowedToTrigger) {
|
|
53
|
-
|
|
53
|
+
Logger.logWithTraceId(
|
|
54
54
|
ctx.botName,
|
|
55
55
|
ctx.traceId,
|
|
56
56
|
ctx.chatName,
|
|
@@ -66,7 +66,7 @@ export default class ScheduledAction implements IActionWithState {
|
|
|
66
66
|
await ctx.storage.saveActionExecutionResult(
|
|
67
67
|
this,
|
|
68
68
|
ctx.chatId,
|
|
69
|
-
new
|
|
69
|
+
new ActionExecutionResult(state, isAllowedToTrigger)
|
|
70
70
|
);
|
|
71
71
|
}
|
|
72
72
|
}
|
|
@@ -93,7 +93,7 @@ export default class ScheduledAction implements IActionWithState {
|
|
|
93
93
|
|
|
94
94
|
this.cachedState.set(key, value);
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
Scheduler.createOnetimeTask(
|
|
97
97
|
`Drop cached value [${this.name} : ${key}]`,
|
|
98
98
|
() => this.cachedState.delete(key),
|
|
99
99
|
hoursToMilliseconds(
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { Telegraf } from 'telegraf';
|
|
2
|
-
import TelegramApiService from '../services/telegramApi';
|
|
3
|
-
import IncomingMessage from './incomingMessage';
|
|
4
|
-
import taskScheduler from '../services/taskScheduler';
|
|
5
|
-
import logger from '../services/logger';
|
|
6
|
-
import CommandAction from './actions/commandAction';
|
|
7
|
-
import ScheduledAction from './actions/scheduledAction';
|
|
8
|
-
import IActionState from '../types/actionState';
|
|
9
2
|
import {
|
|
10
3
|
hoursToMilliseconds,
|
|
11
4
|
secondsToMilliseconds
|
|
12
5
|
} from '../helpers/timeConvertions';
|
|
13
6
|
import { Hours, Seconds } from '../types/timeValues';
|
|
14
|
-
import JsonFileStorage from '../services/jsonFileStorage';
|
|
15
7
|
import { IStorageClient } from '../types/storage';
|
|
16
|
-
|
|
17
|
-
|
|
8
|
+
import { JsonFileStorage } from '../services/jsonFileStorage';
|
|
9
|
+
import { TelegramApiService } from '../services/telegramApi';
|
|
10
|
+
import { IActionState } from '../types/actionState';
|
|
11
|
+
import { CommandAction } from './actions/commandAction';
|
|
12
|
+
import { ScheduledAction } from './actions/scheduledAction';
|
|
13
|
+
import { Logger } from '../services/logger';
|
|
14
|
+
import { Scheduler } from '../services/taskScheduler';
|
|
15
|
+
import { IncomingMessage } from './incomingMessage';
|
|
16
|
+
|
|
17
|
+
export class BotInstance {
|
|
18
18
|
name: string;
|
|
19
19
|
private api: TelegramApiService;
|
|
20
20
|
private telegraf: Telegraf;
|
|
@@ -38,7 +38,7 @@ export default class Bot {
|
|
|
38
38
|
this.scheduled = options.scheduled;
|
|
39
39
|
this.chats = options.chats;
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
Logger.logWithTraceId(
|
|
42
42
|
this.name,
|
|
43
43
|
`System:Bot-${this.name}-Start`,
|
|
44
44
|
'System',
|
|
@@ -61,7 +61,7 @@ export default class Bot {
|
|
|
61
61
|
|
|
62
62
|
const messageFromName = msg.from?.first_name ?? 'Unknown';
|
|
63
63
|
const messageFromId = msg.from?.id ?? 'Unknown';
|
|
64
|
-
|
|
64
|
+
Logger.logWithTraceId(
|
|
65
65
|
this.name,
|
|
66
66
|
msg.traceId,
|
|
67
67
|
msg.chatName,
|
|
@@ -75,7 +75,7 @@ export default class Bot {
|
|
|
75
75
|
|
|
76
76
|
this.telegraf.launch();
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
Scheduler.createTask(
|
|
79
79
|
'MessageProcessing',
|
|
80
80
|
async () => {
|
|
81
81
|
while (this.messageQueue.length > 0) {
|
|
@@ -87,7 +87,7 @@ export default class Bot {
|
|
|
87
87
|
this.name
|
|
88
88
|
);
|
|
89
89
|
|
|
90
|
-
|
|
90
|
+
Scheduler.createTask(
|
|
91
91
|
'ScheduledProcessing',
|
|
92
92
|
async () => {
|
|
93
93
|
await this.runScheduled();
|
|
@@ -104,7 +104,7 @@ export default class Bot {
|
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
async stop(code: string) {
|
|
107
|
-
|
|
107
|
+
Logger.logWithTraceId(
|
|
108
108
|
this.name,
|
|
109
109
|
`System:Bot-${this.name}-Stop`,
|
|
110
110
|
'System',
|
|
@@ -124,7 +124,7 @@ export default class Bot {
|
|
|
124
124
|
await trig.exec(ctx);
|
|
125
125
|
} catch (error) {
|
|
126
126
|
console.dir(error);
|
|
127
|
-
|
|
127
|
+
Logger.errorWithTraceId(
|
|
128
128
|
ctx.botName,
|
|
129
129
|
ctx.traceId,
|
|
130
130
|
chatName,
|
|
@@ -145,7 +145,7 @@ export default class Bot {
|
|
|
145
145
|
try {
|
|
146
146
|
await cmd.exec(ctx);
|
|
147
147
|
} catch (error) {
|
|
148
|
-
|
|
148
|
+
Logger.errorWithTraceId(
|
|
149
149
|
ctx.botName,
|
|
150
150
|
ctx.traceId,
|
|
151
151
|
ctx.chatName,
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import ImageMessage from '../responses/imageMessage';
|
|
2
|
-
import TextMessage from '../responses/textMessage';
|
|
3
|
-
import VideoMessage from '../responses/videoMessage';
|
|
4
1
|
import { resolve } from 'path';
|
|
5
2
|
import { IBotApiInteractions } from '../../services/telegramApi';
|
|
6
3
|
import { IStorageClient } from '../../types/storage';
|
|
4
|
+
import { ImageMessage } from '../responses/imageMessage';
|
|
5
|
+
import { TextMessage } from '../responses/textMessage';
|
|
6
|
+
import { VideoMessage } from '../responses/videoMessage';
|
|
7
7
|
|
|
8
|
-
export
|
|
8
|
+
export class ChatContext {
|
|
9
9
|
botName: string;
|
|
10
10
|
interactions: IBotApiInteractions;
|
|
11
11
|
chatId: number;
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import ActionStateBase from '../states/actionStateBase';
|
|
2
|
-
import ImageMessage from '../responses/imageMessage';
|
|
3
|
-
import TextMessage from '../responses/textMessage';
|
|
4
|
-
import VideoMessage from '../responses/videoMessage';
|
|
5
|
-
import ChatContext from './chatContext';
|
|
6
1
|
import { resolve } from 'path';
|
|
7
|
-
import IActionState from '../../types/actionState';
|
|
8
2
|
import { IBotApiInteractions } from '../../services/telegramApi';
|
|
9
3
|
import { TelegramEmoji } from 'telegraf/types';
|
|
10
|
-
import Reaction from '../responses/reaction';
|
|
11
4
|
import { IStorageClient } from '../../types/storage';
|
|
5
|
+
import { IActionState } from '../../types/actionState';
|
|
6
|
+
import { ImageMessage } from '../responses/imageMessage';
|
|
7
|
+
import { Reaction } from '../responses/reaction';
|
|
8
|
+
import { TextMessage } from '../responses/textMessage';
|
|
9
|
+
import { VideoMessage } from '../responses/videoMessage';
|
|
10
|
+
import { ActionStateBase } from '../states/actionStateBase';
|
|
11
|
+
import { ChatContext } from './chatContext';
|
|
12
12
|
|
|
13
|
-
export
|
|
13
|
+
export class MessageContext<
|
|
14
14
|
TActionState extends IActionState
|
|
15
15
|
> extends ChatContext {
|
|
16
16
|
messageId: number;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { InputFile } from 'telegraf/types';
|
|
2
|
-
import IReplyMessage from '../../types/replyMessage';
|
|
2
|
+
import { IReplyMessage } from '../../types/replyMessage';
|
|
3
3
|
|
|
4
|
-
export
|
|
4
|
+
export class ImageMessage implements IReplyMessage<InputFile> {
|
|
5
5
|
content: InputFile;
|
|
6
6
|
chatId: number;
|
|
7
7
|
replyId: number | undefined;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import IReplyMessage from '../../types/replyMessage';
|
|
1
|
+
import { IReplyMessage } from '../../types/replyMessage';
|
|
2
2
|
|
|
3
|
-
export
|
|
3
|
+
export class TextMessage implements IReplyMessage<string> {
|
|
4
4
|
content: string;
|
|
5
5
|
chatId: number;
|
|
6
6
|
replyId: number | undefined;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { InputFile } from 'telegraf/types';
|
|
2
|
-
import IReplyMessage from '../../types/replyMessage';
|
|
2
|
+
import { IReplyMessage } from '../../types/replyMessage';
|
|
3
3
|
|
|
4
|
-
export
|
|
4
|
+
export class VideoMessage implements IReplyMessage<InputFile> {
|
|
5
5
|
content: InputFile;
|
|
6
6
|
chatId: number;
|
|
7
7
|
replyId: number | undefined;
|
package/entities/taskRecord.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import CommandAction from '../../entities/actions/commandAction';
|
|
2
|
-
import ActionStateBase from '../../entities/states/actionStateBase';
|
|
3
|
-
import toArray from '../toArray';
|
|
4
|
-
import IActionState from '../../types/actionState';
|
|
5
1
|
import { CommandHandler } from '../../types/handlers';
|
|
6
2
|
import { CommandCondition } from '../../types/commandCondition';
|
|
7
3
|
import { Seconds } from '../../types/timeValues';
|
|
8
|
-
import
|
|
4
|
+
import { CommandAction } from '../../entities/actions/commandAction';
|
|
5
|
+
import { ActionStateBase } from '../../entities/states/actionStateBase';
|
|
6
|
+
import { IActionState } from '../../types/actionState';
|
|
7
|
+
import { toArray } from '../toArray';
|
|
8
|
+
import { Noop } from '../noop';
|
|
9
9
|
|
|
10
10
|
export class CommandActionBuilderWithState<TActionState extends IActionState> {
|
|
11
11
|
name: string;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import ScheduledAction from '../../entities/actions/scheduledAction';
|
|
2
|
-
import CachedStateFactory from '../../entities/cachedStateFactory';
|
|
1
|
+
import { ScheduledAction } from '../../entities/actions/scheduledAction';
|
|
2
|
+
import { CachedStateFactory } from '../../entities/cachedStateFactory';
|
|
3
3
|
import { ScheduledHandler } from '../../types/handlers';
|
|
4
4
|
import { Hours, HoursOfDay } from '../../types/timeValues';
|
|
5
|
-
import Noop from '../noop';
|
|
5
|
+
import { Noop } from '../noop';
|
|
6
6
|
|
|
7
|
-
export
|
|
7
|
+
export class ScheduledActionBuilder {
|
|
8
8
|
active = true;
|
|
9
9
|
time: HoursOfDay = 0;
|
|
10
10
|
cachedStateFactories = new Map<string, CachedStateFactory>();
|
package/helpers/noop.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
-
class Noop {
|
|
2
|
+
export class Noop {
|
|
3
3
|
static async true<T1>(arg1: T1) {
|
|
4
4
|
return true;
|
|
5
5
|
}
|
|
@@ -9,5 +9,3 @@ class Noop {
|
|
|
9
9
|
static async call<T1, T2>(arg1: T1, arg2: T2): Promise<void>;
|
|
10
10
|
static async call<T1>(arg1: T1) {}
|
|
11
11
|
}
|
|
12
|
-
|
|
13
|
-
export default Noop;
|
package/helpers/toArray.ts
CHANGED
package/index.ts
CHANGED
|
@@ -1,31 +1,8 @@
|
|
|
1
1
|
export { startBot, stopBots } from './main';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} from './helpers/timeConvertions';
|
|
10
|
-
import { IStorageClient } from './types/storage';
|
|
11
|
-
import IActionState from './types/actionState';
|
|
12
|
-
import ActionStateBase from './entities/states/actionStateBase';
|
|
13
|
-
import { Hours, Milliseconds, Seconds } from './types/timeValues';
|
|
14
|
-
|
|
15
|
-
export const helpers = {
|
|
16
|
-
hoursToMilliseconds,
|
|
17
|
-
hoursToSeconds,
|
|
18
|
-
secondsToMilliseconds
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export {
|
|
22
|
-
CommandActionBuilder,
|
|
23
|
-
CommandActionBuilderWithState,
|
|
24
|
-
ScheduledActionBuilder,
|
|
25
|
-
IStorageClient,
|
|
26
|
-
IActionState,
|
|
27
|
-
ActionStateBase,
|
|
28
|
-
Milliseconds,
|
|
29
|
-
Seconds,
|
|
30
|
-
Hours
|
|
31
|
-
};
|
|
2
|
+
export { CommandActionBuilder } from './helpers/builders/commandActionBuilder';
|
|
3
|
+
export { CommandActionBuilderWithState } from './helpers/builders/commandActionBuilder';
|
|
4
|
+
export * from './helpers/builders/scheduledActionBuilder';
|
|
5
|
+
export { IStorageClient } from './types/storage';
|
|
6
|
+
export * from './types/actionState';
|
|
7
|
+
export * from './entities/states/actionStateBase';
|
|
8
|
+
export { Hours, Milliseconds, Seconds } from './types/timeValues';
|