chz-telegram-bot 0.0.30 → 0.0.32
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 +21 -19
- package/dist/entities/botInstance.d.ts.map +1 -1
- package/dist/entities/botInstance.js +1 -1
- package/dist/main.d.ts +3 -0
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +2 -1
- package/entities/actions/commandAction.ts +25 -22
- package/entities/botInstance.ts +1 -2
- package/main.ts +5 -1
- 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,CAAC,CAAC;IAC1C,gBAAgB,EAAE,MAAM,YAAY,CAA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C,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;CAsDvB"}
|
|
@@ -54,31 +54,33 @@ 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
|
+
if (!isUserAllowed)
|
|
58
|
+
return commandTriggerCheckResult_1.CommandTriggerCheckResult.DontTriggerAndSkipCooldown;
|
|
57
59
|
const lastExecutedDate = (0, moment_1.default)(state.lastExecutedDate);
|
|
58
60
|
const cooldownInMilliseconds = (0, timeConvertions_1.secondsToMilliseconds)(this.cooldownInSeconds);
|
|
59
|
-
const
|
|
60
|
-
if (
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
61
|
+
const onCooldown = (0, moment_1.default)().diff(lastExecutedDate) < cooldownInMilliseconds;
|
|
62
|
+
if (onCooldown)
|
|
63
|
+
return commandTriggerCheckResult_1.CommandTriggerCheckResult.DoNotTrigger;
|
|
64
|
+
if (typeof trigger == 'string') {
|
|
65
|
+
shouldTrigger = ctx.messageText.toLowerCase() == trigger;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
trigger.lastIndex = 0;
|
|
69
|
+
const execResult = trigger.exec(ctx.messageText);
|
|
70
|
+
if (execResult != null) {
|
|
71
|
+
matchResults.push(execResult);
|
|
72
|
+
if (trigger.global) {
|
|
73
|
+
while (true) {
|
|
74
|
+
const nextResult = trigger.exec(ctx.messageText);
|
|
75
|
+
if (nextResult == null)
|
|
76
|
+
break;
|
|
77
|
+
matchResults.push(nextResult);
|
|
76
78
|
}
|
|
77
79
|
}
|
|
78
|
-
shouldTrigger = matchResults.length > 0;
|
|
79
80
|
}
|
|
81
|
+
shouldTrigger = matchResults.length > 0;
|
|
80
82
|
}
|
|
81
|
-
return new commandTriggerCheckResult_1.CommandTriggerCheckResult(shouldTrigger, matchResults,
|
|
83
|
+
return new commandTriggerCheckResult_1.CommandTriggerCheckResult(shouldTrigger, matchResults, false);
|
|
82
84
|
}
|
|
83
85
|
}
|
|
84
86
|
exports.CommandAction = CommandAction;
|
|
@@ -1 +1 @@
|
|
|
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,CAA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AC;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;IA4CrC,OAAO,CAAC,2BAA2B;IAsB7B,IAAI,CAAC,IAAI,EAAE,MAAM;YAYT,YAAY;YAyBZ,cAAc;CAmB/B"}
|
|
@@ -33,13 +33,13 @@ class BotInstance {
|
|
|
33
33
|
initializeScheduledProcessing(period) {
|
|
34
34
|
if (this.scheduled.length > 0) {
|
|
35
35
|
const now = (0, moment_1.default)();
|
|
36
|
-
let nextExecutionTime = now.clone().startOf('hour');
|
|
37
36
|
if (now.minute() == 0 && now.second() == 0) {
|
|
38
37
|
taskScheduler_1.Scheduler.createTask('ScheduledProcessing', async () => {
|
|
39
38
|
await this.runScheduled();
|
|
40
39
|
}, (0, timeConvertions_1.secondsToMilliseconds)(period), true, this.name);
|
|
41
40
|
return;
|
|
42
41
|
}
|
|
42
|
+
let nextExecutionTime = now.clone().startOf('hour');
|
|
43
43
|
if (now.minute() > 0 || now.second() > 0) {
|
|
44
44
|
nextExecutionTime = nextExecutionTime.add(1, 'hour');
|
|
45
45
|
}
|
package/dist/main.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { CommandAction } from './entities/actions/commandAction';
|
|
|
3
3
|
import { ScheduledAction } from './entities/actions/scheduledAction';
|
|
4
4
|
import { IActionState } from './types/actionState';
|
|
5
5
|
import { BotInstance } from './entities/botInstance';
|
|
6
|
+
import { Seconds } from './types/timeValues';
|
|
6
7
|
/**
|
|
7
8
|
* Starts bot
|
|
8
9
|
*/
|
|
@@ -21,6 +22,8 @@ declare function startBot(options: {
|
|
|
21
22
|
storageClient?: IStorageClient;
|
|
22
23
|
/** 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
24
|
storagePath?: string;
|
|
25
|
+
/** Period of time between execution of scheduled actions. */
|
|
26
|
+
scheduledPeriod?: Seconds;
|
|
24
27
|
}): Promise<BotInstance>;
|
|
25
28
|
/**
|
|
26
29
|
* Terminates all scheduled tasks, closes storage connections and stops all bots.
|
package/dist/main.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../main.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEjD,OAAO,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAC;AACjE,OAAO,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AACrE,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../main.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEjD,OAAO,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAC;AACjE,OAAO,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AACrE,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAQ7C;;GAEG;AACH,iBAAe,QAAQ,CAAC,OAAO,EAAE;IAC7B,gCAAgC;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,kDAAkD;IAClD,aAAa,EAAE,MAAM,CAAC;IACtB,2HAA2H;IAC3H,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,CAAC;IACxC,mGAAmG;IACnG,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;IAC3C,yGAAyG;IACzG,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9B,qGAAqG;IACrG,aAAa,CAAC,EAAE,cAAc,CAAC;IAC/B,2KAA2K;IAC3K,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,6DAA6D;IAC7D,eAAe,CAAC,EAAE,OAAO,CAAC;CAC7B,wBAeA;AAED;;GAEG;AACH,iBAAe,QAAQ,CAAC,MAAM,EAAE,MAAM,iBASrC;AAED,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC"}
|
package/dist/main.js
CHANGED
|
@@ -22,7 +22,8 @@ async function startBot(options) {
|
|
|
22
22
|
scheduled: options.scheduled,
|
|
23
23
|
chats: options.chats,
|
|
24
24
|
storageClient: options.storageClient,
|
|
25
|
-
storagePath: options.storagePath
|
|
25
|
+
storagePath: options.storagePath,
|
|
26
|
+
scheduledPeriod: options.scheduledPeriod
|
|
26
27
|
});
|
|
27
28
|
bots.push(bot);
|
|
28
29
|
return bot;
|
|
@@ -112,40 +112,43 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
112
112
|
this.allowedUsers.length == 0 ||
|
|
113
113
|
this.allowedUsers.includes(ctx.fromUserId);
|
|
114
114
|
|
|
115
|
+
if (!isUserAllowed)
|
|
116
|
+
return CommandTriggerCheckResult.DontTriggerAndSkipCooldown;
|
|
117
|
+
|
|
115
118
|
const lastExecutedDate = moment(state.lastExecutedDate);
|
|
116
119
|
const cooldownInMilliseconds = secondsToMilliseconds(
|
|
117
120
|
this.cooldownInSeconds
|
|
118
121
|
);
|
|
119
|
-
const
|
|
120
|
-
moment().diff(lastExecutedDate)
|
|
121
|
-
|
|
122
|
-
if (
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
122
|
+
const onCooldown =
|
|
123
|
+
moment().diff(lastExecutedDate) < cooldownInMilliseconds;
|
|
124
|
+
|
|
125
|
+
if (onCooldown) return CommandTriggerCheckResult.DoNotTrigger;
|
|
126
|
+
|
|
127
|
+
if (typeof trigger == 'string') {
|
|
128
|
+
shouldTrigger = ctx.messageText.toLowerCase() == trigger;
|
|
129
|
+
} else {
|
|
130
|
+
trigger.lastIndex = 0;
|
|
131
|
+
|
|
132
|
+
const execResult = trigger.exec(ctx.messageText);
|
|
133
|
+
if (execResult != null) {
|
|
134
|
+
matchResults.push(execResult);
|
|
135
|
+
|
|
136
|
+
if (trigger.global) {
|
|
137
|
+
while (true) {
|
|
138
|
+
const nextResult = trigger.exec(ctx.messageText);
|
|
139
|
+
if (nextResult == null) break;
|
|
140
|
+
matchResults.push(nextResult);
|
|
138
141
|
}
|
|
139
142
|
}
|
|
140
|
-
|
|
141
|
-
shouldTrigger = matchResults.length > 0;
|
|
142
143
|
}
|
|
144
|
+
|
|
145
|
+
shouldTrigger = matchResults.length > 0;
|
|
143
146
|
}
|
|
144
147
|
|
|
145
148
|
return new CommandTriggerCheckResult(
|
|
146
149
|
shouldTrigger,
|
|
147
150
|
matchResults,
|
|
148
|
-
|
|
151
|
+
false
|
|
149
152
|
);
|
|
150
153
|
}
|
|
151
154
|
}
|
package/entities/botInstance.ts
CHANGED
|
@@ -72,8 +72,6 @@ export class BotInstance {
|
|
|
72
72
|
if (this.scheduled.length > 0) {
|
|
73
73
|
const now = moment();
|
|
74
74
|
|
|
75
|
-
let nextExecutionTime = now.clone().startOf('hour');
|
|
76
|
-
|
|
77
75
|
if (now.minute() == 0 && now.second() == 0) {
|
|
78
76
|
Scheduler.createTask(
|
|
79
77
|
'ScheduledProcessing',
|
|
@@ -88,6 +86,7 @@ export class BotInstance {
|
|
|
88
86
|
return;
|
|
89
87
|
}
|
|
90
88
|
|
|
89
|
+
let nextExecutionTime = now.clone().startOf('hour');
|
|
91
90
|
if (now.minute() > 0 || now.second() > 0) {
|
|
92
91
|
nextExecutionTime = nextExecutionTime.add(1, 'hour');
|
|
93
92
|
}
|
package/main.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { ScheduledAction } from './entities/actions/scheduledAction';
|
|
|
6
6
|
import { IActionState } from './types/actionState';
|
|
7
7
|
import { Scheduler } from './services/taskScheduler';
|
|
8
8
|
import { BotInstance } from './entities/botInstance';
|
|
9
|
+
import { Seconds } from './types/timeValues';
|
|
9
10
|
|
|
10
11
|
const bots: BotInstance[] = [];
|
|
11
12
|
|
|
@@ -31,6 +32,8 @@ async function startBot(options: {
|
|
|
31
32
|
storageClient?: IStorageClient;
|
|
32
33
|
/** 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.*/
|
|
33
34
|
storagePath?: string;
|
|
35
|
+
/** Period of time between execution of scheduled actions. */
|
|
36
|
+
scheduledPeriod?: Seconds;
|
|
34
37
|
}) {
|
|
35
38
|
const token = await readFile(options.tokenFilePath, 'utf8');
|
|
36
39
|
const bot = new BotInstance({
|
|
@@ -40,7 +43,8 @@ async function startBot(options: {
|
|
|
40
43
|
scheduled: options.scheduled,
|
|
41
44
|
chats: options.chats,
|
|
42
45
|
storageClient: options.storageClient,
|
|
43
|
-
storagePath: options.storagePath
|
|
46
|
+
storagePath: options.storagePath,
|
|
47
|
+
scheduledPeriod: options.scheduledPeriod
|
|
44
48
|
});
|
|
45
49
|
bots.push(bot);
|
|
46
50
|
|