chz-telegram-bot 0.0.23 → 0.0.24
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/botInstance.d.ts +1 -1
- package/dist/entities/botInstance.d.ts.map +1 -1
- package/dist/entities/botInstance.js +24 -6
- package/dist/entities/context/chatContext.d.ts +30 -3
- package/dist/entities/context/chatContext.d.ts.map +1 -1
- package/dist/entities/context/chatContext.js +22 -0
- package/dist/entities/context/messageContext.d.ts +38 -1
- package/dist/entities/context/messageContext.d.ts.map +1 -1
- package/dist/entities/context/messageContext.js +34 -1
- package/dist/helpers/builders/commandActionBuilder.d.ts +38 -0
- package/dist/helpers/builders/commandActionBuilder.d.ts.map +1 -1
- package/dist/helpers/builders/commandActionBuilder.js +38 -0
- package/dist/helpers/builders/scheduledActionBuilder.d.ts +34 -0
- package/dist/helpers/builders/scheduledActionBuilder.d.ts.map +1 -1
- package/dist/helpers/builders/scheduledActionBuilder.js +34 -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/main.d.ts +14 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +7 -1
- package/dist/services/taskScheduler.d.ts.map +1 -1
- package/dist/services/taskScheduler.js +1 -2
- package/dist/services/telegramApi.d.ts +2 -2
- package/dist/services/telegramApi.d.ts.map +1 -1
- package/dist/services/telegramApi.js +5 -4
- package/dist/types/handlers.d.ts +12 -2
- package/dist/types/handlers.d.ts.map +1 -1
- package/entities/botInstance.ts +43 -11
- package/entities/context/chatContext.ts +30 -3
- package/entities/context/messageContext.ts +38 -1
- package/helpers/builders/commandActionBuilder.ts +38 -0
- package/helpers/builders/scheduledActionBuilder.ts +34 -0
- package/helpers/reverseRecord.ts +7 -0
- package/main.ts +15 -2
- package/package.json +1 -1
- package/services/taskScheduler.ts +2 -3
- package/services/telegramApi.ts +8 -6
- package/types/handlers.ts +5 -0
- package/helpers/reverseMap.ts +0 -3
package/main.ts
CHANGED
|
@@ -13,13 +13,23 @@ function log(text: string) {
|
|
|
13
13
|
Logger.logWithTraceId('ALL BOTS', 'System:Bot', 'System', text);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Starts bot
|
|
18
|
+
*/
|
|
16
19
|
async function startBot(options: {
|
|
20
|
+
/** Bot name, used in logging */
|
|
17
21
|
name: string;
|
|
22
|
+
/** Path to file containing Telegram Bot token. */
|
|
18
23
|
tokenFilePath: string;
|
|
24
|
+
/** Collection of actions that will be executed as a response to message from used. Created using `CommandActionBuilder`.*/
|
|
19
25
|
commands: CommandAction<IActionState>[];
|
|
26
|
+
/** Collection of actions that will be executed on timer. Created using `ScheduledActionBuilder`.*/
|
|
20
27
|
scheduled: ScheduledAction<IActionState>[];
|
|
21
|
-
|
|
28
|
+
/** Object containing chat name and chat id pairs. Used for logging and execution of scheduled action. */
|
|
29
|
+
chats: Record<string, number>;
|
|
30
|
+
/** Storage client for bot state storage. If not provided, default `JsonFileStorage` will be used. */
|
|
22
31
|
storageClient?: IStorageClient;
|
|
32
|
+
/** Storage path for default `JsonFileStorage` client. Will be used only if `storageClient` is not provided. If not provided, default value of `./storage/` will be used.*/
|
|
23
33
|
storagePath?: string;
|
|
24
34
|
}) {
|
|
25
35
|
const token = await readFile(options.tokenFilePath, 'utf8');
|
|
@@ -37,6 +47,9 @@ async function startBot(options: {
|
|
|
37
47
|
return bot;
|
|
38
48
|
}
|
|
39
49
|
|
|
50
|
+
/**
|
|
51
|
+
* Terminates all scheduled tasks, closes storage connections and stops all bots.
|
|
52
|
+
*/
|
|
40
53
|
async function stopBots(reason: string) {
|
|
41
54
|
log(`Recieved termination code: ${reason}`);
|
|
42
55
|
Scheduler.stopAll();
|
|
@@ -44,7 +57,7 @@ async function stopBots(reason: string) {
|
|
|
44
57
|
|
|
45
58
|
log('Stopping bots...');
|
|
46
59
|
for (const bot of bots) {
|
|
47
|
-
bot.stop(reason);
|
|
60
|
+
await bot.stop(reason);
|
|
48
61
|
}
|
|
49
62
|
}
|
|
50
63
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { TaskRecord } from '../entities/taskRecord';
|
|
2
|
-
import {
|
|
3
|
-
import { Milliseconds, Seconds } from '../types/timeValues';
|
|
2
|
+
import { Milliseconds } from '../types/timeValues';
|
|
4
3
|
import { Logger } from './logger';
|
|
5
4
|
|
|
6
5
|
class TaskScheduler {
|
|
@@ -24,7 +23,7 @@ class TaskScheduler {
|
|
|
24
23
|
const task = new TaskRecord(name, taskId, interval);
|
|
25
24
|
|
|
26
25
|
if (executeRightAway) {
|
|
27
|
-
|
|
26
|
+
setImmediate(action);
|
|
28
27
|
}
|
|
29
28
|
|
|
30
29
|
Logger.logWithTraceId(
|
package/services/telegramApi.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { InputFile, Message } from 'telegraf/types';
|
|
2
2
|
import { ChatContext } from '../entities/context/chatContext';
|
|
3
3
|
import { MessageContext } from '../entities/context/messageContext';
|
|
4
|
-
import {
|
|
4
|
+
import { reverseRecord } from '../helpers/reverseRecord';
|
|
5
5
|
import { IStorageClient } from '../types/storage';
|
|
6
6
|
import { Logger } from './logger';
|
|
7
7
|
import { Reaction } from '../entities/responses/reaction';
|
|
@@ -21,24 +21,26 @@ export class TelegramApiService {
|
|
|
21
21
|
|
|
22
22
|
botName: string;
|
|
23
23
|
telegram: Telegram;
|
|
24
|
-
chats:
|
|
24
|
+
chats: Record<number, string>;
|
|
25
25
|
storage: IStorageClient;
|
|
26
26
|
|
|
27
27
|
constructor(
|
|
28
28
|
botName: string,
|
|
29
29
|
telegram: Telegram,
|
|
30
30
|
storage: IStorageClient,
|
|
31
|
-
chats:
|
|
31
|
+
chats: Record<string, number>
|
|
32
32
|
) {
|
|
33
33
|
this.telegram = telegram;
|
|
34
34
|
this.botName = botName;
|
|
35
|
-
this.chats =
|
|
35
|
+
this.chats = reverseRecord(chats);
|
|
36
36
|
this.storage = storage;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
async flushResponses() {
|
|
40
40
|
if (this.isFlushing) return;
|
|
41
41
|
|
|
42
|
+
this.isFlushing = true;
|
|
43
|
+
|
|
42
44
|
while (this.messageQueue.length) {
|
|
43
45
|
const message = this.messageQueue.pop();
|
|
44
46
|
|
|
@@ -51,7 +53,7 @@ export class TelegramApiService {
|
|
|
51
53
|
Logger.errorWithTraceId(
|
|
52
54
|
this.botName,
|
|
53
55
|
message.traceId,
|
|
54
|
-
this.chats
|
|
56
|
+
this.chats[message.chatId],
|
|
55
57
|
error,
|
|
56
58
|
message
|
|
57
59
|
);
|
|
@@ -199,7 +201,7 @@ export class TelegramApiService {
|
|
|
199
201
|
scheduledKey,
|
|
200
202
|
this.getInteractions(),
|
|
201
203
|
chatId,
|
|
202
|
-
this.chats
|
|
204
|
+
this.chats[chatId],
|
|
203
205
|
`Scheduled:${scheduledKey}:${chatId}`,
|
|
204
206
|
this.storage
|
|
205
207
|
);
|
package/types/handlers.ts
CHANGED
|
@@ -4,12 +4,17 @@ import { IActionState } from './actionState';
|
|
|
4
4
|
import { CachedValueAccessor } from './cachedValueAccessor';
|
|
5
5
|
|
|
6
6
|
export type CommandHandler<TActionState extends IActionState> = (
|
|
7
|
+
/** Context of action executed in chat, in response to a message. */
|
|
7
8
|
ctx: MessageContext<TActionState>,
|
|
9
|
+
/** State of an action being executed. */
|
|
8
10
|
state: TActionState
|
|
9
11
|
) => Promise<void>;
|
|
10
12
|
|
|
11
13
|
export type ScheduledHandler<TActionState extends IActionState> = (
|
|
14
|
+
/** Context of action executed in chat. */
|
|
12
15
|
ctx: ChatContext,
|
|
16
|
+
/** Function that will attempt to get value from cache. If there is no value found, corresponding cached state factory will be called. */
|
|
13
17
|
getCached: CachedValueAccessor,
|
|
18
|
+
/** State of an action being executed. */
|
|
14
19
|
state: TActionState
|
|
15
20
|
) => Promise<void>;
|
package/helpers/reverseMap.ts
DELETED