chz-telegram-bot 0.0.28 → 0.0.30
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.map +1 -1
- package/dist/entities/actions/commandAction.js +2 -2
- package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
- package/dist/entities/actions/scheduledAction.js +8 -3
- package/dist/entities/botInstance.d.ts +2 -0
- package/dist/entities/botInstance.d.ts.map +1 -1
- package/dist/entities/botInstance.js +4 -4
- package/entities/actions/commandAction.ts +3 -2
- package/entities/actions/scheduledAction.ts +9 -5
- package/entities/botInstance.ts +12 -6
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commandAction.d.ts","sourceRoot":"","sources":["../../../entities/actions/commandAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAGjD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAE1E,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAI3D,qBAAa,aAAa,CAAC,YAAY,SAAS,YAAY,CACxD,YAAW,gBAAgB;IAE3B,QAAQ,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IAC9B,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,CAAC;IACtC,IAAI,EAAE,MAAM,CAAC;IACb,iBAAiB,EAAE,OAAO,CAAC;IAC3B,MAAM,EAAE,OAAO,CAAC;IAChB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,SAAS,EAAE,gBAAgB,CAAC,YAAY,CA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AAC;IACvB,SAAS,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;IAC1C,gBAAgB,EAAE,MAAM,YAAY,CAAC;IACrC,GAAG,EAAE,SAAS,CAAC;gBAGX,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,EAAE,EAC9C,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,EACrC,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,OAAO,EACf,QAAQ,EAAE,OAAO,EACjB,cAAc,EAAE,MAAM,EAAE,EACxB,YAAY,EAAE,MAAM,EAAE,EACtB,SAAS,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACzC,gBAAgB,EAAE,MAAM,YAAY;IAelC,IAAI,CAAC,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC;IAgD5C,OAAO,CAAC,YAAY;CAmDvB"}
|
|
@@ -54,9 +54,9 @@ class CommandAction {
|
|
|
54
54
|
return commandTriggerCheckResult_1.CommandTriggerCheckResult.DontTriggerAndSkipCooldown;
|
|
55
55
|
const isUserAllowed = this.allowedUsers.length == 0 ||
|
|
56
56
|
this.allowedUsers.includes(ctx.fromUserId);
|
|
57
|
+
const lastExecutedDate = (0, moment_1.default)(state.lastExecutedDate);
|
|
57
58
|
const cooldownInMilliseconds = (0, timeConvertions_1.secondsToMilliseconds)(this.cooldownInSeconds);
|
|
58
|
-
const notOnCooldown = (0, moment_1.default)().
|
|
59
|
-
cooldownInMilliseconds;
|
|
59
|
+
const notOnCooldown = (0, moment_1.default)().diff(lastExecutedDate) >= cooldownInMilliseconds;
|
|
60
60
|
if (isUserAllowed && notOnCooldown) {
|
|
61
61
|
if (typeof trigger == 'string') {
|
|
62
62
|
shouldTrigger = ctx.messageText.toLowerCase() == trigger;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scheduledAction.d.ts","sourceRoot":"","sources":["../../../entities/actions/scheduledAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAA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ldTrigger(state) {
|
|
58
|
-
const
|
|
59
|
-
const
|
|
60
|
-
const
|
|
58
|
+
const startOfToday = (0, moment_1.default)().startOf('day').valueOf();
|
|
59
|
+
const lastExecutedDate = (0, moment_1.default)(state.lastExecutedDate);
|
|
60
|
+
const currentTime = (0, moment_1.default)();
|
|
61
|
+
const scheduledTime = (0, moment_1.default)()
|
|
62
|
+
.startOf('day')
|
|
63
|
+
.add(this.timeinHours, 'hours');
|
|
64
|
+
const isAllowedToTrigger = currentTime.isSameOrAfter(scheduledTime);
|
|
65
|
+
const hasTriggeredToday = lastExecutedDate.isAfter(startOfToday);
|
|
61
66
|
return isAllowedToTrigger && !hasTriggeredToday;
|
|
62
67
|
}
|
|
63
68
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Seconds } from '../types/timeValues';
|
|
1
2
|
import { IStorageClient } from '../types/storage';
|
|
2
3
|
import { IActionState } from '../types/actionState';
|
|
3
4
|
import { CommandAction } from './actions/commandAction';
|
|
@@ -18,6 +19,7 @@ export declare class BotInstance {
|
|
|
18
19
|
chats: Record<string, number>;
|
|
19
20
|
storageClient?: IStorageClient;
|
|
20
21
|
storagePath?: string;
|
|
22
|
+
scheduledPeriod?: Seconds;
|
|
21
23
|
});
|
|
22
24
|
private initializeScheduledProcessing;
|
|
23
25
|
private initializeMessageProcessing;
|
|
@@ -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":"AAKA,OAAO,EAAuB,OAAO,EAAE,MAAM,qBAAqB,CAAC;AACnE,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;AAM5D,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,CAAyB;IACtC,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,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC9B,aAAa,CAAC,EAAE,cAAc,CAAC;QAC/B,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,eAAe,CAAC,EAAE,OAAO,CAAC;KAC7B;IAmCD,OAAO,CAAC,6BAA6B;IA6CrC,OAAO,CAAC,2BAA2B;IAsB7B,IAAI,CAAC,IAAI,EAAE,MAAM;YAYT,YAAY;YAyBZ,cAAc;CAmB/B"}
|
|
@@ -26,18 +26,18 @@ class BotInstance {
|
|
|
26
26
|
new jsonFileStorage_1.JsonFileStorage(options.name, actions, options.storagePath);
|
|
27
27
|
this.api = new telegramApi_1.TelegramApiService(this.name, this.telegraf.telegram, this.storage, this.chats);
|
|
28
28
|
this.initializeMessageProcessing();
|
|
29
|
-
this.initializeScheduledProcessing();
|
|
29
|
+
this.initializeScheduledProcessing(options.scheduledPeriod ?? (0, timeConvertions_1.hoursToSeconds)(1));
|
|
30
30
|
this.storage.saveMetadata(actions, this.name);
|
|
31
31
|
this.telegraf.launch();
|
|
32
32
|
}
|
|
33
|
-
initializeScheduledProcessing() {
|
|
33
|
+
initializeScheduledProcessing(period) {
|
|
34
34
|
if (this.scheduled.length > 0) {
|
|
35
35
|
const now = (0, moment_1.default)();
|
|
36
36
|
let nextExecutionTime = now.clone().startOf('hour');
|
|
37
37
|
if (now.minute() == 0 && now.second() == 0) {
|
|
38
38
|
taskScheduler_1.Scheduler.createTask('ScheduledProcessing', async () => {
|
|
39
39
|
await this.runScheduled();
|
|
40
|
-
}, (0, timeConvertions_1.
|
|
40
|
+
}, (0, timeConvertions_1.secondsToMilliseconds)(period), true, this.name);
|
|
41
41
|
return;
|
|
42
42
|
}
|
|
43
43
|
if (now.minute() > 0 || now.second() > 0) {
|
|
@@ -47,7 +47,7 @@ class BotInstance {
|
|
|
47
47
|
taskScheduler_1.Scheduler.createOnetimeTask('ScheduledProcessing_OneTime', async () => {
|
|
48
48
|
taskScheduler_1.Scheduler.createTask('ScheduledProcessing', async () => {
|
|
49
49
|
await this.runScheduled();
|
|
50
|
-
}, (0, timeConvertions_1.
|
|
50
|
+
}, (0, timeConvertions_1.secondsToMilliseconds)(period), true, this.name);
|
|
51
51
|
}, delay, this.name);
|
|
52
52
|
}
|
|
53
53
|
}
|
|
@@ -111,12 +111,13 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
111
111
|
const isUserAllowed =
|
|
112
112
|
this.allowedUsers.length == 0 ||
|
|
113
113
|
this.allowedUsers.includes(ctx.fromUserId);
|
|
114
|
+
|
|
115
|
+
const lastExecutedDate = moment(state.lastExecutedDate);
|
|
114
116
|
const cooldownInMilliseconds = secondsToMilliseconds(
|
|
115
117
|
this.cooldownInSeconds
|
|
116
118
|
);
|
|
117
119
|
const notOnCooldown =
|
|
118
|
-
moment().
|
|
119
|
-
cooldownInMilliseconds;
|
|
120
|
+
moment().diff(lastExecutedDate) >= cooldownInMilliseconds;
|
|
120
121
|
|
|
121
122
|
if (isUserAllowed && notOnCooldown) {
|
|
122
123
|
if (typeof trigger == 'string') {
|
|
@@ -119,11 +119,15 @@ export class ScheduledAction<TActionState extends IActionState>
|
|
|
119
119
|
}
|
|
120
120
|
|
|
121
121
|
private shouldTrigger(state: IActionState): boolean {
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
122
|
+
const startOfToday = moment().startOf('day').valueOf();
|
|
123
|
+
const lastExecutedDate = moment(state.lastExecutedDate);
|
|
124
|
+
const currentTime = moment();
|
|
125
|
+
const scheduledTime = moment()
|
|
126
|
+
.startOf('day')
|
|
127
|
+
.add(this.timeinHours, 'hours');
|
|
128
|
+
|
|
129
|
+
const isAllowedToTrigger = currentTime.isSameOrAfter(scheduledTime);
|
|
130
|
+
const hasTriggeredToday = lastExecutedDate.isAfter(startOfToday);
|
|
127
131
|
|
|
128
132
|
return isAllowedToTrigger && !hasTriggeredToday;
|
|
129
133
|
}
|
package/entities/botInstance.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { Telegraf } from 'telegraf';
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
hoursToSeconds,
|
|
4
|
+
secondsToMilliseconds
|
|
5
|
+
} from '../helpers/timeConvertions';
|
|
6
|
+
import { Hours, Milliseconds, Seconds } from '../types/timeValues';
|
|
4
7
|
import { IStorageClient } from '../types/storage';
|
|
5
8
|
import { JsonFileStorage } from '../services/jsonFileStorage';
|
|
6
9
|
import { TelegramApiService } from '../services/telegramApi';
|
|
@@ -29,6 +32,7 @@ export class BotInstance {
|
|
|
29
32
|
chats: Record<string, number>;
|
|
30
33
|
storageClient?: IStorageClient;
|
|
31
34
|
storagePath?: string;
|
|
35
|
+
scheduledPeriod?: Seconds;
|
|
32
36
|
}) {
|
|
33
37
|
this.name = options.name;
|
|
34
38
|
this.commands = options.commands;
|
|
@@ -55,14 +59,16 @@ export class BotInstance {
|
|
|
55
59
|
);
|
|
56
60
|
|
|
57
61
|
this.initializeMessageProcessing();
|
|
58
|
-
this.initializeScheduledProcessing(
|
|
62
|
+
this.initializeScheduledProcessing(
|
|
63
|
+
options.scheduledPeriod ?? hoursToSeconds(1 as Hours)
|
|
64
|
+
);
|
|
59
65
|
|
|
60
66
|
this.storage.saveMetadata(actions, this.name);
|
|
61
67
|
|
|
62
68
|
this.telegraf.launch();
|
|
63
69
|
}
|
|
64
70
|
|
|
65
|
-
private initializeScheduledProcessing() {
|
|
71
|
+
private initializeScheduledProcessing(period: Seconds) {
|
|
66
72
|
if (this.scheduled.length > 0) {
|
|
67
73
|
const now = moment();
|
|
68
74
|
|
|
@@ -74,7 +80,7 @@ export class BotInstance {
|
|
|
74
80
|
async () => {
|
|
75
81
|
await this.runScheduled();
|
|
76
82
|
},
|
|
77
|
-
|
|
83
|
+
secondsToMilliseconds(period),
|
|
78
84
|
true,
|
|
79
85
|
this.name
|
|
80
86
|
);
|
|
@@ -96,7 +102,7 @@ export class BotInstance {
|
|
|
96
102
|
async () => {
|
|
97
103
|
await this.runScheduled();
|
|
98
104
|
},
|
|
99
|
-
|
|
105
|
+
secondsToMilliseconds(period),
|
|
100
106
|
true,
|
|
101
107
|
this.name
|
|
102
108
|
);
|