chz-telegram-bot 0.1.11 → 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.
@@ -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
+ ];
24
28
  this.telegraf = new telegraf_1.Telegraf(options.token);
25
- this.telegraf.launch();
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();
26
41
  this.botInfo = this.telegraf.botInfo;
27
42
  this.name = options.name;
28
43
  this.commands =
@@ -37,19 +52,9 @@ class BotInstance {
37
52
  this.scheduled = options.actions.scheduled;
38
53
  this.inlineQueries = options.actions.inlineQueries;
39
54
  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
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M,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,8 +58,48 @@ export class BotInstance {
58
58
  scheduler?: IScheduler;
59
59
  };
60
60
  }) {
61
+ const actions = [
62
+ ...options.actions.commands,
63
+ ...options.actions.scheduled
64
+ ];
65
+
61
66
  this.telegraf = new Telegraf(options.token);
62
- this.telegraf.launch();
67
+ this.logger = options.services?.logger ?? new JsonLogger();
68
+ this.scheduler =
69
+ options.services?.scheduler ??
70
+ new NodeTimeoutScheduler(this.logger);
71
+ this.storage =
72
+ options.services?.storageClient ??
73
+ new JsonFileStorage(options.name, actions, options.storagePath);
74
+ this.api = new TelegramApiService(
75
+ this.name,
76
+ this.telegraf.telegram,
77
+ this.storage,
78
+ this.logger
79
+ );
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();
63
103
  this.botInfo = this.telegraf.botInfo!;
64
104
 
65
105
  this.name = options.name;
@@ -80,21 +120,6 @@ export class BotInstance {
80
120
  this.inlineQueries = options.actions.inlineQueries;
81
121
  this.chats = options.chats;
82
122
 
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
-
98
123
  this.initializeMessageProcessing(
99
124
  options.verboseLoggingForIncomingMessage ?? false
100
125
  );
@@ -103,8 +128,6 @@ export class BotInstance {
103
128
  options.scheduledPeriod ?? hoursToSeconds(1 as Hours)
104
129
  );
105
130
 
106
- this.storage.saveMetadata(actions, this.name);
107
-
108
131
  this.logger.logWithTraceId(
109
132
  this.name,
110
133
  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.12",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "async-sema": "^3.1.1",