chz-telegram-bot 0.0.15 → 0.0.17
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} +20 -22
- 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 +7 -5
- package/dist/entities/context/chatContext.d.ts.map +1 -1
- package/dist/entities/context/chatContext.js +17 -14
- package/dist/entities/context/messageContext.d.ts +7 -7
- package/dist/entities/context/messageContext.d.ts.map +1 -1
- package/dist/entities/context/messageContext.js +19 -21
- 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 +6 -3
- package/dist/entities/responses/imageMessage.d.ts.map +1 -1
- package/dist/entities/responses/imageMessage.js +7 -2
- package/dist/entities/responses/reaction.d.ts +5 -2
- package/dist/entities/responses/reaction.d.ts.map +1 -1
- package/dist/entities/responses/reaction.js +6 -2
- package/dist/entities/responses/textMessage.d.ts +6 -3
- package/dist/entities/responses/textMessage.d.ts.map +1 -1
- package/dist/entities/responses/textMessage.js +7 -2
- package/dist/entities/responses/unpin.d.ts +10 -0
- package/dist/entities/responses/unpin.d.ts.map +1 -0
- package/dist/entities/responses/unpin.js +14 -0
- package/dist/entities/responses/videoMessage.d.ts +6 -3
- package/dist/entities/responses/videoMessage.d.ts.map +1 -1
- package/dist/entities/responses/videoMessage.js +7 -2
- package/dist/entities/states/actionStateBase.d.ts +3 -2
- package/dist/entities/states/actionStateBase.d.ts.map +1 -1
- package/dist/entities/states/actionStateBase.js +3 -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/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 +6 -5
- package/dist/services/jsonFileStorage.d.ts.map +1 -1
- package/dist/services/jsonFileStorage.js +10 -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 +20 -15
- package/dist/services/telegramApi.d.ts.map +1 -1
- package/dist/services/telegramApi.js +50 -43
- package/dist/types/actionState.d.ts +2 -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/response.d.ts +26 -0
- package/dist/types/response.d.ts.map +1 -0
- package/dist/types/response.js +10 -0
- package/dist/types/storage.d.ts +5 -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} +20 -20
- package/entities/cachedStateFactory.ts +1 -1
- package/entities/commandTriggerCheckResult.ts +1 -1
- package/entities/context/chatContext.ts +35 -10
- package/entities/context/messageContext.ts +37 -16
- package/entities/incomingMessage.ts +1 -1
- package/entities/responses/imageMessage.ts +11 -3
- package/entities/responses/reaction.ts +8 -2
- package/entities/responses/textMessage.ts +11 -3
- package/entities/responses/unpin.ts +22 -0
- package/entities/responses/videoMessage.ts +11 -3
- package/entities/states/actionStateBase.ts +3 -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/main.ts +9 -9
- package/package.json +1 -1
- package/services/jsonFileStorage.ts +21 -6
- package/services/logger.ts +2 -2
- package/services/taskScheduler.ts +6 -6
- package/services/telegramApi.ts +95 -59
- package/types/actionState.ts +2 -1
- package/types/actionWithState.ts +2 -2
- package/types/commandCondition.ts +2 -2
- package/types/handlers.ts +3 -3
- package/types/response.ts +34 -0
- package/types/storage.ts +9 -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
- package/types/replyMessage.ts +0 -7
|
@@ -1,11 +1,9 @@
|
|
|
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
|
-
|
|
3
|
+
exports.Scheduler = void 0;
|
|
4
|
+
const taskRecord_1 = require("../entities/taskRecord");
|
|
7
5
|
const timeConvertions_1 = require("../helpers/timeConvertions");
|
|
8
|
-
const logger_1 =
|
|
6
|
+
const logger_1 = require("./logger");
|
|
9
7
|
class TaskScheduler {
|
|
10
8
|
constructor() {
|
|
11
9
|
this.activeTasks = [];
|
|
@@ -18,20 +16,20 @@ class TaskScheduler {
|
|
|
18
16
|
createTask(name, action, interval, executeRightAway, ownerName) {
|
|
19
17
|
executeRightAway = executeRightAway ?? false;
|
|
20
18
|
const taskId = setInterval(action, interval);
|
|
21
|
-
const task = new taskRecord_1.
|
|
19
|
+
const task = new taskRecord_1.TaskRecord(name, taskId, interval);
|
|
22
20
|
if (executeRightAway) {
|
|
23
21
|
setTimeout(action, (0, timeConvertions_1.secondsToMilliseconds)(1));
|
|
24
22
|
}
|
|
25
|
-
logger_1.
|
|
23
|
+
logger_1.Logger.logWithTraceId(ownerName, `System:TaskScheduler-${ownerName}-${name}`, 'System', `Created task [${taskId}]${name}, that will run every ${interval}ms.`);
|
|
26
24
|
this.activeTasks.push(task);
|
|
27
25
|
}
|
|
28
26
|
createOnetimeTask(name, action, delay, ownerName) {
|
|
29
27
|
const actionWrapper = () => {
|
|
30
|
-
logger_1.
|
|
28
|
+
logger_1.Logger.logWithTraceId(ownerName, `System:TaskScheduler-${ownerName}-${name}`, 'System', `Executing delayed oneshot [${taskId}]${name}`);
|
|
31
29
|
action();
|
|
32
30
|
};
|
|
33
31
|
const taskId = setTimeout(actionWrapper, delay);
|
|
34
|
-
logger_1.
|
|
32
|
+
logger_1.Logger.logWithTraceId(ownerName, `System:TaskScheduler-${ownerName}-${name}`, 'System', `Created oneshot task [${taskId}]${name}, that will run in ${delay}ms.`);
|
|
35
33
|
}
|
|
36
34
|
}
|
|
37
|
-
exports.
|
|
35
|
+
exports.Scheduler = new TaskScheduler();
|
|
@@ -1,27 +1,32 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import { Telegraf } from 'telegraf';
|
|
4
|
-
import IReplyMessage from '../types/replyMessage';
|
|
5
|
-
import IncomingMessage from '../entities/incomingMessage';
|
|
6
|
-
import Reaction from '../entities/responses/reaction';
|
|
1
|
+
import { ChatContext } from '../entities/context/chatContext';
|
|
2
|
+
import { MessageContext } from '../entities/context/messageContext';
|
|
7
3
|
import { IStorageClient } from '../types/storage';
|
|
8
|
-
|
|
4
|
+
import { Reaction } from '../entities/responses/reaction';
|
|
5
|
+
import { IncomingMessage } from '../entities/incomingMessage';
|
|
6
|
+
import { BotResponse } from '../types/response';
|
|
7
|
+
import { UnpinResponse } from '../entities/responses/unpin';
|
|
8
|
+
import { TextMessage } from '../entities/responses/textMessage';
|
|
9
|
+
import { VideoMessage } from '../entities/responses/videoMessage';
|
|
10
|
+
import { ImageMessage } from '../entities/responses/imageMessage';
|
|
11
|
+
import { Telegram } from 'telegraf/typings/telegram';
|
|
12
|
+
export declare class TelegramApiService {
|
|
9
13
|
botName: string;
|
|
10
|
-
|
|
14
|
+
telegram: Telegram;
|
|
11
15
|
chats: Map<number, string>;
|
|
12
|
-
messageQueue: Array<
|
|
16
|
+
messageQueue: Array<BotResponse>;
|
|
13
17
|
storage: IStorageClient;
|
|
14
|
-
constructor(botName: string,
|
|
18
|
+
constructor(botName: string, telegram: Telegram, storage: IStorageClient, chats: Map<string, number>);
|
|
15
19
|
private dequeueResponse;
|
|
20
|
+
private pinIfShould;
|
|
16
21
|
private processResponse;
|
|
17
|
-
private
|
|
18
|
-
private enqueueReaction;
|
|
22
|
+
private enqueue;
|
|
19
23
|
private getInteractions;
|
|
20
|
-
createContextForMessage(incomingMessage: IncomingMessage): MessageContext<import("
|
|
21
|
-
createContextForChat(chatId: number,
|
|
24
|
+
createContextForMessage(incomingMessage: IncomingMessage, commandKey: string): MessageContext<import("..").IActionState>;
|
|
25
|
+
createContextForChat(chatId: number, scheduledKey: string): ChatContext;
|
|
22
26
|
}
|
|
23
27
|
export interface IBotApiInteractions {
|
|
24
|
-
respond:
|
|
28
|
+
respond: (response: TextMessage | VideoMessage | ImageMessage) => void;
|
|
25
29
|
react: (reaction: Reaction) => void;
|
|
30
|
+
unpin: (unpinMessage: UnpinResponse) => void;
|
|
26
31
|
}
|
|
27
32
|
//# sourceMappingURL=telegramApi.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"telegramApi.d.ts","sourceRoot":"","sources":["../../services/telegramApi.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"telegramApi.d.ts","sourceRoot":"","sources":["../../services/telegramApi.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AAEpE,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAIlD,OAAO,EAAE,QAAQ,EAAE,MAAM,gCAAgC,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAiB,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,MAAM,mCAAmC,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AAClE,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAErD,qBAAa,kBAAkB;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,QAAQ,CAAC;IACnB,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC3B,YAAY,EAAE,KAAK,CAAC,WAAW,CAAC,CAAM;IACtC,OAAO,EAAE,cAAc,CAAC;gBAGpB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,cAAc,EACvB,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC;YAkBhB,eAAe;YAkBf,WAAW;YAqBX,eAAe;IA2E7B,OAAO,CAAC,OAAO;IAIf,OAAO,CAAC,eAAe;IAQvB,uBAAuB,CACnB,eAAe,EAAE,eAAe,EAChC,UAAU,EAAE,MAAM;IAsBtB,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM;CAW5D;AAED,MAAM,WAAW,mBAAmB;IAChC,OAAO,EAAE,CAAC,QAAQ,EAAE,WAAW,GAAG,YAAY,GAAG,YAAY,KAAK,IAAI,CAAC;IACvE,KAAK,EAAE,CAAC,QAAQ,EAAE,QAAQ,KAAK,IAAI,CAAC;IACpC,KAAK,EAAE,CAAC,YAAY,EAAE,aAAa,KAAK,IAAI,CAAC;CAChD"}
|
|
@@ -1,24 +1,19 @@
|
|
|
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 textMessage_1 = __importDefault(require("../entities/responses/textMessage"));
|
|
10
|
-
const videoMessage_1 = __importDefault(require("../entities/responses/videoMessage"));
|
|
11
|
-
const taskScheduler_1 = __importDefault(require("./taskScheduler"));
|
|
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");
|
|
13
6
|
const reverseMap_1 = require("../helpers/reverseMap");
|
|
7
|
+
const taskScheduler_1 = require("./taskScheduler");
|
|
8
|
+
const logger_1 = require("./logger");
|
|
14
9
|
class TelegramApiService {
|
|
15
|
-
constructor(botName,
|
|
10
|
+
constructor(botName, telegram, storage, chats) {
|
|
16
11
|
this.messageQueue = [];
|
|
17
|
-
this.
|
|
12
|
+
this.telegram = telegram;
|
|
18
13
|
this.botName = botName;
|
|
19
14
|
this.chats = (0, reverseMap_1.reverseMap)(chats);
|
|
20
15
|
this.storage = storage;
|
|
21
|
-
taskScheduler_1.
|
|
16
|
+
taskScheduler_1.Scheduler.createTask('MessageSending', () => {
|
|
22
17
|
this.dequeueResponse();
|
|
23
18
|
}, 100, false, this.botName);
|
|
24
19
|
}
|
|
@@ -30,66 +25,78 @@ class TelegramApiService {
|
|
|
30
25
|
await this.processResponse(message);
|
|
31
26
|
}
|
|
32
27
|
catch (error) {
|
|
33
|
-
logger_1.
|
|
28
|
+
logger_1.Logger.errorWithTraceId(this.botName, message.traceId, this.chats.get(message.chatId), error, message);
|
|
34
29
|
}
|
|
35
30
|
}
|
|
36
|
-
async
|
|
37
|
-
if (
|
|
38
|
-
this.
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
], true);
|
|
44
|
-
return;
|
|
31
|
+
async pinIfShould(response, sentMessage) {
|
|
32
|
+
if (response.shouldPin) {
|
|
33
|
+
await this.telegram.pinChatMessage(response.chatId, sentMessage.message_id, { disable_notification: true });
|
|
34
|
+
await this.storage.updateStateFor(response.sourceActionKey, response.chatId, async (state) => {
|
|
35
|
+
state.pinnedMessages.push(sentMessage.message_id);
|
|
36
|
+
});
|
|
45
37
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
38
|
+
}
|
|
39
|
+
async processResponse(response) {
|
|
40
|
+
let sentMessage;
|
|
41
|
+
switch (response.kind) {
|
|
42
|
+
case 'text':
|
|
43
|
+
sentMessage = await this.telegram.sendMessage(response.chatId, response.content, {
|
|
49
44
|
reply_to_message_id: response.replyId,
|
|
50
45
|
parse_mode: 'MarkdownV2',
|
|
51
46
|
disable_web_page_preview: response.disableWebPreview
|
|
52
47
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
53
48
|
});
|
|
49
|
+
await this.pinIfShould(response, sentMessage);
|
|
54
50
|
break;
|
|
55
|
-
case
|
|
56
|
-
await this.
|
|
51
|
+
case 'image':
|
|
52
|
+
sentMessage = await this.telegram.sendPhoto(response.chatId, response.content, response.replyId
|
|
57
53
|
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
58
54
|
{ reply_to_message_id: response.replyId }
|
|
59
55
|
: undefined);
|
|
56
|
+
await this.pinIfShould(response, sentMessage);
|
|
60
57
|
break;
|
|
61
|
-
case
|
|
62
|
-
await this.
|
|
58
|
+
case 'video':
|
|
59
|
+
sentMessage = await this.telegram.sendVideo(response.chatId, response.content, response.replyId
|
|
63
60
|
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
64
61
|
{ reply_to_message_id: response.replyId }
|
|
65
62
|
: undefined);
|
|
63
|
+
await this.pinIfShould(response, sentMessage);
|
|
66
64
|
break;
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
case 'react':
|
|
66
|
+
await this.telegram.setMessageReaction(response.chatId, response.messageId, [
|
|
67
|
+
{
|
|
68
|
+
type: 'emoji',
|
|
69
|
+
emoji: response.emoji
|
|
70
|
+
}
|
|
71
|
+
], true);
|
|
72
|
+
return;
|
|
73
|
+
case 'unpin':
|
|
74
|
+
await this.telegram.unpinChatMessage(response.chatId, response.messageId);
|
|
75
|
+
await this.storage.updateStateFor(response.sourceActionKey, response.chatId, async (state) => {
|
|
76
|
+
state.pinnedMessages = state.pinnedMessages.filter((x) => x != response.messageId);
|
|
77
|
+
});
|
|
69
78
|
break;
|
|
70
79
|
}
|
|
71
80
|
}
|
|
72
|
-
|
|
73
|
-
this.messageQueue.push(response);
|
|
74
|
-
}
|
|
75
|
-
enqueueReaction(reaction) {
|
|
81
|
+
enqueue(reaction) {
|
|
76
82
|
this.messageQueue.push(reaction);
|
|
77
83
|
}
|
|
78
84
|
getInteractions() {
|
|
79
85
|
return {
|
|
80
|
-
react: (reaction) => this.
|
|
81
|
-
respond: (response) => this.
|
|
86
|
+
react: (reaction) => this.enqueue(reaction),
|
|
87
|
+
respond: (response) => this.enqueue(response),
|
|
88
|
+
unpin: (unpinMessage) => this.enqueue(unpinMessage)
|
|
82
89
|
};
|
|
83
90
|
}
|
|
84
|
-
createContextForMessage(incomingMessage) {
|
|
91
|
+
createContextForMessage(incomingMessage, commandKey) {
|
|
85
92
|
const firstName = incomingMessage.from?.first_name ?? 'Unknown user';
|
|
86
93
|
const lastName = incomingMessage.from?.last_name
|
|
87
94
|
? ` ${incomingMessage.from?.last_name}`
|
|
88
95
|
: '';
|
|
89
|
-
return new messageContext_1.
|
|
96
|
+
return new messageContext_1.MessageContext(this.botName, commandKey, this.getInteractions(), incomingMessage.chat.id, incomingMessage.chatName, incomingMessage.message_id, incomingMessage.text, incomingMessage.from?.id, incomingMessage.traceId, firstName + lastName, this.storage);
|
|
90
97
|
}
|
|
91
|
-
createContextForChat(chatId,
|
|
92
|
-
return new chatContext_1.
|
|
98
|
+
createContextForChat(chatId, scheduledKey) {
|
|
99
|
+
return new chatContext_1.ChatContext(this.botName, scheduledKey, this.getInteractions(), chatId, this.chats.get(chatId), `Scheduled:${scheduledKey}:${chatId}`, this.storage);
|
|
93
100
|
}
|
|
94
101
|
}
|
|
95
|
-
exports.
|
|
102
|
+
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;IACzB,cAAc,EAAE,MAAM,EAAE,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"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ImageMessage } from '../entities/responses/imageMessage';
|
|
2
|
+
import { Reaction } from '../entities/responses/reaction';
|
|
3
|
+
import { TextMessage } from '../entities/responses/textMessage';
|
|
4
|
+
import { UnpinResponse } from '../entities/responses/unpin';
|
|
5
|
+
import { VideoMessage } from '../entities/responses/videoMessage';
|
|
6
|
+
export declare const BotResponseTypes: {
|
|
7
|
+
readonly unpin: "unpin";
|
|
8
|
+
readonly text: "text";
|
|
9
|
+
readonly image: "image";
|
|
10
|
+
readonly video: "video";
|
|
11
|
+
readonly react: "react";
|
|
12
|
+
};
|
|
13
|
+
export type BotResponse = UnpinResponse | Reaction | TextMessage | VideoMessage | ImageMessage;
|
|
14
|
+
export interface IChatResponse {
|
|
15
|
+
kind: keyof typeof BotResponseTypes;
|
|
16
|
+
chatId: number;
|
|
17
|
+
traceId: number | string;
|
|
18
|
+
sourceActionKey: string;
|
|
19
|
+
}
|
|
20
|
+
export interface IReplyMessage<TType> extends IChatResponse {
|
|
21
|
+
content: TType;
|
|
22
|
+
replyId: number | undefined;
|
|
23
|
+
disableWebPreview: boolean;
|
|
24
|
+
shouldPin: boolean;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=response.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response.d.ts","sourceRoot":"","sources":["../../types/response.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AAClE,OAAO,EAAE,QAAQ,EAAE,MAAM,gCAAgC,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,mCAAmC,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AAElE,eAAO,MAAM,gBAAgB;;;;;;CAMnB,CAAC;AAEX,MAAM,MAAM,WAAW,GACjB,aAAa,GACb,QAAQ,GACR,WAAW,GACX,YAAY,GACZ,YAAY,CAAC;AAEnB,MAAM,WAAW,aAAa;IAC1B,IAAI,EAAE,MAAM,OAAO,gBAAgB,CAAC;IACpC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,eAAe,EAAE,MAAM,CAAC;CAC3B;AAED,MAAM,WAAW,aAAa,CAAC,KAAK,CAAE,SAAQ,aAAa;IACvD,OAAO,EAAE,KAAK,CAAC;IACf,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5B,iBAAiB,EAAE,OAAO,CAAC;IAC3B,SAAS,EAAE,OAAO,CAAC;CACtB"}
|
package/dist/types/storage.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
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
|
+
updateStateFor<TActionState extends IActionState>(sourceActionKey: string, chatId: number, update: (state: TActionState) => Promise<void>): Promise<void>;
|
|
5
6
|
close(): Promise<void>;
|
|
6
7
|
load<TActionState extends IActionState>(key: string): Promise<Record<number, TActionState>>;
|
|
7
8
|
saveMetadata(actions: IActionWithState[], botName: string): Promise<void>;
|
|
8
9
|
getActionState<TActionState extends IActionState>(entity: IActionWithState, chatId: number): Promise<TActionState>;
|
|
9
|
-
saveActionExecutionResult(action: IActionWithState, chatId: number, transactionResult:
|
|
10
|
+
saveActionExecutionResult(action: IActionWithState, chatId: number, transactionResult: ActionExecutionResult): Promise<void>;
|
|
10
11
|
}
|
|
11
12
|
//# 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,cAAc,CAAC,YAAY,SAAS,YAAY,EAC5C,eAAe,EAAE,MAAM,EACvB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,GAC/C,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,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',
|
|
@@ -50,7 +50,7 @@ export default class Bot {
|
|
|
50
50
|
new JsonFileStorage(options.name, options.storagePath);
|
|
51
51
|
this.api = new TelegramApiService(
|
|
52
52
|
this.name,
|
|
53
|
-
this.telegraf,
|
|
53
|
+
this.telegraf.telegram,
|
|
54
54
|
this.storage,
|
|
55
55
|
this.chats
|
|
56
56
|
);
|
|
@@ -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',
|
|
@@ -118,13 +118,13 @@ export default class Bot {
|
|
|
118
118
|
private async runScheduled() {
|
|
119
119
|
for (const [chatName, chatId] of this.chats.entries()) {
|
|
120
120
|
for (const trig of this.scheduled) {
|
|
121
|
-
const ctx = this.api.createContextForChat(chatId, trig.
|
|
121
|
+
const ctx = this.api.createContextForChat(chatId, trig.key);
|
|
122
122
|
|
|
123
123
|
try {
|
|
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,
|
|
@@ -140,12 +140,12 @@ export default class Bot {
|
|
|
140
140
|
const msg = this.messageQueue.pop()!;
|
|
141
141
|
|
|
142
142
|
for (const cmd of this.commands) {
|
|
143
|
-
const ctx = this.api.createContextForMessage(msg);
|
|
143
|
+
const ctx = this.api.createContextForMessage(msg, cmd.key);
|
|
144
144
|
|
|
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,
|