@2byte/tgbot-framework 1.0.14 → 1.0.15

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@2byte/tgbot-framework",
3
- "version": "1.0.14",
3
+ "version": "1.0.15",
4
4
  "description": "A TypeScript framework for creating Telegram bots with sections-based architecture (Bun optimized)",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
package/src/core/App.ts CHANGED
@@ -74,7 +74,7 @@ export class App {
74
74
  }
75
75
  > = new Map();
76
76
 
77
- private messageHandlers: any[] = [];
77
+ private messageHandlers: RunSectionRoute[] | CallableFunction<this>[] = [];
78
78
 
79
79
  constructor() {
80
80
  this.middlewares.push(this.mainMiddleware.bind(this));
@@ -162,13 +162,12 @@ export class App {
162
162
  return this;
163
163
  }
164
164
 
165
- messageHandlers(handlers: any[]): this {
165
+ messageHandlers(handlers: RunSectionRoute[] | CallableFunction<this>[]): this {
166
166
  this.app.messageHandlers = handlers;
167
167
  return this;
168
168
  }
169
169
 
170
170
  /**
171
- *
172
171
  * @param keep Whether to keep section instances in memory after they are run.
173
172
  * If true, sections will not be reloaded on each request, improving performance for frequently accessed sections.
174
173
  * If false, sections will be reloaded each time they are accessed, ensuring the latest version is used.
@@ -433,16 +432,46 @@ export class App {
433
432
  !ctx.userSession.stateAfterValidatedUserResponse
434
433
  ) {
435
434
  this.messageHandlers.forEach(async (handler: any) => {
435
+ if (ctx.caught) {
436
+ this.debugLog("Message already caught by another handler, skipping remaining handlers.");
437
+ return;
438
+ }
439
+
440
+ const isHandlerRunSectionRoute = handler instanceof RunSectionRoute;
441
+
442
+ if (isHandlerRunSectionRoute) {
443
+ this.debugLog("Checking message handler section route:", handler);
444
+ await this.runSection(ctx, handler, {
445
+ cbBeforeRunMethod: async (sectionInstance: Section) => {
446
+ sectionInstance.runForMessageHandler = true;
447
+ },
448
+ });
449
+ if (ctx.caught) {
450
+ this.debugLog("Message handler route caught the message, skipping remaining handlers.");
451
+ return;
452
+ }
453
+ }
454
+
436
455
  const handlerIsClass =
437
456
  typeof handler === "function" && /^\s*class\s+/.test(handler.toString());
438
457
  const nameHandler = handlerIsClass
439
458
  ? handler.name
440
459
  : handler.constructor?.name || "unknown";
441
460
 
442
- this.debugLog("Handling message with handler class:", nameHandler);
443
-
444
- if (handlerIsClass) {
461
+ if (handlerIsClass && !ctx.caught) {
462
+ this.debugLog(`Running message handler class ${nameHandler} for user ${ctx.user.username}`);
445
463
  await new handler(this).handle(ctx);
464
+ if (ctx.caught) {
465
+ this.debugLog("Message handler class caught the message, skipping remaining handlers.");
466
+ return;
467
+ }
468
+ } else if (!handlerIsClass && typeof handler === "function" && !ctx.caught) {
469
+ this.debugLog(`Running message handler function ${nameHandler} for user ${ctx.user.username}`);
470
+ await handler(ctx);
471
+ if (ctx.caught) {
472
+ this.debugLog("Message handler function caught the message, skipping remaining handlers.");
473
+ return;
474
+ }
446
475
  }
447
476
  });
448
477
  } else {
@@ -63,6 +63,10 @@ export class RunSectionRoute {
63
63
  return this;
64
64
  }
65
65
 
66
+ static for(sectionId: string, methodName: string = 'index'): RunSectionRoute {
67
+ return new RunSectionRoute().section(sectionId).method(methodName);
68
+ }
69
+
66
70
  hasTriggers(): boolean {
67
71
  return this.runParams.triggers.length > 0;
68
72
  }
@@ -15,6 +15,7 @@ export class Section {
15
15
  static actionRoutes: { [key: string]: string };
16
16
  public sectionId: string = "BaseSection";
17
17
  public route: RunSectionRoute;
18
+ public runForMessageHandler: boolean = false;
18
19
  protected ctx: Telegraf2byteContext;
19
20
  protected bot: Telegraf<Telegraf2byteContext>;
20
21
  protected app: App;
@@ -11,6 +11,7 @@ export interface Telegraf2byteContext extends Context, ITelegraf2byteContextExtr
11
11
  user: UserModel;
12
12
  userStorage: UserStore;
13
13
  userSession: UserSession;
14
+ caught: boolean;
14
15
  // msgId?: number;
15
16
  }
16
17
 
@@ -36,10 +36,10 @@ export class MassSendApiService extends ApiService<MassSendApiParams> {
36
36
  if (receivedData && typeof receivedData == "object") {
37
37
  userIds = receivedData?.userIds || [];
38
38
  message = receivedData?.message || "Hello from MassSendApiService";
39
+
40
+ this.sendMassMessage(userIds, message, receivedData.extra);
39
41
  }
40
42
 
41
- this.sendMassMessage(userIds, message, receivedData.extra);
42
-
43
43
  return Response.json({ status: 200, body: "Mass message sending initiated." });
44
44
  },
45
45
  },
@@ -21,7 +21,7 @@
21
21
  "author": "{{author}}",
22
22
  "license": "MIT",
23
23
  "dependencies": {
24
- "@2byte/tgbot-framework": "^1.0.14"
24
+ "@2byte/tgbot-framework": "^1.0.15"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/node": "^20.19.8",