@2byte/tgbot-framework 1.0.9 → 1.0.11

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.9",
3
+ "version": "1.0.11",
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
@@ -46,7 +46,7 @@ export class App {
46
46
 
47
47
  public bot!: Telegraf<Telegraf2byteContext>;
48
48
  private sectionClasses: Map<string, typeof Section> = new Map();
49
- private runnedSections: WeakMap<UserModel, RunnedSection | Map<string, RunnedSection>> =
49
+ private runnedSections: WeakMap<UserModel, RunnedSection[]> =
50
50
  new WeakMap();
51
51
  private middlewares: CallableFunction[] = [];
52
52
  private apiServiceManager!: ApiServiceManager;
@@ -205,9 +205,9 @@ export class App {
205
205
  middleware();
206
206
  });
207
207
 
208
+ this.registerCommands();
208
209
  this.registerActionForCallbackQuery();
209
210
  this.registerHears();
210
- this.registerCommands();
211
211
  this.registerMessageHandlers();
212
212
  await this.registerServices();
213
213
 
@@ -377,7 +377,7 @@ export class App {
377
377
  const sectionRoute = new RunSectionRoute()
378
378
  .section(sectionId)
379
379
  .method(method)
380
- .actionPath(actionPath);
380
+ .callbackParams(actionPath, actionParams.toString());
381
381
 
382
382
  this.runSection(ctx, sectionRoute).catch((err) => {
383
383
  this.debugLog("Error running section:", err);
@@ -575,6 +575,7 @@ export class App {
575
575
 
576
576
  // Если указан runSection, выполняем его
577
577
  if (runSection) {
578
+ runSection.runAsCommand();
578
579
  await this.runSection(ctx, runSection);
579
580
  }
580
581
  } else {
@@ -702,7 +703,7 @@ export class App {
702
703
  this.debugLog(`Register command ${command} for section ${sectionId}`);
703
704
  if (command) {
704
705
  this.bot.command(command, async (ctx: Telegraf2byteContext) => {
705
- const sectionRoute = new RunSectionRoute().section(sectionId).method("index");
706
+ const sectionRoute = new RunSectionRoute().section(sectionId).method("index").runAsCommand();
706
707
  await this.runSection(ctx, sectionRoute);
707
708
  });
708
709
  }
@@ -791,56 +792,74 @@ export class App {
791
792
  }
792
793
  this.debugLog("Using section class:", sectionClass);
793
794
 
794
- const sectionInstance: Section = new sectionClass({
795
- ctx,
796
- bot: this.bot,
797
- app: this,
798
- route: sectionRoute,
799
- } as SectionOptions);
795
+ let sectionInstance: Section | undefined;
796
+
797
+ const createSectionInstance = (sectionClass: typeof Section) => {
798
+ return new sectionClass({
799
+ ctx,
800
+ bot: this.bot,
801
+ app: this,
802
+ route: sectionRoute,
803
+ } as SectionOptions);
804
+ };
805
+
806
+ const createRunnedSection = (instance: Section, route: RunSectionRoute): RunnedSection => {
807
+ return {
808
+ instance,
809
+ route,
810
+ };
811
+ };
812
+
813
+ const findRunnedSection = () => {
814
+ const userRunnedSections = this.runnedSections.get(ctx.user);
815
+ if (userRunnedSections && Array.isArray(userRunnedSections)) {
816
+ return userRunnedSections.find((section) => section.route.getSection() === sectionId);
817
+ }
818
+ return undefined;
819
+ };
800
820
 
801
- let runnedSection;
802
- let userRunnedSections: Map<string, RunnedSection> | RunnedSection | undefined;
803
- let sectionInstalled = false;
821
+ let isRestoredSection = false;
822
+ let runnedSection : RunnedSection | undefined = undefined;
823
+ let createdNewSectionInstance = false;
804
824
 
805
825
  if (this.config.keepSectionInstances) {
806
- userRunnedSections = this.runnedSections.get(ctx.user);
807
- if (userRunnedSections instanceof Map) {
808
- runnedSection &&= userRunnedSections.get(sectionId);
826
+ runnedSection = findRunnedSection();
827
+ if (runnedSection) {
828
+ runnedSection.instance
829
+ .updateCtx(ctx)
830
+ .updateRoute(sectionRoute)
831
+ .setCallbackParams(sectionRoute.getCallbackParams());
832
+
833
+ runnedSection.route.runAsCallbackQuery(sectionRoute.runIsCallbackQuery);
834
+
835
+ isRestoredSection = true;
836
+ } else {
837
+ createdNewSectionInstance = true;
809
838
  }
810
839
  } else {
811
- runnedSection &&= this.runnedSections.get(ctx.user);
840
+ createdNewSectionInstance = true;
812
841
  }
813
- if (runnedSection) {
814
- this.debugLog(`Restored a runned section for user ${ctx.user.username}:`, runnedSection);
842
+
843
+ if (isRestoredSection) {
844
+ this.debugLog(`Restored a runned section for user ${ctx.user.username}:`, runnedSection?.instance.sectionId);
815
845
  }
816
846
 
817
- if (!runnedSection && this.config.keepSectionInstances) {
818
- if (userRunnedSections instanceof Map) {
819
- userRunnedSections.set(sectionId, {
820
- instance: sectionInstance,
821
- route: sectionRoute,
822
- });
823
- sectionInstalled = true;
824
- } else if (userRunnedSections === undefined) {
825
- userRunnedSections = new Map<string, RunnedSection>([
826
- [
827
- sectionId,
828
- {
829
- instance: sectionInstance,
830
- route: sectionRoute,
831
- },
832
- ],
833
- ]);
834
- sectionInstalled = true;
847
+ if (createdNewSectionInstance) {
848
+ this.debugLog(`Creating new section instance for user ${ctx.user.username}`);
849
+ runnedSection = createRunnedSection(createSectionInstance(sectionClass), sectionRoute);
850
+
851
+ if (this.config.keepSectionInstances) {
852
+ if (!this.runnedSections.has(ctx.user)) {
853
+ this.runnedSections.set(ctx.user, []);
854
+ }
855
+ (this.runnedSections.get(ctx.user) as RunnedSection[]).push(runnedSection);
835
856
  }
836
857
  }
837
858
 
838
- if (!runnedSection && !this.config.keepSectionInstances) {
839
- this.runnedSections.set(ctx.user, {
840
- instance: sectionInstance,
841
- route: sectionRoute,
842
- });
843
- sectionInstalled = true;
859
+ if (runnedSection) {
860
+ sectionInstance = runnedSection.instance;
861
+ } else {
862
+ throw new Error(`Failed to create or retrieve runned section for ${sectionId}`);
844
863
  }
845
864
 
846
865
  if (!(sectionInstance as any)[method]) {
@@ -862,14 +881,12 @@ export class App {
862
881
  const unsetupMethod = sectionInstance.unsetup;
863
882
 
864
883
  // Run setup if section is installed
865
- if (sectionInstalled && setupMethod && typeof setupMethod === "function") {
866
- if (sectionInstalled) {
884
+ if (createdNewSectionInstance && setupMethod && typeof setupMethod === "function") {
867
885
  this.debugLog(`[Setup] Section ${sectionId} install for user ${ctx.user.username}`);
868
886
  await sectionInstance.setup();
869
887
  this.debugLog(
870
888
  `[Setup finish] Section ${sectionId} installed for user ${ctx.user.username}`
871
889
  );
872
- }
873
890
  }
874
891
 
875
892
  // Run up method
@@ -1,6 +1,6 @@
1
1
  import { Telegraf2byteContext } from "./Telegraf2byteContext";
2
2
  import { Input, Markup } from "telegraf";
3
- import type { ReplyKeyboardMarkup } from 'telegraf/core/types/telegram';
3
+ import type { ReplyKeyboardMarkup } from "telegraf/core/types/telegram";
4
4
  import { InlineKeyboard } from "./InlineKeyboard";
5
5
  import { RequestInputOptions } from "../types";
6
6
  import { Message } from "telegraf/types";
@@ -198,36 +198,37 @@ export default class Message2byte {
198
198
  }
199
199
 
200
200
  const message = this.ctx.callbackQuery?.message as Message;
201
-
201
+
202
202
  if (message) {
203
- if ('media_group_id' in message || 'caption' in message) {
203
+ if ("media_group_id" in message || "caption" in message) {
204
204
  const editMessageCaption = this.editMessageCaption(this.messageValue, this.messageExtra);
205
205
 
206
- if (editMessageCaption && 'message_id' in editMessageCaption) {
206
+ if (editMessageCaption && "message_id" in editMessageCaption) {
207
207
  this.messageId = editMessageCaption.message_id as number;
208
208
  }
209
209
 
210
210
  return editMessageCaption;
211
211
  } else {
212
-
213
212
  const editedText = this.editMessageText(this.messageValue, this.messageExtra);
214
213
 
215
- if (editedText && 'message_id' in editedText) {
216
- this.messageId = editedText.message_id as number;
214
+ if (editedText && "message_id" in editedText) {
215
+ // this.messageId = editedText.message_id as number;
217
216
  }
218
217
 
219
218
  return editedText;
220
219
  }
221
220
  } else {
222
- this.messageExtra.message_id = this.messageId;
221
+ // this.messageExtra.message_id = this.messageId;
223
222
 
224
- const messageEntity = await this.editMessageText(this.messageValue, this.messageExtra);
223
+ try {
224
+ const messageEntity = await this.editMessageText(this.messageValue, this.messageExtra);
225
225
 
226
- if (typeof messageEntity === "object" && 'message_id' in messageEntity) {
227
- this.messageId = messageEntity.message_id as number;
228
- }
229
-
230
- return messageEntity;
226
+ if (typeof messageEntity === "object" && "message_id" in messageEntity) {
227
+ this.messageId = messageEntity.message_id as number;
228
+ }
229
+
230
+ return messageEntity;
231
+ } catch (e) {}
231
232
  }
232
233
  }
233
234
 
@@ -237,7 +238,7 @@ export default class Message2byte {
237
238
 
238
239
  const replyEntity = this.ctx.reply(this.messageValue, this.messageExtra);
239
240
 
240
- this.messageId = (await replyEntity).message_id;
241
+ // this.messageId = (await replyEntity).message_id;
241
242
 
242
243
  return replyEntity;
243
244
  }
@@ -1,6 +1,7 @@
1
1
  import type { Section, Telegraf2byteContext } from "@2byte/tgbot-framework";
2
2
  import Message2byte from "./Message2Byte";
3
3
  import Message2ByteLiveProgressive from "./Message2ByteLiveProgressive";
4
+ import { InlineKeyboard } from "./InlineKeyboard";
4
5
 
5
6
  export default class Message2bytePool {
6
7
  private message2byte: Message2byte;
@@ -61,6 +62,11 @@ export default class Message2bytePool {
61
62
  return Message2ByteLiveProgressive.init(this.message2byte, this);
62
63
  }
63
64
 
65
+ inlineKeyboard(keyboard: InlineKeyboard): this {
66
+ this.message2byte.inlineKeyboard(keyboard);
67
+ return this;
68
+ }
69
+
64
70
  async send() {
65
71
 
66
72
  const entity = await this.message2byte.send();
@@ -6,7 +6,7 @@ export class RunSectionRoute {
6
6
  method: null,
7
7
  methodArgs: null,
8
8
  callbackParams: new URLSearchParams(),
9
- runAsCallcackQuery: false,
9
+ runAsCallbackQuery: false,
10
10
  actionPath: null,
11
11
  hearsKey: null,
12
12
  };
@@ -20,7 +20,7 @@ export class RunSectionRoute {
20
20
 
21
21
  method(name: string = 'index', isRunCallbackQuery: boolean = false): this {
22
22
  this.runParams.method = name;
23
- this.runParams.runAsCallcackQuery = isRunCallbackQuery;
23
+ this.runParams.runAsCallbackQuery = isRunCallbackQuery;
24
24
  return this;
25
25
  }
26
26
 
@@ -32,13 +32,13 @@ export class RunSectionRoute {
32
32
  callbackParams(actionPath: string, params: string | Record<string, string>): this {
33
33
  this.runParams.callbackParams = new URLSearchParams(params);
34
34
  this.actionPath(actionPath);
35
- this.runParams.runAsCallcackQuery = true;
35
+ this.runParams.runAsCallbackQuery = true;
36
36
  return this;
37
37
  }
38
38
 
39
39
  actionPath(path: string): this {
40
40
  this.runParams.actionPath = path;
41
- this.runParams.runAsCallcackQuery = true;
41
+ this.runParams.runAsCallbackQuery = true;
42
42
  return this;
43
43
  }
44
44
 
@@ -47,6 +47,16 @@ export class RunSectionRoute {
47
47
  return this;
48
48
  }
49
49
 
50
+ runAsCommand(flag: boolean = true): this {
51
+ this.runParams.runAsCallbackQuery = !flag;
52
+ return this;
53
+ }
54
+
55
+ runAsCallbackQuery(flag: boolean = true): this {
56
+ this.runParams.runAsCallbackQuery = flag;
57
+ return this;
58
+ }
59
+
50
60
  getMethod(): string | null {
51
61
  return this.runParams.method;
52
62
  }
@@ -80,6 +90,6 @@ export class RunSectionRoute {
80
90
  }
81
91
 
82
92
  get runIsCallbackQuery(): boolean {
83
- return this.runParams.runAsCallcackQuery;
93
+ return this.runParams.runAsCallbackQuery;
84
94
  }
85
95
  }
@@ -310,7 +310,7 @@ export class Section {
310
310
  return false;
311
311
  }
312
312
 
313
- backInlineButtion(data: string): any {
313
+ backInlineButton(data: string): any {
314
314
  return this.markup.button.callback(this.labelBack, data);
315
315
  }
316
316
 
@@ -396,6 +396,16 @@ export class Section {
396
396
  : this.createPoolNewMessage(message);
397
397
  }
398
398
 
399
+ updateCtx(newCtx: Telegraf2byteContext): this {
400
+ this.ctx = newCtx;
401
+ return this;
402
+ }
403
+
404
+ updateRoute(newRoute: RunSectionRoute): this {
405
+ this.route = newRoute;
406
+ return this;
407
+ }
408
+
399
409
  getCtx(): Telegraf2byteContext {
400
410
  return this.ctx;
401
411
  }
package/src/types.ts CHANGED
@@ -133,7 +133,7 @@ export interface RunSectionRouteParams {
133
133
  method: string | null;
134
134
  methodArgs: any[] | null;
135
135
  callbackParams: URLSearchParams;
136
- runAsCallcackQuery: boolean;
136
+ runAsCallbackQuery: boolean;
137
137
  actionPath: string | null;
138
138
  hearsKey: string | null;
139
139
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "{{kebabName}}",
3
- "version": "1.0.9",
3
+ "version": "1.0.0",
4
4
  "description": "{{description}}",
5
5
  "main": "bot.ts",
6
6
  "scripts": {
@@ -21,7 +21,7 @@
21
21
  "author": "{{author}}",
22
22
  "license": "MIT",
23
23
  "dependencies": {
24
- "@2byte/tgbot-framework": "^1.0.6"
24
+ "@2byte/tgbot-framework": "^1.0.11"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/node": "^20.19.8",