chz-telegram-bot 0.0.2 → 0.0.3
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/actions/commandAction.d.ts +22 -0
- package/dist/entities/actions/commandAction.d.ts.map +1 -0
- package/dist/entities/actions/commandAction.js +2 -3
- 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 +2 -3
- package/dist/entities/bot.d.ts +27 -0
- package/dist/entities/bot.d.ts.map +1 -0
- package/dist/entities/bot.js +14 -10
- package/dist/entities/cachedStateFactory.d.ts +7 -0
- package/dist/entities/cachedStateFactory.d.ts.map +1 -0
- package/dist/entities/commandTriggerCheckResult.d.ts +10 -0
- package/dist/entities/commandTriggerCheckResult.d.ts.map +1 -0
- package/dist/entities/context/chatContext.d.ts +15 -0
- package/dist/entities/context/chatContext.d.ts.map +1 -0
- package/dist/entities/context/chatContext.js +2 -1
- package/dist/entities/context/messageContext.d.ts +22 -0
- package/dist/entities/context/messageContext.d.ts.map +1 -0
- package/dist/entities/context/messageContext.js +3 -4
- package/dist/entities/incomingMessage.d.ts +16 -0
- package/dist/entities/incomingMessage.d.ts.map +1 -0
- package/dist/entities/incomingMessage.js +2 -5
- package/dist/entities/responses/imageMessage.d.ts +10 -0
- package/dist/entities/responses/imageMessage.d.ts.map +1 -0
- package/dist/entities/responses/reaction.d.ts +9 -0
- package/dist/entities/responses/reaction.d.ts.map +1 -0
- package/dist/entities/responses/textMessage.d.ts +9 -0
- package/dist/entities/responses/textMessage.d.ts.map +1 -0
- package/dist/entities/responses/videoMessage.d.ts +10 -0
- package/dist/entities/responses/videoMessage.d.ts.map +1 -0
- package/dist/entities/states/actionStateBase.d.ts +5 -0
- package/dist/entities/states/actionStateBase.d.ts.map +1 -0
- package/dist/entities/taskRecord.d.ts +8 -0
- package/dist/entities/taskRecord.d.ts.map +1 -0
- package/dist/entities/transactionResult.d.ts +7 -0
- package/dist/entities/transactionResult.d.ts.map +1 -0
- package/dist/helpers/builders/commandActionBuilder.d.ts +30 -0
- package/dist/helpers/builders/commandActionBuilder.d.ts.map +1 -0
- package/dist/helpers/builders/scheduledActionBuilder.d.ts +20 -0
- package/dist/helpers/builders/scheduledActionBuilder.d.ts.map +1 -0
- package/dist/helpers/noop.d.ts +7 -0
- package/dist/helpers/noop.d.ts.map +1 -0
- package/dist/helpers/reverseMap.d.ts +2 -0
- package/dist/helpers/reverseMap.d.ts.map +1 -0
- package/dist/helpers/timeConvertions.d.ts +5 -0
- package/dist/helpers/timeConvertions.d.ts.map +1 -0
- package/dist/helpers/toArray.d.ts +2 -0
- package/dist/helpers/toArray.d.ts.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -17
- package/dist/main.d.ts +8 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +10 -6
- package/dist/services/jsonFileStorage.d.ts +23 -0
- package/dist/services/jsonFileStorage.d.ts.map +1 -0
- package/dist/services/{storage.js → jsonFileStorage.js} +13 -11
- package/dist/services/logger.d.ts +7 -0
- package/dist/services/logger.d.ts.map +1 -0
- package/dist/services/taskScheduler.d.ts +11 -0
- package/dist/services/taskScheduler.d.ts.map +1 -0
- package/dist/services/telegramApi.d.ts +27 -0
- package/dist/services/telegramApi.d.ts.map +1 -0
- package/dist/services/telegramApi.js +4 -3
- package/dist/types/actionState.d.ts +4 -0
- package/dist/types/actionState.d.ts.map +1 -0
- package/dist/types/actionWithState.d.ts +6 -0
- package/dist/types/actionWithState.d.ts.map +1 -0
- package/dist/types/cachedValueAccessor.d.ts +2 -0
- package/dist/types/cachedValueAccessor.d.ts.map +1 -0
- package/dist/types/commandCondition.d.ts +4 -0
- package/dist/types/commandCondition.d.ts.map +1 -0
- package/dist/types/daysOfTheWeek.d.ts +10 -0
- package/dist/types/daysOfTheWeek.d.ts.map +1 -0
- package/dist/types/handlers.d.ts +7 -0
- package/dist/types/handlers.d.ts.map +1 -0
- package/dist/types/replyMessage.d.ts +7 -0
- package/dist/types/replyMessage.d.ts.map +1 -0
- package/dist/types/storage.d.ts +12 -0
- package/dist/types/storage.d.ts.map +1 -0
- package/dist/types/timeValues.d.ts +11 -0
- package/dist/types/timeValues.d.ts.map +1 -0
- package/entities/actions/commandAction.ts +4 -5
- package/entities/actions/scheduledAction.ts +2 -3
- package/entities/bot.ts +39 -24
- package/entities/context/chatContext.ts +5 -1
- package/entities/context/messageContext.ts +10 -6
- package/entities/incomingMessage.ts +2 -2
- package/index.ts +28 -3
- package/main.ts +12 -8
- package/package.json +1 -2
- package/services/{storage.ts → jsonFileStorage.ts} +20 -14
- package/services/telegramApi.ts +8 -2
- package/tsconfig.json +2 -2
- package/types/storage.ts +19 -0
- package/dist/entities/states/potuzhnoState.js +0 -13
- package/dist/helpers/escapeMarkdown.js +0 -17
- package/dist/helpers/getWeek.js +0 -12
- package/dist/helpers/randomInt.js +0 -8
- package/entities/states/potuzhnoState.ts +0 -5
- package/helpers/escapeMarkdown.ts +0 -12
- package/helpers/getWeek.ts +0 -8
- package/helpers/randomInt.ts +0 -7
- package/types/scheduledItem.ts +0 -6
- /package/dist/types/{scheduledItem.js → storage.js} +0 -0
package/main.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { readFile } from 'fs/promises';
|
|
2
2
|
import BotInstance from './entities/bot.js';
|
|
3
3
|
import taskScheduler from './services/taskScheduler.js';
|
|
4
|
-
import storage from './services/storage.js';
|
|
5
4
|
import logger from './services/logger.js';
|
|
6
5
|
import CommandAction from './entities/actions/commandAction.js';
|
|
7
6
|
import IActionState from './types/actionState.js';
|
|
@@ -15,13 +14,21 @@ function log(text: string) {
|
|
|
15
14
|
|
|
16
15
|
async function startBot(
|
|
17
16
|
name: string,
|
|
18
|
-
|
|
17
|
+
tokenFilePath: string,
|
|
19
18
|
commands: CommandAction<IActionState>[],
|
|
20
19
|
scheduled: ScheduledAction[],
|
|
21
|
-
chats: Map<string, number
|
|
20
|
+
chats: Map<string, number>,
|
|
21
|
+
storagePath?: string
|
|
22
22
|
) {
|
|
23
|
-
const token = await readFile(
|
|
24
|
-
const bot = new BotInstance(
|
|
23
|
+
const token = await readFile(tokenFilePath, 'utf8');
|
|
24
|
+
const bot = new BotInstance({
|
|
25
|
+
name,
|
|
26
|
+
token,
|
|
27
|
+
commands,
|
|
28
|
+
scheduled,
|
|
29
|
+
chats,
|
|
30
|
+
storagePath
|
|
31
|
+
});
|
|
25
32
|
bots.push(bot);
|
|
26
33
|
|
|
27
34
|
return bot;
|
|
@@ -31,14 +38,11 @@ async function stopBots(reason: string) {
|
|
|
31
38
|
log(`Recieved termination code: ${reason}`);
|
|
32
39
|
taskScheduler.stopAll();
|
|
33
40
|
log('Acquiring storage semaphore...');
|
|
34
|
-
await storage.semaphoreInstance.acquire();
|
|
35
41
|
|
|
36
42
|
log('Stopping bots...');
|
|
37
43
|
for (const bot of bots) {
|
|
38
44
|
bot.stop(reason);
|
|
39
45
|
}
|
|
40
|
-
|
|
41
|
-
process.exit(0);
|
|
42
46
|
}
|
|
43
47
|
|
|
44
48
|
export { startBot, stopBots };
|
package/package.json
CHANGED
|
@@ -6,26 +6,27 @@ import ActionStateBase from '../entities/states/actionStateBase';
|
|
|
6
6
|
import IActionState from '../types/actionState';
|
|
7
7
|
import IActionWithState from '../types/actionWithState';
|
|
8
8
|
import { Sema as Semaphore } from 'async-sema';
|
|
9
|
+
import { IStorageClient } from '../types/storage';
|
|
9
10
|
|
|
10
|
-
class
|
|
11
|
-
|
|
12
|
-
cache: Map<string, Record<number, ActionStateBase>>;
|
|
11
|
+
export default class JsonFileStorage implements IStorageClient {
|
|
12
|
+
semaphore = new Semaphore(1);
|
|
13
|
+
private cache: Map<string, Record<number, ActionStateBase>>;
|
|
14
|
+
private storagePath: string;
|
|
15
|
+
private botName: string;
|
|
13
16
|
|
|
14
|
-
|
|
15
|
-
return Storage.semaphore;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
constructor() {
|
|
17
|
+
constructor(botName: string, path: string | undefined) {
|
|
19
18
|
this.cache = new Map<string, Record<number, ActionStateBase>>();
|
|
19
|
+
this.botName = botName;
|
|
20
|
+
this.storagePath = path ?? 'storage';
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
private async lock<TType>(action: () => Promise<TType>) {
|
|
23
|
-
await this.
|
|
24
|
+
await this.semaphore.acquire();
|
|
24
25
|
|
|
25
26
|
try {
|
|
26
27
|
return await action();
|
|
27
28
|
} finally {
|
|
28
|
-
this.
|
|
29
|
+
this.semaphore.release();
|
|
29
30
|
}
|
|
30
31
|
}
|
|
31
32
|
|
|
@@ -62,7 +63,10 @@ class Storage {
|
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
private buidPathFromKey(key: string) {
|
|
65
|
-
return
|
|
66
|
+
return `${this.storagePath}/${this.botName}/${key.replaceAll(
|
|
67
|
+
':',
|
|
68
|
+
'/'
|
|
69
|
+
)}.json`;
|
|
66
70
|
}
|
|
67
71
|
|
|
68
72
|
async load(key: string) {
|
|
@@ -92,7 +96,7 @@ class Storage {
|
|
|
92
96
|
});
|
|
93
97
|
}
|
|
94
98
|
|
|
95
|
-
async
|
|
99
|
+
async saveActionExecutionResult(
|
|
96
100
|
action: IActionWithState,
|
|
97
101
|
chatId: number,
|
|
98
102
|
transactionResult: TransactionResult
|
|
@@ -106,6 +110,8 @@ class Storage {
|
|
|
106
110
|
}
|
|
107
111
|
});
|
|
108
112
|
}
|
|
109
|
-
}
|
|
110
113
|
|
|
111
|
-
|
|
114
|
+
async close(): Promise<void> {
|
|
115
|
+
await this.semaphore.acquire();
|
|
116
|
+
}
|
|
117
|
+
}
|
package/services/telegramApi.ts
CHANGED
|
@@ -12,21 +12,25 @@ import { Milliseconds } from '../types/timeValues';
|
|
|
12
12
|
import Reaction from '../entities/responses/reaction';
|
|
13
13
|
import { InputFile } from 'telegraf/types';
|
|
14
14
|
import { reverseMap } from '../helpers/reverseMap';
|
|
15
|
+
import { IStorageClient } from '../types/storage';
|
|
15
16
|
|
|
16
17
|
export default class TelegramApiService {
|
|
17
18
|
botName: string;
|
|
18
19
|
telegraf: Telegraf;
|
|
19
20
|
chats: Map<number, string>;
|
|
20
21
|
messageQueue: Array<IReplyMessage<unknown> | Reaction> = [];
|
|
22
|
+
storage: IStorageClient;
|
|
21
23
|
|
|
22
24
|
constructor(
|
|
23
25
|
botName: string,
|
|
24
26
|
telegraf: Telegraf,
|
|
27
|
+
storage: IStorageClient,
|
|
25
28
|
chats: Map<string, number>
|
|
26
29
|
) {
|
|
27
30
|
this.telegraf = telegraf;
|
|
28
31
|
this.botName = botName;
|
|
29
32
|
this.chats = reverseMap(chats);
|
|
33
|
+
this.storage = storage;
|
|
30
34
|
|
|
31
35
|
taskScheduler.createTask(
|
|
32
36
|
'MessageSending',
|
|
@@ -151,7 +155,8 @@ export default class TelegramApiService {
|
|
|
151
155
|
incomingMessage.text,
|
|
152
156
|
incomingMessage.from?.id,
|
|
153
157
|
incomingMessage.traceId,
|
|
154
|
-
firstName + lastName
|
|
158
|
+
firstName + lastName,
|
|
159
|
+
this.storage
|
|
155
160
|
);
|
|
156
161
|
}
|
|
157
162
|
|
|
@@ -161,7 +166,8 @@ export default class TelegramApiService {
|
|
|
161
166
|
this.getInteractions(),
|
|
162
167
|
chatId,
|
|
163
168
|
this.chats.get(chatId)!,
|
|
164
|
-
`Scheduled:${scheduledName}:${chatId}
|
|
169
|
+
`Scheduled:${scheduledName}:${chatId}`,
|
|
170
|
+
this.storage
|
|
165
171
|
);
|
|
166
172
|
}
|
|
167
173
|
}
|
package/tsconfig.json
CHANGED
|
@@ -50,8 +50,8 @@
|
|
|
50
50
|
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
51
51
|
|
|
52
52
|
/* Emit */
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
54
|
+
"declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
55
55
|
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
56
56
|
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
57
57
|
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
package/types/storage.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import ActionStateBase from '../entities/states/actionStateBase';
|
|
2
|
+
import TransactionResult from '../entities/transactionResult';
|
|
3
|
+
import IActionState from './actionState';
|
|
4
|
+
import IActionWithState from './actionWithState';
|
|
5
|
+
|
|
6
|
+
export interface IStorageClient {
|
|
7
|
+
close(): Promise<void>;
|
|
8
|
+
load(key: string): Promise<Record<number, ActionStateBase>>;
|
|
9
|
+
saveMetadata(actions: IActionWithState[], botName: string): Promise<void>;
|
|
10
|
+
getActionState<TActionState extends IActionState>(
|
|
11
|
+
entity: IActionWithState,
|
|
12
|
+
chatId: number
|
|
13
|
+
): Promise<TActionState>;
|
|
14
|
+
saveActionExecutionResult(
|
|
15
|
+
action: IActionWithState,
|
|
16
|
+
chatId: number,
|
|
17
|
+
transactionResult: TransactionResult
|
|
18
|
+
): Promise<void>;
|
|
19
|
+
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const actionStateBase_1 = __importDefault(require("./actionStateBase"));
|
|
7
|
-
class PotuzhnoState extends actionStateBase_1.default {
|
|
8
|
-
constructor() {
|
|
9
|
-
super(...arguments);
|
|
10
|
-
this.scoreBoard = {};
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
exports.default = PotuzhnoState;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.default = escapeMarkdown;
|
|
7
|
-
const markdown_escape_1 = __importDefault(require("markdown-escape"));
|
|
8
|
-
function escapeMarkdown(text) {
|
|
9
|
-
return (0, markdown_escape_1.default)(text)
|
|
10
|
-
.replaceAll(/\./g, '\\.')
|
|
11
|
-
.replaceAll(/-/g, '\\-')
|
|
12
|
-
.replaceAll(/\+/g, '\\+')
|
|
13
|
-
.replaceAll(/\{/g, '\\{')
|
|
14
|
-
.replaceAll(/!/g, '\\!')
|
|
15
|
-
.replaceAll(/\|/g, '\\|')
|
|
16
|
-
.replaceAll(/\}/g, '\\}');
|
|
17
|
-
}
|
package/dist/helpers/getWeek.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.default = getCurrentWeek;
|
|
7
|
-
const moment_1 = __importDefault(require("moment"));
|
|
8
|
-
function getCurrentWeek() {
|
|
9
|
-
const firstDay = (0, moment_1.default)().startOf('isoWeek').format('YYYY-MM-DD');
|
|
10
|
-
const lastDay = (0, moment_1.default)().endOf('isoWeek').format('YYYY-MM-DD');
|
|
11
|
-
return { firstDay, lastDay };
|
|
12
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import escape from 'markdown-escape';
|
|
2
|
-
|
|
3
|
-
export default function escapeMarkdown(text: string) {
|
|
4
|
-
return escape(text)
|
|
5
|
-
.replaceAll(/\./g, '\\.')
|
|
6
|
-
.replaceAll(/-/g, '\\-')
|
|
7
|
-
.replaceAll(/\+/g, '\\+')
|
|
8
|
-
.replaceAll(/\{/g, '\\{')
|
|
9
|
-
.replaceAll(/!/g, '\\!')
|
|
10
|
-
.replaceAll(/\|/g, '\\|')
|
|
11
|
-
.replaceAll(/\}/g, '\\}');
|
|
12
|
-
}
|
package/helpers/getWeek.ts
DELETED
package/helpers/randomInt.ts
DELETED
package/types/scheduledItem.ts
DELETED
|
File without changes
|