chz-telegram-bot 0.0.21 → 0.0.22
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 -2
- package/dist/entities/botInstance.d.ts.map +1 -1
- package/dist/entities/botInstance.js +3 -9
- package/dist/services/telegramApi.d.ts +3 -2
- package/dist/services/telegramApi.d.ts.map +1 -1
- package/dist/services/telegramApi.js +18 -14
- package/entities/botInstance.ts +6 -22
- package/package.json +1 -1
- package/services/telegramApi.ts +27 -28
|
@@ -9,7 +9,6 @@ export declare class BotInstance {
|
|
|
9
9
|
private commands;
|
|
10
10
|
private scheduled;
|
|
11
11
|
private chats;
|
|
12
|
-
private messageQueue;
|
|
13
12
|
storage: IStorageClient;
|
|
14
13
|
constructor(options: {
|
|
15
14
|
name: string;
|
|
@@ -24,6 +23,6 @@ export declare class BotInstance {
|
|
|
24
23
|
private initializeMessageProcessing;
|
|
25
24
|
stop(code: string): Promise<void>;
|
|
26
25
|
private runScheduled;
|
|
27
|
-
private
|
|
26
|
+
private processMessage;
|
|
28
27
|
}
|
|
29
28
|
//# sourceMappingURL=botInstance.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"botInstance.d.ts","sourceRoot":"","sources":["../../entities/botInstance.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"botInstance.d.ts","sourceRoot":"","sources":["../../entities/botInstance.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAGlD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAK5D,qBAAa,WAAW;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,GAAG,CAAqB;IAChC,OAAO,CAAC,QAAQ,CAAW;IAC3B,OAAO,CAAC,QAAQ,CAAgC;IAChD,OAAO,CAAC,SAAS,CAAkC;IACnD,OAAO,CAAC,KAAK,CAAsB;IACnC,OAAO,EAAE,cAAc,CAAC;gBAEZ,OAAO,EAAE;QACjB,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,CAAC;QACxC,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;QAC3C,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC3B,aAAa,CAAC,EAAE,cAAc,CAAC;QAC/B,WAAW,CAAC,EAAE,MAAM,CAAC;KACxB;IAkCD,OAAO,CAAC,6BAA6B;IAcrC,OAAO,CAAC,2BAA2B;IAsB7B,IAAI,CAAC,IAAI,EAAE,MAAM;YAYT,YAAY;YAoBZ,cAAc;CAmB/B"}
|
|
@@ -10,7 +10,6 @@ const taskScheduler_1 = require("../services/taskScheduler");
|
|
|
10
10
|
const incomingMessage_1 = require("./incomingMessage");
|
|
11
11
|
class BotInstance {
|
|
12
12
|
constructor(options) {
|
|
13
|
-
this.messageQueue = [];
|
|
14
13
|
this.name = options.name;
|
|
15
14
|
this.commands = options.commands;
|
|
16
15
|
this.scheduled = options.scheduled;
|
|
@@ -42,14 +41,9 @@ class BotInstance {
|
|
|
42
41
|
const messageFromId = msg.from?.id ?? 'Unknown';
|
|
43
42
|
logger_1.Logger.logWithTraceId(this.name, msg.traceId, msg.chatName, `${messageFromName} (${messageFromId}): ${messageContent}`);
|
|
44
43
|
if (msg.text) {
|
|
45
|
-
this.
|
|
44
|
+
await this.processMessage(msg);
|
|
46
45
|
}
|
|
47
46
|
});
|
|
48
|
-
taskScheduler_1.Scheduler.createTask('MessageProcessing', async () => {
|
|
49
|
-
while (this.messageQueue.length > 0) {
|
|
50
|
-
await this.processMessages();
|
|
51
|
-
}
|
|
52
|
-
}, (0, timeConvertions_1.secondsToMilliseconds)(0.3), false, this.name);
|
|
53
47
|
}
|
|
54
48
|
}
|
|
55
49
|
async stop(code) {
|
|
@@ -70,8 +64,7 @@ class BotInstance {
|
|
|
70
64
|
}
|
|
71
65
|
}
|
|
72
66
|
}
|
|
73
|
-
async
|
|
74
|
-
const msg = this.messageQueue.pop();
|
|
67
|
+
async processMessage(msg) {
|
|
75
68
|
for (const cmd of this.commands) {
|
|
76
69
|
const ctx = this.api.createContextForMessage(msg, cmd.key);
|
|
77
70
|
try {
|
|
@@ -81,6 +74,7 @@ class BotInstance {
|
|
|
81
74
|
logger_1.Logger.errorWithTraceId(ctx.botName, ctx.traceId, ctx.chatName, error, ctx);
|
|
82
75
|
}
|
|
83
76
|
}
|
|
77
|
+
this.api.flushResponses();
|
|
84
78
|
}
|
|
85
79
|
}
|
|
86
80
|
exports.BotInstance = BotInstance;
|
|
@@ -10,13 +10,14 @@ import { VideoMessage } from '../entities/responses/videoMessage';
|
|
|
10
10
|
import { ImageMessage } from '../entities/responses/imageMessage';
|
|
11
11
|
import { Telegram } from 'telegraf/typings/telegram';
|
|
12
12
|
export declare class TelegramApiService {
|
|
13
|
+
isFlushing: boolean;
|
|
14
|
+
messageQueue: Array<BotResponse>;
|
|
13
15
|
botName: string;
|
|
14
16
|
telegram: Telegram;
|
|
15
17
|
chats: Map<number, string>;
|
|
16
|
-
messageQueue: Array<BotResponse>;
|
|
17
18
|
storage: IStorageClient;
|
|
18
19
|
constructor(botName: string, telegram: Telegram, storage: IStorageClient, chats: Map<string, number>);
|
|
19
|
-
|
|
20
|
+
flushResponses(): Promise<void>;
|
|
20
21
|
private pinIfShould;
|
|
21
22
|
private processResponse;
|
|
22
23
|
private enqueue;
|
|
@@ -1 +1 @@
|
|
|
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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E,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"}
|
|
@@ -4,29 +4,33 @@ exports.TelegramApiService = void 0;
|
|
|
4
4
|
const chatContext_1 = require("../entities/context/chatContext");
|
|
5
5
|
const messageContext_1 = require("../entities/context/messageContext");
|
|
6
6
|
const reverseMap_1 = require("../helpers/reverseMap");
|
|
7
|
-
const taskScheduler_1 = require("./taskScheduler");
|
|
8
7
|
const logger_1 = require("./logger");
|
|
8
|
+
const promises_1 = require("timers/promises");
|
|
9
9
|
class TelegramApiService {
|
|
10
10
|
constructor(botName, telegram, storage, chats) {
|
|
11
|
+
this.isFlushing = false;
|
|
11
12
|
this.messageQueue = [];
|
|
12
13
|
this.telegram = telegram;
|
|
13
14
|
this.botName = botName;
|
|
14
15
|
this.chats = (0, reverseMap_1.reverseMap)(chats);
|
|
15
16
|
this.storage = storage;
|
|
16
|
-
taskScheduler_1.Scheduler.createTask('MessageSending', () => {
|
|
17
|
-
this.dequeueResponse();
|
|
18
|
-
}, 100, false, this.botName);
|
|
19
17
|
}
|
|
20
|
-
async
|
|
21
|
-
|
|
22
|
-
if (!message)
|
|
18
|
+
async flushResponses() {
|
|
19
|
+
if (this.isFlushing)
|
|
23
20
|
return;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
21
|
+
while (this.messageQueue.length) {
|
|
22
|
+
const message = this.messageQueue.pop();
|
|
23
|
+
if (!message)
|
|
24
|
+
break;
|
|
25
|
+
try {
|
|
26
|
+
await this.processResponse(message);
|
|
27
|
+
await (0, promises_1.setTimeout)(100);
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
logger_1.Logger.errorWithTraceId(this.botName, message.traceId, this.chats.get(message.chatId), error, message);
|
|
31
|
+
}
|
|
29
32
|
}
|
|
33
|
+
this.isFlushing = false;
|
|
30
34
|
}
|
|
31
35
|
async pinIfShould(response, sentMessage) {
|
|
32
36
|
if (response.shouldPin) {
|
|
@@ -78,8 +82,8 @@ class TelegramApiService {
|
|
|
78
82
|
break;
|
|
79
83
|
}
|
|
80
84
|
}
|
|
81
|
-
enqueue(
|
|
82
|
-
this.messageQueue.push(
|
|
85
|
+
enqueue(response) {
|
|
86
|
+
this.messageQueue.push(response);
|
|
83
87
|
}
|
|
84
88
|
getInteractions() {
|
|
85
89
|
return {
|
package/entities/botInstance.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import { Telegraf } from 'telegraf';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
secondsToMilliseconds
|
|
5
|
-
} from '../helpers/timeConvertions';
|
|
6
|
-
import { Hours, Seconds } from '../types/timeValues';
|
|
2
|
+
import { hoursToMilliseconds } from '../helpers/timeConvertions';
|
|
3
|
+
import { Hours } from '../types/timeValues';
|
|
7
4
|
import { IStorageClient } from '../types/storage';
|
|
8
5
|
import { JsonFileStorage } from '../services/jsonFileStorage';
|
|
9
6
|
import { TelegramApiService } from '../services/telegramApi';
|
|
@@ -21,7 +18,6 @@ export class BotInstance {
|
|
|
21
18
|
private commands: CommandAction<IActionState>[];
|
|
22
19
|
private scheduled: ScheduledAction<IActionState>[];
|
|
23
20
|
private chats: Map<string, number>;
|
|
24
|
-
private messageQueue: IncomingMessage[] = [];
|
|
25
21
|
storage: IStorageClient;
|
|
26
22
|
|
|
27
23
|
constructor(options: {
|
|
@@ -96,21 +92,9 @@ export class BotInstance {
|
|
|
96
92
|
);
|
|
97
93
|
|
|
98
94
|
if (msg.text) {
|
|
99
|
-
this.
|
|
95
|
+
await this.processMessage(msg);
|
|
100
96
|
}
|
|
101
97
|
});
|
|
102
|
-
|
|
103
|
-
Scheduler.createTask(
|
|
104
|
-
'MessageProcessing',
|
|
105
|
-
async () => {
|
|
106
|
-
while (this.messageQueue.length > 0) {
|
|
107
|
-
await this.processMessages();
|
|
108
|
-
}
|
|
109
|
-
},
|
|
110
|
-
secondsToMilliseconds(0.3 as Seconds),
|
|
111
|
-
false,
|
|
112
|
-
this.name
|
|
113
|
-
);
|
|
114
98
|
}
|
|
115
99
|
}
|
|
116
100
|
|
|
@@ -146,9 +130,7 @@ export class BotInstance {
|
|
|
146
130
|
}
|
|
147
131
|
}
|
|
148
132
|
|
|
149
|
-
private async
|
|
150
|
-
const msg = this.messageQueue.pop()!;
|
|
151
|
-
|
|
133
|
+
private async processMessage(msg: IncomingMessage) {
|
|
152
134
|
for (const cmd of this.commands) {
|
|
153
135
|
const ctx = this.api.createContextForMessage(msg, cmd.key);
|
|
154
136
|
|
|
@@ -164,5 +146,7 @@ export class BotInstance {
|
|
|
164
146
|
);
|
|
165
147
|
}
|
|
166
148
|
}
|
|
149
|
+
|
|
150
|
+
this.api.flushResponses();
|
|
167
151
|
}
|
|
168
152
|
}
|
package/package.json
CHANGED
package/services/telegramApi.ts
CHANGED
|
@@ -3,8 +3,6 @@ import { ChatContext } from '../entities/context/chatContext';
|
|
|
3
3
|
import { MessageContext } from '../entities/context/messageContext';
|
|
4
4
|
import { reverseMap } from '../helpers/reverseMap';
|
|
5
5
|
import { IStorageClient } from '../types/storage';
|
|
6
|
-
import { Milliseconds } from '../types/timeValues';
|
|
7
|
-
import { Scheduler } from './taskScheduler';
|
|
8
6
|
import { Logger } from './logger';
|
|
9
7
|
import { Reaction } from '../entities/responses/reaction';
|
|
10
8
|
import { IncomingMessage } from '../entities/incomingMessage';
|
|
@@ -14,12 +12,16 @@ import { TextMessage } from '../entities/responses/textMessage';
|
|
|
14
12
|
import { VideoMessage } from '../entities/responses/videoMessage';
|
|
15
13
|
import { ImageMessage } from '../entities/responses/imageMessage';
|
|
16
14
|
import { Telegram } from 'telegraf/typings/telegram';
|
|
15
|
+
import { setTimeout } from 'timers/promises';
|
|
16
|
+
import { Milliseconds } from '../types/timeValues';
|
|
17
17
|
|
|
18
18
|
export class TelegramApiService {
|
|
19
|
+
isFlushing = false;
|
|
20
|
+
messageQueue: Array<BotResponse> = [];
|
|
21
|
+
|
|
19
22
|
botName: string;
|
|
20
23
|
telegram: Telegram;
|
|
21
24
|
chats: Map<number, string>;
|
|
22
|
-
messageQueue: Array<BotResponse> = [];
|
|
23
25
|
storage: IStorageClient;
|
|
24
26
|
|
|
25
27
|
constructor(
|
|
@@ -32,34 +34,31 @@ export class TelegramApiService {
|
|
|
32
34
|
this.botName = botName;
|
|
33
35
|
this.chats = reverseMap(chats);
|
|
34
36
|
this.storage = storage;
|
|
35
|
-
|
|
36
|
-
Scheduler.createTask(
|
|
37
|
-
'MessageSending',
|
|
38
|
-
() => {
|
|
39
|
-
this.dequeueResponse();
|
|
40
|
-
},
|
|
41
|
-
100 as Milliseconds,
|
|
42
|
-
false,
|
|
43
|
-
this.botName
|
|
44
|
-
);
|
|
45
37
|
}
|
|
46
38
|
|
|
47
|
-
|
|
48
|
-
|
|
39
|
+
async flushResponses() {
|
|
40
|
+
if (this.isFlushing) return;
|
|
49
41
|
|
|
50
|
-
|
|
42
|
+
while (this.messageQueue.length) {
|
|
43
|
+
const message = this.messageQueue.pop();
|
|
51
44
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
45
|
+
if (!message) break;
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
await this.processResponse(message);
|
|
49
|
+
await setTimeout(100 as Milliseconds);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
Logger.errorWithTraceId(
|
|
52
|
+
this.botName,
|
|
53
|
+
message.traceId,
|
|
54
|
+
this.chats.get(message.chatId)!,
|
|
55
|
+
error,
|
|
56
|
+
message
|
|
57
|
+
);
|
|
58
|
+
}
|
|
62
59
|
}
|
|
60
|
+
|
|
61
|
+
this.isFlushing = false;
|
|
63
62
|
}
|
|
64
63
|
|
|
65
64
|
private async pinIfShould<T>(
|
|
@@ -158,8 +157,8 @@ export class TelegramApiService {
|
|
|
158
157
|
}
|
|
159
158
|
}
|
|
160
159
|
|
|
161
|
-
private enqueue(
|
|
162
|
-
this.messageQueue.push(
|
|
160
|
+
private enqueue(response: BotResponse) {
|
|
161
|
+
this.messageQueue.push(response);
|
|
163
162
|
}
|
|
164
163
|
|
|
165
164
|
private getInteractions() {
|