chz-telegram-bot 0.0.15 → 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/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 +7 -7
- 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/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 +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/main.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { readFile } from 'fs/promises';
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import IActionState from './types/actionState
|
|
7
|
-
import
|
|
8
|
-
import {
|
|
2
|
+
import { IStorageClient } from './types/storage';
|
|
3
|
+
import { Logger } from './services/logger';
|
|
4
|
+
import { CommandAction } from './entities/actions/commandAction';
|
|
5
|
+
import { ScheduledAction } from './entities/actions/scheduledAction';
|
|
6
|
+
import { IActionState } from './types/actionState';
|
|
7
|
+
import { Scheduler } from './services/taskScheduler';
|
|
8
|
+
import { BotInstance } from './entities/botInstance';
|
|
9
9
|
|
|
10
10
|
const bots: BotInstance[] = [];
|
|
11
11
|
|
|
12
12
|
function log(text: string) {
|
|
13
|
-
|
|
13
|
+
Logger.logWithTraceId('ALL BOTS', 'System:Bot', 'System', text);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
async function startBot(options: {
|
|
@@ -39,7 +39,7 @@ async function startBot(options: {
|
|
|
39
39
|
|
|
40
40
|
async function stopBots(reason: string) {
|
|
41
41
|
log(`Recieved termination code: ${reason}`);
|
|
42
|
-
|
|
42
|
+
Scheduler.stopAll();
|
|
43
43
|
log('Acquiring storage semaphore...');
|
|
44
44
|
|
|
45
45
|
log('Stopping bots...');
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { existsSync, mkdirSync } from 'fs';
|
|
2
2
|
import { dirname } from 'path';
|
|
3
|
-
import TransactionResult from '../entities/transactionResult';
|
|
4
3
|
import { mkdir, readFile, writeFile } from 'fs/promises';
|
|
5
|
-
import ActionStateBase from '../entities/states/actionStateBase';
|
|
6
|
-
import IActionState from '../types/actionState';
|
|
7
|
-
import IActionWithState from '../types/actionWithState';
|
|
8
4
|
import { Sema as Semaphore } from 'async-sema';
|
|
9
5
|
import { IStorageClient } from '../types/storage';
|
|
6
|
+
import { ActionStateBase } from '../entities/states/actionStateBase';
|
|
7
|
+
import { ActionExecutionResult } from '../entities/actionExecutionResult';
|
|
8
|
+
import { IActionState } from '../types/actionState';
|
|
9
|
+
import { IActionWithState } from '../types/actionWithState';
|
|
10
10
|
|
|
11
|
-
export
|
|
11
|
+
export class JsonFileStorage implements IStorageClient {
|
|
12
12
|
semaphore = new Semaphore(1);
|
|
13
13
|
private cache: Map<string, Record<number, IActionState>>;
|
|
14
14
|
private storagePath: string;
|
|
@@ -105,7 +105,7 @@ export default class JsonFileStorage implements IStorageClient {
|
|
|
105
105
|
async saveActionExecutionResult(
|
|
106
106
|
action: IActionWithState,
|
|
107
107
|
chatId: number,
|
|
108
|
-
transactionResult:
|
|
108
|
+
transactionResult: ActionExecutionResult
|
|
109
109
|
) {
|
|
110
110
|
await this.lock(async () => {
|
|
111
111
|
const data = await this.loadInternal(action.key);
|
package/services/logger.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import TaskRecord from '../entities/taskRecord';
|
|
1
|
+
import { TaskRecord } from '../entities/taskRecord';
|
|
2
2
|
import { secondsToMilliseconds } from '../helpers/timeConvertions';
|
|
3
3
|
import { Milliseconds, Seconds } from '../types/timeValues';
|
|
4
|
-
import
|
|
4
|
+
import { Logger } from './logger';
|
|
5
5
|
|
|
6
6
|
class TaskScheduler {
|
|
7
7
|
activeTasks: TaskRecord[] = [];
|
|
@@ -27,7 +27,7 @@ class TaskScheduler {
|
|
|
27
27
|
setTimeout(action, secondsToMilliseconds(1 as Seconds));
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
Logger.logWithTraceId(
|
|
31
31
|
ownerName,
|
|
32
32
|
`System:TaskScheduler-${ownerName}-${name}`,
|
|
33
33
|
'System',
|
|
@@ -44,7 +44,7 @@ class TaskScheduler {
|
|
|
44
44
|
ownerName: string
|
|
45
45
|
) {
|
|
46
46
|
const actionWrapper = () => {
|
|
47
|
-
|
|
47
|
+
Logger.logWithTraceId(
|
|
48
48
|
ownerName,
|
|
49
49
|
`System:TaskScheduler-${ownerName}-${name}`,
|
|
50
50
|
'System',
|
|
@@ -54,7 +54,7 @@ class TaskScheduler {
|
|
|
54
54
|
};
|
|
55
55
|
const taskId = setTimeout(actionWrapper, delay);
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
Logger.logWithTraceId(
|
|
58
58
|
ownerName,
|
|
59
59
|
`System:TaskScheduler-${ownerName}-${name}`,
|
|
60
60
|
'System',
|
|
@@ -63,4 +63,4 @@ class TaskScheduler {
|
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
export
|
|
66
|
+
export const Scheduler = new TaskScheduler();
|