chz-telegram-bot 0.1.10 → 0.1.12
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 +2 -2
- package/dist/entities/actions/commandAction.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.js +2 -2
- package/dist/entities/botInstance.d.ts +24 -5
- package/dist/entities/botInstance.d.ts.map +1 -1
- package/dist/entities/botInstance.js +19 -13
- package/dist/helpers/builders/commandActionBuilder.d.ts +2 -2
- package/dist/helpers/builders/commandActionBuilder.d.ts.map +1 -1
- package/dist/helpers/builders/commandActionBuilder.js +4 -4
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +4 -2
- package/entities/actions/commandAction.ts +3 -3
- package/entities/botInstance.ts +54 -25
- package/eslint.config.js +14 -0
- package/helpers/builders/commandActionBuilder.ts +4 -4
- package/main.ts +4 -2
- package/package.json +1 -1
|
@@ -16,8 +16,8 @@ export declare class CommandAction<TActionState extends IActionState> implements
|
|
|
16
16
|
readonly condition: CommandCondition<TActionState>;
|
|
17
17
|
readonly stateConstructor: () => TActionState;
|
|
18
18
|
readonly key: ActionKey;
|
|
19
|
-
readonly
|
|
20
|
-
constructor(trigger: CommandTrigger | CommandTrigger[], handler: CommandHandler<TActionState>, name: string, active: boolean, cooldown: Seconds, chatsBlacklist: number[], allowedUsers: number[], condition: CommandCondition<TActionState>, stateConstructor: () => TActionState,
|
|
19
|
+
readonly readmeFactory: (botName: string) => string;
|
|
20
|
+
constructor(trigger: CommandTrigger | CommandTrigger[], handler: CommandHandler<TActionState>, name: string, active: boolean, cooldown: Seconds, chatsBlacklist: number[], allowedUsers: number[], condition: CommandCondition<TActionState>, stateConstructor: () => TActionState, readmeFactory: (botName: string) => string);
|
|
21
21
|
exec(ctx: MessageContext<TActionState>): Promise<import("../../types/response").BotResponse[]>;
|
|
22
22
|
private checkIfShouldBeExecuted;
|
|
23
23
|
private checkTrigger;
|
|
@@ -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,4BAA4B,CAAC;AAEzE,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAG5D,qBAAa,aAAa,CAAC,YAAY,SAAS,YAAY,CACxD,YAAW,gBAAgB,CAAC,YAAY,CAAC;IAEzC,QAAQ,CAAC,QAAQ,EAAE,cAAc,EAAE,CAAC;IACpC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,CAAC;IAC/C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,iBAAiB,EAAE,OAAO,CAAC;IACpC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,cAAc,EAAE,MAAM,EAAE,CAAC;IAClC,QAAQ,CAAC,YAAY,EAAE,MAAM,EAAE,CAAC;IAChC,QAAQ,CAAC,SAAS,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;IACnD,QAAQ,CAAC,gBAAgB,EAAE,MAAM,YAAY,CAAC;IAC9C,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;IACxB,QAAQ,CAAC,
|
|
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,gBA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C,gBAAgB,EAAE,MAAM,YAAY,EACpC,aAAa,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM;IAgBxC,IAAI,CAAC,GAAG,EAAE,cAAc,CAAC,YAAY,CAAC;IAkD5C,OAAO,CAAC,uBAAuB;IAqC/B,OAAO,CAAC,YAAY;CAmCvB"}
|
|
@@ -10,7 +10,7 @@ const toArray_1 = require("../../helpers/toArray");
|
|
|
10
10
|
const commandTriggerCheckResult_1 = require("../../dtos/commandTriggerCheckResult");
|
|
11
11
|
const noop_1 = require("../../helpers/noop");
|
|
12
12
|
class CommandAction {
|
|
13
|
-
constructor(trigger, handler, name, active, cooldown, chatsBlacklist, allowedUsers, condition, stateConstructor,
|
|
13
|
+
constructor(trigger, handler, name, active, cooldown, chatsBlacklist, allowedUsers, condition, stateConstructor, readmeFactory) {
|
|
14
14
|
this.triggers = (0, toArray_1.toArray)(trigger);
|
|
15
15
|
this.handler = handler;
|
|
16
16
|
this.name = name;
|
|
@@ -20,7 +20,7 @@ class CommandAction {
|
|
|
20
20
|
this.allowedUsers = allowedUsers;
|
|
21
21
|
this.condition = condition;
|
|
22
22
|
this.stateConstructor = stateConstructor;
|
|
23
|
-
this.
|
|
23
|
+
this.readmeFactory = readmeFactory;
|
|
24
24
|
this.key = `command:${this.name.replace('.', '-')}`;
|
|
25
25
|
}
|
|
26
26
|
async exec(ctx) {
|
|
@@ -11,12 +11,13 @@ export declare class BotInstance {
|
|
|
11
11
|
private readonly storage;
|
|
12
12
|
private readonly scheduler;
|
|
13
13
|
private readonly logger;
|
|
14
|
-
readonly name: string;
|
|
15
14
|
private readonly telegraf;
|
|
16
|
-
|
|
17
|
-
private
|
|
18
|
-
private
|
|
19
|
-
private
|
|
15
|
+
name: string;
|
|
16
|
+
private botInfo;
|
|
17
|
+
private commands;
|
|
18
|
+
private scheduled;
|
|
19
|
+
private inlineQueries;
|
|
20
|
+
private chats;
|
|
20
21
|
constructor(options: {
|
|
21
22
|
name: string;
|
|
22
23
|
token: string;
|
|
@@ -35,6 +36,24 @@ export declare class BotInstance {
|
|
|
35
36
|
scheduler?: IScheduler;
|
|
36
37
|
};
|
|
37
38
|
});
|
|
39
|
+
start(options: {
|
|
40
|
+
name: string;
|
|
41
|
+
token: string;
|
|
42
|
+
actions: {
|
|
43
|
+
commands: CommandAction<IActionState>[];
|
|
44
|
+
scheduled: ScheduledAction<IActionState>[];
|
|
45
|
+
inlineQueries: InlineQueryAction[];
|
|
46
|
+
};
|
|
47
|
+
chats: Record<string, number>;
|
|
48
|
+
storagePath?: string;
|
|
49
|
+
scheduledPeriod?: Seconds;
|
|
50
|
+
verboseLoggingForIncomingMessage?: boolean;
|
|
51
|
+
services?: {
|
|
52
|
+
storageClient?: IStorageClient;
|
|
53
|
+
logger?: ILogger;
|
|
54
|
+
scheduler?: IScheduler;
|
|
55
|
+
};
|
|
56
|
+
}): Promise<void>;
|
|
38
57
|
private initializeScheduledProcessing;
|
|
39
58
|
private initializeInlineQueryProcessing;
|
|
40
59
|
private initializeMessageProcessing;
|
|
@@ -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;AAO5D,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAGhD,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"botInstance.d.ts","sourceRoot":"","sources":["../../entities/botInstance.ts"],"names":[],"m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AC;QAC1B,gCAAgC,CAAC,EAAE,OAAO,CAAC;QAC3C,QAAQ,CAAC,EAAE;YACP,aAAa,CAAC,EAAE,cAAc,CAAC;YAC/B,MAAM,CAAC,EAAE,OAAO,CAAC;YACjB,SAAS,CAAC,EAAE,UAAU,CAAC;SAC1B,CAAC;KACL;IAwBK,KAAK,CAAC,OAAO,EAAE;QACjB,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC;QACd,OAAO,EAAE;YACL,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,CAAC;YACxC,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;YAC3C,aAAa,EAAE,iBAAiB,EAAE,CAAC;SACtC,CAAC;QACF,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC9B,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,eAAe,CAAC,EAAE,OAAO,CAAC;QAC1B,gCAAgC,CAAC,EAAE,OAAO,CAAC;QAC3C,QAAQ,CAAC,EAAE;YACP,aAAa,CAAC,EAAE,cAAc,CAAC;YAC/B,MAAM,CAAC,EAAE,OAAO,CAAC;YACjB,SAAS,CAAC,EAAE,UAAU,CAAC;SAC1B,CAAC;KACL;IAsCD,OAAO,CAAC,6BAA6B;IA4CrC,OAAO,CAAC,+BAA+B;IA2EvC,OAAO,CAAC,2BAA2B;IAiC7B,IAAI,CAAC,IAAI,EAAE,MAAM;YAaT,YAAY;YAuCZ,cAAc;CA2B/B"}
|
|
@@ -21,12 +21,30 @@ const inlineQueryContext_1 = require("./context/inlineQueryContext");
|
|
|
21
21
|
const helpAction_1 = require("../builtin/helpAction");
|
|
22
22
|
class BotInstance {
|
|
23
23
|
constructor(options) {
|
|
24
|
+
const actions = [
|
|
25
|
+
...options.actions.commands,
|
|
26
|
+
...options.actions.scheduled
|
|
27
|
+
];
|
|
28
|
+
this.telegraf = new telegraf_1.Telegraf(options.token);
|
|
29
|
+
this.logger = options.services?.logger ?? new jsonLogger_1.JsonLogger();
|
|
30
|
+
this.scheduler =
|
|
31
|
+
options.services?.scheduler ??
|
|
32
|
+
new nodeTimeoutScheduler_1.NodeTimeoutScheduler(this.logger);
|
|
33
|
+
this.storage =
|
|
34
|
+
options.services?.storageClient ??
|
|
35
|
+
new jsonFileStorage_1.JsonFileStorage(options.name, actions, options.storagePath);
|
|
36
|
+
this.api = new telegramApi_1.TelegramApiService(this.name, this.telegraf.telegram, this.storage, this.logger);
|
|
37
|
+
this.storage.saveMetadata(actions, this.name);
|
|
38
|
+
}
|
|
39
|
+
async start(options) {
|
|
40
|
+
await this.telegraf.launch();
|
|
41
|
+
this.botInfo = this.telegraf.botInfo;
|
|
24
42
|
this.name = options.name;
|
|
25
43
|
this.commands =
|
|
26
44
|
options.actions.commands.length > 0
|
|
27
45
|
? [
|
|
28
46
|
(0, helpAction_1.buildHelpCommand)(options.actions.commands
|
|
29
|
-
.map((x) => x.
|
|
47
|
+
.map((x) => x.readmeFactory(this.botInfo.username))
|
|
30
48
|
.filter((x) => !!x)),
|
|
31
49
|
...options.actions.commands
|
|
32
50
|
]
|
|
@@ -34,22 +52,10 @@ class BotInstance {
|
|
|
34
52
|
this.scheduled = options.actions.scheduled;
|
|
35
53
|
this.inlineQueries = options.actions.inlineQueries;
|
|
36
54
|
this.chats = options.chats;
|
|
37
|
-
const actions = [...this.commands, ...this.scheduled];
|
|
38
|
-
this.telegraf = new telegraf_1.Telegraf(options.token);
|
|
39
|
-
this.logger = options.services?.logger ?? new jsonLogger_1.JsonLogger();
|
|
40
|
-
this.scheduler =
|
|
41
|
-
options.services?.scheduler ??
|
|
42
|
-
new nodeTimeoutScheduler_1.NodeTimeoutScheduler(this.logger);
|
|
43
|
-
this.storage =
|
|
44
|
-
options.services?.storageClient ??
|
|
45
|
-
new jsonFileStorage_1.JsonFileStorage(options.name, actions, options.storagePath);
|
|
46
|
-
this.api = new telegramApi_1.TelegramApiService(this.name, this.telegraf.telegram, this.storage, this.logger);
|
|
47
55
|
this.initializeMessageProcessing(options.verboseLoggingForIncomingMessage ?? false);
|
|
48
56
|
this.initializeInlineQueryProcessing(1000);
|
|
49
57
|
this.initializeScheduledProcessing(options.scheduledPeriod ?? (0, timeConvertions_1.hoursToSeconds)(1));
|
|
50
|
-
this.storage.saveMetadata(actions, this.name);
|
|
51
58
|
this.logger.logWithTraceId(this.name, (0, traceFactory_1.createTrace)(this, this.name, 'Start'), 'System', 'Starting bot...');
|
|
52
|
-
this.telegraf.launch();
|
|
53
59
|
}
|
|
54
60
|
initializeScheduledProcessing(period) {
|
|
55
61
|
if (this.scheduled.length > 0) {
|
|
@@ -12,7 +12,7 @@ export declare class CommandActionBuilderWithState<TActionState extends IActionS
|
|
|
12
12
|
name: string;
|
|
13
13
|
trigger: CommandTrigger | CommandTrigger[];
|
|
14
14
|
active: boolean;
|
|
15
|
-
|
|
15
|
+
readmeFactory: null | ((botName: string) => string);
|
|
16
16
|
cooldownSeconds: Seconds;
|
|
17
17
|
blacklist: number[];
|
|
18
18
|
allowedUsers: number[];
|
|
@@ -44,7 +44,7 @@ export declare class CommandActionBuilderWithState<TActionState extends IActionS
|
|
|
44
44
|
* @param condition Condition check predicate
|
|
45
45
|
*/
|
|
46
46
|
when(condition: CommandCondition<TActionState>): this;
|
|
47
|
-
withHelp(
|
|
47
|
+
withHelp(readmeFactory: (botName: string) => string): this;
|
|
48
48
|
/** If called during building, action is marked as disabled and never checked. */
|
|
49
49
|
disabled(): this;
|
|
50
50
|
/** Sets action cooldown.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commandActionBuilder.d.ts","sourceRoot":"","sources":["../../../helpers/builders/commandActionBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACjD,OAA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YAAY,CAAC,CAAa;IAClD,SAAS,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAa;IAEtD;;;;OAIG;gBACS,IAAI,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,YAAY;IAK9D;;;;;OAKG;IACH,EAAE,CAAC,OAAO,EAAE,cAAc,GAAG,cAAc,EAAE;IAM7C;;OAEG;IACH,IAAI,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE;IAM1B;;OAEG;IACH,EAAE,CAAC,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC;IAMxC;;OAEG;IACH,IAAI,CAAC,SAAS,EAAE,gBAAgB,CAAC,YAAY,CAAC;IAM9C,QAAQ,CAAC,aAAa,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM;IAMnD,iFAAiF;IACjF,QAAQ;IAMR;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,OAAO;IAMzB;;;OAGG;IACH,UAAU,CAAC,MAAM,EAAE,MAAM;IAMzB,oBAAoB;IACpB,KAAK;CAcR;AAED;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,6BAA6B,CAAC,eAAe,CAAC;IACpF;;OAEG;gBACS,IAAI,EAAE,MAAM;CAG3B"}
|
|
@@ -17,7 +17,7 @@ class CommandActionBuilderWithState {
|
|
|
17
17
|
constructor(name, stateConstructor) {
|
|
18
18
|
this.trigger = [];
|
|
19
19
|
this.active = true;
|
|
20
|
-
this.
|
|
20
|
+
this.readmeFactory = null;
|
|
21
21
|
this.cooldownSeconds = 0;
|
|
22
22
|
this.blacklist = [];
|
|
23
23
|
this.allowedUsers = [];
|
|
@@ -57,8 +57,8 @@ class CommandActionBuilderWithState {
|
|
|
57
57
|
this.condition = condition;
|
|
58
58
|
return this;
|
|
59
59
|
}
|
|
60
|
-
withHelp(
|
|
61
|
-
this.
|
|
60
|
+
withHelp(readmeFactory) {
|
|
61
|
+
this.readmeFactory = readmeFactory;
|
|
62
62
|
return this;
|
|
63
63
|
}
|
|
64
64
|
/** If called during building, action is marked as disabled and never checked. */
|
|
@@ -83,7 +83,7 @@ class CommandActionBuilderWithState {
|
|
|
83
83
|
}
|
|
84
84
|
/** Builds action */
|
|
85
85
|
build() {
|
|
86
|
-
return new commandAction_1.CommandAction(this.trigger, this.handler, this.name, this.active, this.cooldownSeconds, this.blacklist, this.allowedUsers, this.condition, this.stateConstructor, this.
|
|
86
|
+
return new commandAction_1.CommandAction(this.trigger, this.handler, this.name, this.active, this.cooldownSeconds, this.blacklist, this.allowedUsers, this.condition, this.stateConstructor, this.readmeFactory != null ? this.readmeFactory : (_) => '');
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
exports.CommandActionBuilderWithState = CommandActionBuilderWithState;
|
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;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAC;AACjE,OAAO,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AACrE,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACzC,OAAO,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;AACpE,OAAO,EAAE,iBAAiB,EAAE,MAAM,sCAAsC,CAAC;AAEzE,cAAM,eAAe;IACjB,IAAI,EAAE,WAAW,EAAE,CAAM;IAEzB;;OAEG;IACG,QAAQ,CAAC,OAAO,EAAE;QACpB,gCAAgC;QAChC,IAAI,EAAE,MAAM,CAAC;QACb,kDAAkD;QAClD,aAAa,EAAE,MAAM,CAAC;QACtB,OAAO,EAAE;Y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EG;IACG,QAAQ,CAAC,OAAO,EAAE;QACpB,gCAAgC;QAChC,IAAI,EAAE,MAAM,CAAC;QACb,kDAAkD;QAClD,aAAa,EAAE,MAAM,CAAC;QACtB,OAAO,EAAE;YACL,2HAA2H;YAC3H,QAAQ,EAAE,aAAa,CAAC,eAAe,CAAC,EAAE,CAAC;YAC3C,mGAAmG;YACnG,SAAS,EAAE,eAAe,CAAC,eAAe,CAAC,EAAE,CAAC;YAC9C,4DAA4D;YAC5D,aAAa,EAAE,iBAAiB,EAAE,CAAC;SACtC,CAAC;QACF,yGAAyG;QACzG,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC9B,2KAA2K;QAC3K,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,6DAA6D;QAC7D,eAAe,CAAC,EAAE,OAAO,CAAC;QAC1B,+EAA+E;QAC/E,gCAAgC,CAAC,EAAE,OAAO,CAAC;QAC3C,QAAQ,CAAC,EAAE;YACP,qGAAqG;YACrG,aAAa,CAAC,EAAE,cAAc,CAAC;YAC/B,8FAA8F;YAC9F,MAAM,CAAC,EAAE,OAAO,CAAC;YACjB,yGAAyG;YACzG,SAAS,CAAC,EAAE,UAAU,CAAC;SAC1B,CAAC;KACL;IAyBD;;OAEG;IACG,QAAQ,CAAC,MAAM,EAAE,MAAM;CAKhC;AAED,eAAO,MAAM,eAAe,iBAAwB,CAAC"}
|
package/dist/main.js
CHANGED
|
@@ -12,7 +12,7 @@ class BotOrchestrator {
|
|
|
12
12
|
*/
|
|
13
13
|
async startBot(options) {
|
|
14
14
|
const token = await (0, promises_1.readFile)(options.tokenFilePath, 'utf8');
|
|
15
|
-
const
|
|
15
|
+
const remappedOptions = {
|
|
16
16
|
name: options.name,
|
|
17
17
|
token,
|
|
18
18
|
actions: options.actions,
|
|
@@ -25,7 +25,9 @@ class BotOrchestrator {
|
|
|
25
25
|
logger: options.services?.logger,
|
|
26
26
|
scheduler: options.services?.scheduler
|
|
27
27
|
}
|
|
28
|
-
}
|
|
28
|
+
};
|
|
29
|
+
const bot = new botInstance_1.BotInstance(remappedOptions);
|
|
30
|
+
await bot.start(remappedOptions);
|
|
29
31
|
this.bots.push(bot);
|
|
30
32
|
return bot;
|
|
31
33
|
}
|
|
@@ -24,7 +24,7 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
24
24
|
readonly condition: CommandCondition<TActionState>;
|
|
25
25
|
readonly stateConstructor: () => TActionState;
|
|
26
26
|
readonly key: ActionKey;
|
|
27
|
-
readonly
|
|
27
|
+
readonly readmeFactory: (botName: string) => string;
|
|
28
28
|
|
|
29
29
|
constructor(
|
|
30
30
|
trigger: CommandTrigger | CommandTrigger[],
|
|
@@ -36,7 +36,7 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
36
36
|
allowedUsers: number[],
|
|
37
37
|
condition: CommandCondition<TActionState>,
|
|
38
38
|
stateConstructor: () => TActionState,
|
|
39
|
-
|
|
39
|
+
readmeFactory: (botName: string) => string
|
|
40
40
|
) {
|
|
41
41
|
this.triggers = toArray(trigger);
|
|
42
42
|
this.handler = handler;
|
|
@@ -47,7 +47,7 @@ export class CommandAction<TActionState extends IActionState>
|
|
|
47
47
|
this.allowedUsers = allowedUsers;
|
|
48
48
|
this.condition = condition;
|
|
49
49
|
this.stateConstructor = stateConstructor;
|
|
50
|
-
this.
|
|
50
|
+
this.readmeFactory = readmeFactory;
|
|
51
51
|
|
|
52
52
|
this.key = `command:${this.name.replace('.', '-')}` as ActionKey;
|
|
53
53
|
}
|
package/entities/botInstance.ts
CHANGED
|
@@ -24,19 +24,21 @@ import { InlineQueryAction } from './actions/inlineQueryAction';
|
|
|
24
24
|
import { IncomingInlineQuery } from '../dtos/incomingQuery';
|
|
25
25
|
import { InlineQueryContext } from './context/inlineQueryContext';
|
|
26
26
|
import { buildHelpCommand } from '../builtin/helpAction';
|
|
27
|
+
import { UserFromGetMe } from 'telegraf/types';
|
|
27
28
|
|
|
28
29
|
export class BotInstance {
|
|
29
30
|
private readonly api: TelegramApiService;
|
|
30
31
|
private readonly storage: IStorageClient;
|
|
31
32
|
private readonly scheduler: IScheduler;
|
|
32
33
|
private readonly logger: ILogger;
|
|
33
|
-
|
|
34
|
-
readonly name: string;
|
|
35
34
|
private readonly telegraf: Telegraf;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
private
|
|
39
|
-
private
|
|
35
|
+
|
|
36
|
+
name!: string;
|
|
37
|
+
private botInfo!: UserFromGetMe;
|
|
38
|
+
private commands!: CommandAction<IActionState>[];
|
|
39
|
+
private scheduled!: ScheduledAction<IActionState>[];
|
|
40
|
+
private inlineQueries!: InlineQueryAction[];
|
|
41
|
+
private chats!: Record<string, number>;
|
|
40
42
|
|
|
41
43
|
constructor(options: {
|
|
42
44
|
name: string;
|
|
@@ -56,23 +58,11 @@ export class BotInstance {
|
|
|
56
58
|
scheduler?: IScheduler;
|
|
57
59
|
};
|
|
58
60
|
}) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
options.actions.
|
|
62
|
-
|
|
63
|
-
buildHelpCommand(
|
|
64
|
-
options.actions.commands
|
|
65
|
-
.map((x) => x.readme)
|
|
66
|
-
.filter((x) => !!x)
|
|
67
|
-
),
|
|
68
|
-
...options.actions.commands
|
|
69
|
-
]
|
|
70
|
-
: [];
|
|
71
|
-
this.scheduled = options.actions.scheduled;
|
|
72
|
-
this.inlineQueries = options.actions.inlineQueries;
|
|
73
|
-
this.chats = options.chats;
|
|
61
|
+
const actions = [
|
|
62
|
+
...options.actions.commands,
|
|
63
|
+
...options.actions.scheduled
|
|
64
|
+
];
|
|
74
65
|
|
|
75
|
-
const actions = [...this.commands, ...this.scheduled];
|
|
76
66
|
this.telegraf = new Telegraf(options.token);
|
|
77
67
|
this.logger = options.services?.logger ?? new JsonLogger();
|
|
78
68
|
this.scheduler =
|
|
@@ -88,6 +78,48 @@ export class BotInstance {
|
|
|
88
78
|
this.logger
|
|
89
79
|
);
|
|
90
80
|
|
|
81
|
+
this.storage.saveMetadata(actions, this.name);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async start(options: {
|
|
85
|
+
name: string;
|
|
86
|
+
token: string;
|
|
87
|
+
actions: {
|
|
88
|
+
commands: CommandAction<IActionState>[];
|
|
89
|
+
scheduled: ScheduledAction<IActionState>[];
|
|
90
|
+
inlineQueries: InlineQueryAction[];
|
|
91
|
+
};
|
|
92
|
+
chats: Record<string, number>;
|
|
93
|
+
storagePath?: string;
|
|
94
|
+
scheduledPeriod?: Seconds;
|
|
95
|
+
verboseLoggingForIncomingMessage?: boolean;
|
|
96
|
+
services?: {
|
|
97
|
+
storageClient?: IStorageClient;
|
|
98
|
+
logger?: ILogger;
|
|
99
|
+
scheduler?: IScheduler;
|
|
100
|
+
};
|
|
101
|
+
}) {
|
|
102
|
+
await this.telegraf.launch();
|
|
103
|
+
this.botInfo = this.telegraf.botInfo!;
|
|
104
|
+
|
|
105
|
+
this.name = options.name;
|
|
106
|
+
this.commands =
|
|
107
|
+
options.actions.commands.length > 0
|
|
108
|
+
? [
|
|
109
|
+
buildHelpCommand(
|
|
110
|
+
options.actions.commands
|
|
111
|
+
.map((x) =>
|
|
112
|
+
x.readmeFactory(this.botInfo.username)
|
|
113
|
+
)
|
|
114
|
+
.filter((x) => !!x)
|
|
115
|
+
),
|
|
116
|
+
...options.actions.commands
|
|
117
|
+
]
|
|
118
|
+
: [];
|
|
119
|
+
this.scheduled = options.actions.scheduled;
|
|
120
|
+
this.inlineQueries = options.actions.inlineQueries;
|
|
121
|
+
this.chats = options.chats;
|
|
122
|
+
|
|
91
123
|
this.initializeMessageProcessing(
|
|
92
124
|
options.verboseLoggingForIncomingMessage ?? false
|
|
93
125
|
);
|
|
@@ -96,15 +128,12 @@ export class BotInstance {
|
|
|
96
128
|
options.scheduledPeriod ?? hoursToSeconds(1 as Hours)
|
|
97
129
|
);
|
|
98
130
|
|
|
99
|
-
this.storage.saveMetadata(actions, this.name);
|
|
100
|
-
|
|
101
131
|
this.logger.logWithTraceId(
|
|
102
132
|
this.name,
|
|
103
133
|
createTrace(this, this.name, 'Start'),
|
|
104
134
|
'System',
|
|
105
135
|
'Starting bot...'
|
|
106
136
|
);
|
|
107
|
-
this.telegraf.launch();
|
|
108
137
|
}
|
|
109
138
|
|
|
110
139
|
private initializeScheduledProcessing(period: Seconds) {
|
package/eslint.config.js
CHANGED
|
@@ -5,6 +5,20 @@ export default tseslint.config(
|
|
|
5
5
|
eslint.configs.recommended,
|
|
6
6
|
...tseslint.configs.recommended,
|
|
7
7
|
{
|
|
8
|
+
rules: {
|
|
9
|
+
'@typescript-eslint/no-unused-vars': [
|
|
10
|
+
'error',
|
|
11
|
+
{
|
|
12
|
+
args: 'all',
|
|
13
|
+
argsIgnorePattern: '^_',
|
|
14
|
+
caughtErrors: 'all',
|
|
15
|
+
caughtErrorsIgnorePattern: '^_',
|
|
16
|
+
destructuredArrayIgnorePattern: '^_',
|
|
17
|
+
varsIgnorePattern: '^_',
|
|
18
|
+
ignoreRestSiblings: true
|
|
19
|
+
}
|
|
20
|
+
]
|
|
21
|
+
},
|
|
8
22
|
ignores: ['dist/*']
|
|
9
23
|
}
|
|
10
24
|
);
|
|
@@ -16,7 +16,7 @@ export class CommandActionBuilderWithState<TActionState extends IActionState> {
|
|
|
16
16
|
trigger: CommandTrigger | CommandTrigger[] = [];
|
|
17
17
|
|
|
18
18
|
active = true;
|
|
19
|
-
|
|
19
|
+
readmeFactory: null | ((botName: string) => string) = null;
|
|
20
20
|
cooldownSeconds: Seconds = 0 as Seconds;
|
|
21
21
|
blacklist: number[] = [];
|
|
22
22
|
allowedUsers: number[] = [];
|
|
@@ -73,8 +73,8 @@ export class CommandActionBuilderWithState<TActionState extends IActionState> {
|
|
|
73
73
|
return this;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
withHelp(
|
|
77
|
-
this.
|
|
76
|
+
withHelp(readmeFactory: (botName: string) => string) {
|
|
77
|
+
this.readmeFactory = readmeFactory;
|
|
78
78
|
|
|
79
79
|
return this;
|
|
80
80
|
}
|
|
@@ -117,7 +117,7 @@ export class CommandActionBuilderWithState<TActionState extends IActionState> {
|
|
|
117
117
|
this.allowedUsers,
|
|
118
118
|
this.condition,
|
|
119
119
|
this.stateConstructor,
|
|
120
|
-
this.
|
|
120
|
+
this.readmeFactory != null ? this.readmeFactory : (_) => ''
|
|
121
121
|
);
|
|
122
122
|
}
|
|
123
123
|
}
|
package/main.ts
CHANGED
|
@@ -46,7 +46,7 @@ class BotOrchestrator {
|
|
|
46
46
|
};
|
|
47
47
|
}) {
|
|
48
48
|
const token = await readFile(options.tokenFilePath, 'utf8');
|
|
49
|
-
const
|
|
49
|
+
const remappedOptions = {
|
|
50
50
|
name: options.name,
|
|
51
51
|
token,
|
|
52
52
|
actions: options.actions,
|
|
@@ -60,8 +60,10 @@ class BotOrchestrator {
|
|
|
60
60
|
logger: options.services?.logger,
|
|
61
61
|
scheduler: options.services?.scheduler
|
|
62
62
|
}
|
|
63
|
-
}
|
|
63
|
+
};
|
|
64
|
+
const bot = new BotInstance(remappedOptions);
|
|
64
65
|
|
|
66
|
+
await bot.start(remappedOptions);
|
|
65
67
|
this.bots.push(bot);
|
|
66
68
|
|
|
67
69
|
return bot;
|