chz-telegram-bot 0.0.45 → 0.0.47
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/actionExecutionResult.js +10 -0
- package/dist/entities/actions/commandAction.d.ts +23 -0
- package/dist/entities/actions/commandAction.d.ts.map +1 -0
- package/dist/entities/actions/commandAction.js +91 -0
- package/dist/entities/actions/scheduledAction.d.ts +24 -0
- package/dist/entities/actions/scheduledAction.d.ts.map +1 -0
- package/dist/entities/actions/scheduledAction.js +81 -0
- package/dist/entities/botInstance.d.ts +31 -0
- package/dist/entities/botInstance.d.ts.map +1 -0
- package/dist/entities/botInstance.js +107 -0
- package/dist/entities/cachedStateFactory.d.ts +7 -0
- package/dist/entities/cachedStateFactory.d.ts.map +1 -0
- package/dist/entities/cachedStateFactory.js +10 -0
- package/dist/entities/commandTriggerCheckResult.d.ts +10 -0
- package/dist/entities/commandTriggerCheckResult.d.ts.map +1 -0
- package/dist/entities/commandTriggerCheckResult.js +23 -0
- package/dist/entities/context/chatContext.d.ts +55 -0
- package/dist/entities/context/chatContext.d.ts.map +1 -0
- package/dist/entities/context/chatContext.js +70 -0
- package/dist/entities/context/messageContext.d.ts +60 -0
- package/dist/entities/context/messageContext.d.ts.map +1 -0
- package/dist/entities/context/messageContext.js +82 -0
- package/dist/entities/incomingMessage.d.ts +14 -0
- package/dist/entities/incomingMessage.d.ts.map +1 -0
- package/dist/entities/incomingMessage.js +45 -0
- package/dist/entities/responses/imageMessage.d.ts +17 -0
- package/dist/entities/responses/imageMessage.d.ts.map +1 -0
- package/dist/entities/responses/imageMessage.js +17 -0
- package/dist/entities/responses/reaction.d.ts +14 -0
- package/dist/entities/responses/reaction.d.ts.map +1 -0
- package/dist/entities/responses/reaction.js +15 -0
- package/dist/entities/responses/textMessage.d.ts +16 -0
- package/dist/entities/responses/textMessage.d.ts.map +1 -0
- package/dist/entities/responses/textMessage.js +17 -0
- package/dist/entities/responses/unpin.d.ts +12 -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 +17 -0
- package/dist/entities/responses/videoMessage.d.ts.map +1 -0
- package/dist/entities/responses/videoMessage.js +17 -0
- package/dist/entities/states/actionStateBase.d.ts +6 -0
- package/dist/entities/states/actionStateBase.d.ts.map +1 -0
- package/dist/entities/states/actionStateBase.js +10 -0
- package/dist/entities/taskRecord.d.ts +8 -0
- package/dist/entities/taskRecord.d.ts.map +1 -0
- package/dist/entities/taskRecord.js +11 -0
- package/dist/helpers/builders/commandActionBuilder.d.ts +69 -0
- package/dist/helpers/builders/commandActionBuilder.d.ts.map +1 -0
- package/dist/helpers/builders/commandActionBuilder.js +96 -0
- package/dist/helpers/builders/scheduledActionBuilder.d.ts +60 -0
- package/dist/helpers/builders/scheduledActionBuilder.d.ts.map +1 -0
- package/dist/helpers/builders/scheduledActionBuilder.js +82 -0
- package/dist/helpers/inverseRecord.d.ts +2 -0
- package/dist/helpers/inverseRecord.d.ts.map +1 -0
- package/dist/helpers/inverseRecord.js +6 -0
- package/dist/helpers/noop.d.ts +6 -0
- package/dist/helpers/noop.d.ts.map +1 -0
- package/dist/helpers/noop.js +14 -0
- package/dist/helpers/reverseMap.d.ts +2 -0
- package/dist/helpers/reverseMap.d.ts.map +1 -0
- package/dist/helpers/reverseMap.js +6 -0
- package/dist/helpers/reverseRecord.d.ts +2 -0
- package/dist/helpers/reverseRecord.d.ts.map +1 -0
- package/dist/helpers/reverseRecord.js +6 -0
- package/dist/helpers/timeConvertions.d.ts +5 -0
- package/dist/helpers/timeConvertions.d.ts.map +1 -0
- package/dist/helpers/timeConvertions.js +14 -0
- package/dist/helpers/toArray.d.ts +2 -0
- package/dist/helpers/toArray.d.ts.map +1 -0
- package/dist/helpers/toArray.js +6 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -0
- package/dist/main.d.ts +35 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +43 -0
- package/dist/services/jsonFileStorage.d.ts +22 -0
- package/dist/services/jsonFileStorage.d.ts.map +1 -0
- package/dist/services/jsonFileStorage.js +102 -0
- package/dist/services/logger.d.ts +9 -0
- package/dist/services/logger.d.ts.map +1 -0
- package/dist/services/logger.js +28 -0
- package/dist/services/taskScheduler.d.ts +11 -0
- package/dist/services/taskScheduler.d.ts.map +1 -0
- package/dist/services/taskScheduler.js +33 -0
- package/dist/services/telegramApi.d.ts +36 -0
- package/dist/services/telegramApi.d.ts.map +1 -0
- package/dist/services/telegramApi.js +100 -0
- package/dist/types/actionState.d.ts +5 -0
- package/dist/types/actionState.d.ts.map +1 -0
- package/dist/types/actionState.js +2 -0
- package/dist/types/actionWithState.d.ts +9 -0
- package/dist/types/actionWithState.d.ts.map +1 -0
- package/dist/types/actionWithState.js +2 -0
- package/dist/types/cachedValueAccessor.d.ts +2 -0
- package/dist/types/cachedValueAccessor.d.ts.map +1 -0
- package/dist/types/cachedValueAccessor.js +2 -0
- package/dist/types/commandCondition.d.ts +4 -0
- package/dist/types/commandCondition.d.ts.map +1 -0
- package/dist/types/commandCondition.js +2 -0
- package/dist/types/commandTrigger.d.ts +3 -0
- package/dist/types/commandTrigger.d.ts.map +1 -0
- package/dist/types/commandTrigger.js +2 -0
- package/dist/types/daysOfTheWeek.d.ts +10 -0
- package/dist/types/daysOfTheWeek.d.ts.map +1 -0
- package/dist/types/daysOfTheWeek.js +13 -0
- package/dist/types/handlers.d.ts +17 -0
- package/dist/types/handlers.d.ts.map +1 -0
- package/dist/types/handlers.js +2 -0
- package/dist/types/messageSendingOptions.d.ts +7 -0
- package/dist/types/messageSendingOptions.d.ts.map +1 -0
- package/dist/types/messageSendingOptions.js +2 -0
- package/dist/types/messageTypes.d.ts +16 -0
- package/dist/types/messageTypes.d.ts.map +1 -0
- package/dist/types/messageTypes.js +17 -0
- package/dist/types/replyMessage.d.ts +8 -0
- package/dist/types/replyMessage.d.ts.map +1 -0
- package/dist/types/replyMessage.js +2 -0
- package/dist/types/response.d.ts +28 -0
- package/dist/types/response.d.ts.map +1 -0
- package/dist/types/response.js +10 -0
- package/dist/types/storage.d.ts +12 -0
- package/dist/types/storage.d.ts.map +1 -0
- package/dist/types/storage.js +2 -0
- package/dist/types/timeValues.d.ts +11 -0
- package/dist/types/timeValues.d.ts.map +1 -0
- package/dist/types/timeValues.js +2 -0
- package/entities/actionExecutionResult.ts +3 -3
- package/entities/actions/commandAction.ts +7 -2
- package/entities/actions/scheduledAction.ts +7 -2
- package/entities/context/chatContext.ts +4 -3
- package/entities/context/messageContext.ts +1 -1
- package/entities/responses/imageMessage.ts +3 -2
- package/entities/responses/reaction.ts +3 -2
- package/entities/responses/textMessage.ts +3 -2
- package/entities/responses/unpin.ts +3 -2
- package/entities/responses/videoMessage.ts +3 -2
- package/package.json +29 -29
- package/services/jsonFileStorage.ts +22 -13
- package/types/actionWithState.ts +2 -2
- package/types/response.ts +2 -1
- package/types/storage.ts +9 -6
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BotResponseTypes, IChatResponse } from '../../types/response';
|
|
2
2
|
import { IActionWithState } from '../../types/actionWithState';
|
|
3
|
+
import { IActionState } from '../../types/actionState';
|
|
3
4
|
|
|
4
5
|
export class UnpinResponse implements IChatResponse {
|
|
5
6
|
kind = BotResponseTypes.unpin;
|
|
@@ -7,13 +8,13 @@ export class UnpinResponse implements IChatResponse {
|
|
|
7
8
|
messageId: number;
|
|
8
9
|
chatId: number;
|
|
9
10
|
traceId: number | string;
|
|
10
|
-
action: IActionWithState
|
|
11
|
+
action: IActionWithState<IActionState>;
|
|
11
12
|
|
|
12
13
|
constructor(
|
|
13
14
|
messageId: number,
|
|
14
15
|
chatId: number,
|
|
15
16
|
traceId: number | string,
|
|
16
|
-
action: IActionWithState
|
|
17
|
+
action: IActionWithState<IActionState>
|
|
17
18
|
) {
|
|
18
19
|
this.messageId = messageId;
|
|
19
20
|
this.chatId = chatId;
|
|
@@ -2,6 +2,7 @@ import { InputFile } from 'telegraf/types';
|
|
|
2
2
|
import { BotResponseTypes, IReplyMessage } from '../../types/response';
|
|
3
3
|
import { MessageSendingOptions } from '../../types/messageSendingOptions';
|
|
4
4
|
import { IActionWithState } from '../../types/actionWithState';
|
|
5
|
+
import { IActionState } from '../../types/actionState';
|
|
5
6
|
|
|
6
7
|
export class VideoMessage implements IReplyMessage<InputFile> {
|
|
7
8
|
kind = BotResponseTypes.video;
|
|
@@ -12,14 +13,14 @@ export class VideoMessage implements IReplyMessage<InputFile> {
|
|
|
12
13
|
traceId: string | number;
|
|
13
14
|
disableWebPreview = false;
|
|
14
15
|
shouldPin: boolean;
|
|
15
|
-
action: IActionWithState
|
|
16
|
+
action: IActionWithState<IActionState>;
|
|
16
17
|
|
|
17
18
|
constructor(
|
|
18
19
|
video: InputFile,
|
|
19
20
|
chatId: number,
|
|
20
21
|
replyId: number | undefined,
|
|
21
22
|
traceId: number | string,
|
|
22
|
-
action: IActionWithState
|
|
23
|
+
action: IActionWithState<IActionState>,
|
|
23
24
|
options?: MessageSendingOptions
|
|
24
25
|
) {
|
|
25
26
|
this.content = video;
|
package/package.json
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "chz-telegram-bot",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"type": "module",
|
|
5
|
-
"dependencies": {
|
|
6
|
-
"async-sema": "^3.1.1",
|
|
7
|
-
"moment": "^2.29.4",
|
|
8
|
-
"telegraf": "^4.16.3"
|
|
9
|
-
},
|
|
10
|
-
"main": "dist/index.js",
|
|
11
|
-
"types": "dist/index.d.ts",
|
|
12
|
-
"devDependencies": {
|
|
13
|
-
"@eslint/js": "^9.10.0",
|
|
14
|
-
"@types/markdown-escape": "^1.1.3",
|
|
15
|
-
"@types/node": "^22.5.5",
|
|
16
|
-
"eslint": "^9.10.0",
|
|
17
|
-
"typescript": "^5.6.2",
|
|
18
|
-
"typescript-eslint": "^8.5.0"
|
|
19
|
-
},
|
|
20
|
-
"scripts": {
|
|
21
|
-
"build": "tsc",
|
|
22
|
-
"lint": "npx eslint && tsc --noEmit"
|
|
23
|
-
},
|
|
24
|
-
"overrides": {
|
|
25
|
-
"telegraf": {
|
|
26
|
-
"node-fetch": "3.3.2"
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "chz-telegram-bot",
|
|
3
|
+
"version": "0.0.47",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"async-sema": "^3.1.1",
|
|
7
|
+
"moment": "^2.29.4",
|
|
8
|
+
"telegraf": "^4.16.3"
|
|
9
|
+
},
|
|
10
|
+
"main": "dist/index.js",
|
|
11
|
+
"types": "dist/index.d.ts",
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@eslint/js": "^9.10.0",
|
|
14
|
+
"@types/markdown-escape": "^1.1.3",
|
|
15
|
+
"@types/node": "^22.5.5",
|
|
16
|
+
"eslint": "^9.10.0",
|
|
17
|
+
"typescript": "^5.6.2",
|
|
18
|
+
"typescript-eslint": "^8.5.0"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"lint": "npx eslint && tsc --noEmit"
|
|
23
|
+
},
|
|
24
|
+
"overrides": {
|
|
25
|
+
"telegraf": {
|
|
26
|
+
"node-fetch": "3.3.2"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -3,7 +3,6 @@ import { dirname } from 'path';
|
|
|
3
3
|
import { mkdir, readFile, writeFile } from 'fs/promises';
|
|
4
4
|
import { Sema as Semaphore } from 'async-sema';
|
|
5
5
|
import { IStorageClient } from '../types/storage';
|
|
6
|
-
import { ActionStateBase } from '../entities/states/actionStateBase';
|
|
7
6
|
import { ActionExecutionResult } from '../entities/actionExecutionResult';
|
|
8
7
|
import { IActionState } from '../types/actionState';
|
|
9
8
|
import { IActionWithState, ActionKey } from '../types/actionWithState';
|
|
@@ -14,7 +13,11 @@ export class JsonFileStorage implements IStorageClient {
|
|
|
14
13
|
private storagePath: string;
|
|
15
14
|
private botName: string;
|
|
16
15
|
|
|
17
|
-
constructor(
|
|
16
|
+
constructor(
|
|
17
|
+
botName: string,
|
|
18
|
+
actions: IActionWithState<IActionState>[],
|
|
19
|
+
path?: string
|
|
20
|
+
) {
|
|
18
21
|
this.cache = new Map<string, Record<number, IActionState>>();
|
|
19
22
|
this.botName = botName;
|
|
20
23
|
this.storagePath = path ?? 'storage';
|
|
@@ -67,8 +70,11 @@ export class JsonFileStorage implements IStorageClient {
|
|
|
67
70
|
return (this.cache.get(key) ?? {}) as Record<number, TActionState>;
|
|
68
71
|
}
|
|
69
72
|
|
|
70
|
-
private async save
|
|
71
|
-
|
|
73
|
+
private async save<TActionState extends IActionState>(
|
|
74
|
+
data: Record<number, TActionState>,
|
|
75
|
+
key: ActionKey
|
|
76
|
+
) {
|
|
77
|
+
this.cache.set(key, data);
|
|
72
78
|
|
|
73
79
|
const targetPath = this.buidPathFromKey(key);
|
|
74
80
|
const folderName = dirname(targetPath);
|
|
@@ -93,7 +99,10 @@ export class JsonFileStorage implements IStorageClient {
|
|
|
93
99
|
});
|
|
94
100
|
}
|
|
95
101
|
|
|
96
|
-
async saveMetadata(
|
|
102
|
+
async saveMetadata(
|
|
103
|
+
actions: IActionWithState<IActionState>[],
|
|
104
|
+
botName: string
|
|
105
|
+
) {
|
|
97
106
|
const targetPath = this.buidPathFromKey(
|
|
98
107
|
`Metadata-${botName}` as ActionKey
|
|
99
108
|
);
|
|
@@ -104,7 +113,7 @@ export class JsonFileStorage implements IStorageClient {
|
|
|
104
113
|
}
|
|
105
114
|
|
|
106
115
|
async getActionState<TActionState extends IActionState>(
|
|
107
|
-
action: IActionWithState
|
|
116
|
+
action: IActionWithState<TActionState>,
|
|
108
117
|
chatId: number
|
|
109
118
|
) {
|
|
110
119
|
return await this.lock(action.key, async () => {
|
|
@@ -117,13 +126,13 @@ export class JsonFileStorage implements IStorageClient {
|
|
|
117
126
|
});
|
|
118
127
|
}
|
|
119
128
|
|
|
120
|
-
async saveActionExecutionResult(
|
|
121
|
-
action: IActionWithState
|
|
129
|
+
async saveActionExecutionResult<TActionState extends IActionState>(
|
|
130
|
+
action: IActionWithState<TActionState>,
|
|
122
131
|
chatId: number,
|
|
123
|
-
transactionResult: ActionExecutionResult
|
|
132
|
+
transactionResult: ActionExecutionResult<TActionState>
|
|
124
133
|
) {
|
|
125
134
|
await this.lock(action.key, async () => {
|
|
126
|
-
const data = await this.loadInternal(action.key);
|
|
135
|
+
const data = await this.loadInternal<TActionState>(action.key);
|
|
127
136
|
|
|
128
137
|
if (transactionResult.shouldUpdate) {
|
|
129
138
|
data[chatId] = transactionResult.data;
|
|
@@ -139,16 +148,16 @@ export class JsonFileStorage implements IStorageClient {
|
|
|
139
148
|
}
|
|
140
149
|
|
|
141
150
|
async updateStateFor<TActionState extends IActionState>(
|
|
142
|
-
action: IActionWithState
|
|
151
|
+
action: IActionWithState<TActionState>,
|
|
143
152
|
chatId: number,
|
|
144
153
|
update: (state: TActionState) => Promise<void>
|
|
145
154
|
) {
|
|
146
155
|
await this.lock(action.key, async () => {
|
|
147
|
-
const data = await this.loadInternal(action.key);
|
|
156
|
+
const data = await this.loadInternal<TActionState>(action.key);
|
|
148
157
|
const state = Object.assign(
|
|
149
158
|
action.stateConstructor(),
|
|
150
159
|
data[chatId]
|
|
151
|
-
)
|
|
160
|
+
);
|
|
152
161
|
|
|
153
162
|
await update(state);
|
|
154
163
|
|
package/types/actionWithState.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { IActionState } from './actionState';
|
|
|
2
2
|
|
|
3
3
|
export type ActionKey = string & { __brand: 'actionKey' };
|
|
4
4
|
|
|
5
|
-
export interface IActionWithState {
|
|
5
|
+
export interface IActionWithState<TActionState extends IActionState> {
|
|
6
6
|
key: ActionKey;
|
|
7
|
-
stateConstructor: () =>
|
|
7
|
+
stateConstructor: () => TActionState;
|
|
8
8
|
}
|
package/types/response.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { Reaction } from '../entities/responses/reaction';
|
|
|
3
3
|
import { TextMessage } from '../entities/responses/textMessage';
|
|
4
4
|
import { UnpinResponse } from '../entities/responses/unpin';
|
|
5
5
|
import { VideoMessage } from '../entities/responses/videoMessage';
|
|
6
|
+
import { IActionState } from './actionState';
|
|
6
7
|
import { IActionWithState } from './actionWithState';
|
|
7
8
|
|
|
8
9
|
export const BotResponseTypes = {
|
|
@@ -25,7 +26,7 @@ export interface IChatResponse {
|
|
|
25
26
|
chatId: number;
|
|
26
27
|
traceId: number | string;
|
|
27
28
|
|
|
28
|
-
action: IActionWithState
|
|
29
|
+
action: IActionWithState<IActionState>;
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
export interface IReplyMessage<TType> extends IChatResponse {
|
package/types/storage.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { ActionKey, IActionWithState } from './actionWithState';
|
|
|
4
4
|
|
|
5
5
|
export interface IStorageClient {
|
|
6
6
|
updateStateFor<TActionState extends IActionState>(
|
|
7
|
-
action: IActionWithState
|
|
7
|
+
action: IActionWithState<TActionState>,
|
|
8
8
|
chatId: number,
|
|
9
9
|
update: (state: TActionState) => Promise<void>
|
|
10
10
|
): Promise<void>;
|
|
@@ -12,14 +12,17 @@ export interface IStorageClient {
|
|
|
12
12
|
load<TActionState extends IActionState>(
|
|
13
13
|
key: ActionKey
|
|
14
14
|
): Promise<Record<number, TActionState>>;
|
|
15
|
-
saveMetadata
|
|
15
|
+
saveMetadata<TActionState extends IActionState>(
|
|
16
|
+
actions: IActionWithState<TActionState>[],
|
|
17
|
+
botName: string
|
|
18
|
+
): Promise<void>;
|
|
16
19
|
getActionState<TActionState extends IActionState>(
|
|
17
|
-
action: IActionWithState
|
|
20
|
+
action: IActionWithState<TActionState>,
|
|
18
21
|
chatId: number
|
|
19
22
|
): Promise<TActionState>;
|
|
20
|
-
saveActionExecutionResult(
|
|
21
|
-
action: IActionWithState
|
|
23
|
+
saveActionExecutionResult<TActionState extends IActionState>(
|
|
24
|
+
action: IActionWithState<TActionState>,
|
|
22
25
|
chatId: number,
|
|
23
|
-
transactionResult: ActionExecutionResult
|
|
26
|
+
transactionResult: ActionExecutionResult<TActionState>
|
|
24
27
|
): Promise<void>;
|
|
25
28
|
}
|