chz-telegram-bot 0.1.11 → 0.1.13

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.
@@ -11,13 +11,13 @@ export declare class BotInstance {
11
11
  private readonly storage;
12
12
  private readonly scheduler;
13
13
  private readonly logger;
14
- private readonly botInfo;
15
- readonly name: string;
16
14
  private readonly telegraf;
17
- private readonly commands;
18
- private readonly scheduled;
19
- private readonly inlineQueries;
20
- private readonly chats;
15
+ name: string;
16
+ private botInfo;
17
+ private commands;
18
+ private scheduled;
19
+ private inlineQueries;
20
+ private chats;
21
21
  constructor(options: {
22
22
  name: string;
23
23
  token: string;
@@ -36,6 +36,24 @@ export declare class BotInstance {
36
36
  scheduler?: IScheduler;
37
37
  };
38
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>;
39
57
  private initializeScheduledProcessing;
40
58
  private initializeInlineQueryProcessing;
41
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,EA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
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.name = options.name;
29
+ this.chats = options.chats;
24
30
  this.telegraf = new telegraf_1.Telegraf(options.token);
25
- this.telegraf.launch();
31
+ this.logger = options.services?.logger ?? new jsonLogger_1.JsonLogger();
32
+ this.scheduler =
33
+ options.services?.scheduler ??
34
+ new nodeTimeoutScheduler_1.NodeTimeoutScheduler(this.logger);
35
+ this.storage =
36
+ options.services?.storageClient ??
37
+ new jsonFileStorage_1.JsonFileStorage(options.name, actions, options.storagePath);
38
+ this.api = new telegramApi_1.TelegramApiService(this.name, this.telegraf.telegram, this.storage, this.logger);
39
+ this.storage.saveMetadata(actions, this.name);
40
+ }
41
+ async start(options) {
42
+ await this.telegraf.launch();
26
43
  this.botInfo = this.telegraf.botInfo;
27
- this.name = options.name;
28
44
  this.commands =
29
45
  options.actions.commands.length > 0
30
46
  ? [
@@ -36,20 +52,9 @@ class BotInstance {
36
52
  : [];
37
53
  this.scheduled = options.actions.scheduled;
38
54
  this.inlineQueries = options.actions.inlineQueries;
39
- this.chats = options.chats;
40
- const actions = [...this.commands, ...this.scheduled];
41
- this.logger = options.services?.logger ?? new jsonLogger_1.JsonLogger();
42
- this.scheduler =
43
- options.services?.scheduler ??
44
- new nodeTimeoutScheduler_1.NodeTimeoutScheduler(this.logger);
45
- this.storage =
46
- options.services?.storageClient ??
47
- new jsonFileStorage_1.JsonFileStorage(options.name, actions, options.storagePath);
48
- this.api = new telegramApi_1.TelegramApiService(this.name, this.telegraf.telegram, this.storage, this.logger);
49
55
  this.initializeMessageProcessing(options.verboseLoggingForIncomingMessage ?? false);
50
56
  this.initializeInlineQueryProcessing(1000);
51
57
  this.initializeScheduledProcessing(options.scheduledPeriod ?? (0, timeConvertions_1.hoursToSeconds)(1));
52
- this.storage.saveMetadata(actions, this.name);
53
58
  this.logger.logWithTraceId(this.name, (0, traceFactory_1.createTrace)(this, this.name, 'Start'), 'System', 'Starting bot...');
54
59
  }
55
60
  initializeScheduledProcessing(period) {
@@ -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;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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AAE,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 bot = new botInstance_1.BotInstance({
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
  }
@@ -31,14 +31,14 @@ export class BotInstance {
31
31
  private readonly storage: IStorageClient;
32
32
  private readonly scheduler: IScheduler;
33
33
  private readonly logger: ILogger;
34
- private readonly botInfo: UserFromGetMe;
35
-
36
- readonly name: string;
37
34
  private readonly telegraf: Telegraf;
38
- private readonly commands: CommandAction<IActionState>[];
39
- private readonly scheduled: ScheduledAction<IActionState>[];
40
- private readonly inlineQueries: InlineQueryAction[];
41
- private readonly chats: Record<string, number>;
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>;
42
42
 
43
43
  constructor(options: {
44
44
  name: string;
@@ -58,11 +58,53 @@ export class BotInstance {
58
58
  scheduler?: IScheduler;
59
59
  };
60
60
  }) {
61
+ const actions = [
62
+ ...options.actions.commands,
63
+ ...options.actions.scheduled
64
+ ];
65
+
66
+ this.name = options.name;
67
+ this.chats = options.chats;
68
+
61
69
  this.telegraf = new Telegraf(options.token);
62
- this.telegraf.launch();
70
+ this.logger = options.services?.logger ?? new JsonLogger();
71
+ this.scheduler =
72
+ options.services?.scheduler ??
73
+ new NodeTimeoutScheduler(this.logger);
74
+ this.storage =
75
+ options.services?.storageClient ??
76
+ new JsonFileStorage(options.name, actions, options.storagePath);
77
+ this.api = new TelegramApiService(
78
+ this.name,
79
+ this.telegraf.telegram,
80
+ this.storage,
81
+ this.logger
82
+ );
83
+
84
+ this.storage.saveMetadata(actions, this.name);
85
+ }
86
+
87
+ async start(options: {
88
+ name: string;
89
+ token: string;
90
+ actions: {
91
+ commands: CommandAction<IActionState>[];
92
+ scheduled: ScheduledAction<IActionState>[];
93
+ inlineQueries: InlineQueryAction[];
94
+ };
95
+ chats: Record<string, number>;
96
+ storagePath?: string;
97
+ scheduledPeriod?: Seconds;
98
+ verboseLoggingForIncomingMessage?: boolean;
99
+ services?: {
100
+ storageClient?: IStorageClient;
101
+ logger?: ILogger;
102
+ scheduler?: IScheduler;
103
+ };
104
+ }) {
105
+ await this.telegraf.launch();
63
106
  this.botInfo = this.telegraf.botInfo!;
64
107
 
65
- this.name = options.name;
66
108
  this.commands =
67
109
  options.actions.commands.length > 0
68
110
  ? [
@@ -78,22 +120,6 @@ export class BotInstance {
78
120
  : [];
79
121
  this.scheduled = options.actions.scheduled;
80
122
  this.inlineQueries = options.actions.inlineQueries;
81
- this.chats = options.chats;
82
-
83
- const actions = [...this.commands, ...this.scheduled];
84
- this.logger = options.services?.logger ?? new JsonLogger();
85
- this.scheduler =
86
- options.services?.scheduler ??
87
- new NodeTimeoutScheduler(this.logger);
88
- this.storage =
89
- options.services?.storageClient ??
90
- new JsonFileStorage(options.name, actions, options.storagePath);
91
- this.api = new TelegramApiService(
92
- this.name,
93
- this.telegraf.telegram,
94
- this.storage,
95
- this.logger
96
- );
97
123
 
98
124
  this.initializeMessageProcessing(
99
125
  options.verboseLoggingForIncomingMessage ?? false
@@ -103,8 +129,6 @@ export class BotInstance {
103
129
  options.scheduledPeriod ?? hoursToSeconds(1 as Hours)
104
130
  );
105
131
 
106
- this.storage.saveMetadata(actions, this.name);
107
-
108
132
  this.logger.logWithTraceId(
109
133
  this.name,
110
134
  createTrace(this, this.name, 'Start'),
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 bot = new BotInstance({
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chz-telegram-bot",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "async-sema": "^3.1.1",