@2byte/tgbot-framework 1.0.9 → 1.0.10

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.10",
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
 
@@ -702,7 +702,7 @@ export class App {
702
702
  this.debugLog(`Register command ${command} for section ${sectionId}`);
703
703
  if (command) {
704
704
  this.bot.command(command, async (ctx: Telegraf2byteContext) => {
705
- const sectionRoute = new RunSectionRoute().section(sectionId).method("index");
705
+ const sectionRoute = new RunSectionRoute().section(sectionId).method("index").runAsCommand();
706
706
  await this.runSection(ctx, sectionRoute);
707
707
  });
708
708
  }
@@ -791,56 +791,74 @@ export class App {
791
791
  }
792
792
  this.debugLog("Using section class:", sectionClass);
793
793
 
794
- const sectionInstance: Section = new sectionClass({
795
- ctx,
796
- bot: this.bot,
797
- app: this,
798
- route: sectionRoute,
799
- } as SectionOptions);
794
+ let sectionInstance: Section | undefined;
800
795
 
801
- let runnedSection;
802
- let userRunnedSections: Map<string, RunnedSection> | RunnedSection | undefined;
803
- let sectionInstalled = false;
796
+ const createSectionInstance = (sectionClass: typeof Section) => {
797
+ return new sectionClass({
798
+ ctx,
799
+ bot: this.bot,
800
+ app: this,
801
+ route: sectionRoute,
802
+ } as SectionOptions);
803
+ };
804
+
805
+ const createRunnedSection = (instance: Section, route: RunSectionRoute): RunnedSection => {
806
+ return {
807
+ instance,
808
+ route,
809
+ };
810
+ };
811
+
812
+ const findRunnedSection = () => {
813
+ const userRunnedSections = this.runnedSections.get(ctx.user);
814
+ if (userRunnedSections && Array.isArray(userRunnedSections)) {
815
+ return userRunnedSections.find((section) => section.route.getSection() === sectionId);
816
+ }
817
+ return undefined;
818
+ };
819
+
820
+ let isRestoredSection = false;
821
+ let runnedSection : RunnedSection | undefined = undefined;
822
+ let createdNewSectionInstance = false;
804
823
 
805
824
  if (this.config.keepSectionInstances) {
806
- userRunnedSections = this.runnedSections.get(ctx.user);
807
- if (userRunnedSections instanceof Map) {
808
- runnedSection &&= userRunnedSections.get(sectionId);
825
+ runnedSection = findRunnedSection();
826
+ if (runnedSection) {
827
+ runnedSection.instance
828
+ .updateCtx(ctx);
829
+
830
+ if (sectionRoute.runIsCallbackQuery) {
831
+ runnedSection.route.runAsCallbackQuery();
832
+ }
833
+
834
+ isRestoredSection = true;
835
+ } else {
836
+ createdNewSectionInstance = true;
809
837
  }
810
838
  } else {
811
- runnedSection &&= this.runnedSections.get(ctx.user);
839
+ createdNewSectionInstance = true;
812
840
  }
813
- if (runnedSection) {
814
- this.debugLog(`Restored a runned section for user ${ctx.user.username}:`, runnedSection);
841
+
842
+ if (isRestoredSection) {
843
+ this.debugLog(`Restored a runned section for user ${ctx.user.username}:`, runnedSection?.instance.sectionId);
815
844
  }
816
845
 
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;
846
+ if (createdNewSectionInstance) {
847
+ this.debugLog(`Creating new section instance for user ${ctx.user.username}`);
848
+ runnedSection = createRunnedSection(createSectionInstance(sectionClass), sectionRoute);
849
+
850
+ if (this.config.keepSectionInstances) {
851
+ if (!this.runnedSections.has(ctx.user)) {
852
+ this.runnedSections.set(ctx.user, []);
853
+ }
854
+ (this.runnedSections.get(ctx.user) as RunnedSection[]).push(runnedSection);
835
855
  }
836
856
  }
837
857
 
838
- if (!runnedSection && !this.config.keepSectionInstances) {
839
- this.runnedSections.set(ctx.user, {
840
- instance: sectionInstance,
841
- route: sectionRoute,
842
- });
843
- sectionInstalled = true;
858
+ if (runnedSection) {
859
+ sectionInstance = runnedSection.instance;
860
+ } else {
861
+ throw new Error(`Failed to create or retrieve runned section for ${sectionId}`);
844
862
  }
845
863
 
846
864
  if (!(sectionInstance as any)[method]) {
@@ -862,14 +880,12 @@ export class App {
862
880
  const unsetupMethod = sectionInstance.unsetup;
863
881
 
864
882
  // Run setup if section is installed
865
- if (sectionInstalled && setupMethod && typeof setupMethod === "function") {
866
- if (sectionInstalled) {
883
+ if (createdNewSectionInstance && setupMethod && typeof setupMethod === "function") {
867
884
  this.debugLog(`[Setup] Section ${sectionId} install for user ${ctx.user.username}`);
868
885
  await sectionInstance.setup();
869
886
  this.debugLog(
870
887
  `[Setup finish] Section ${sectionId} installed for user ${ctx.user.username}`
871
888
  );
872
- }
873
889
  }
874
890
 
875
891
  // Run up method
@@ -213,13 +213,13 @@ export default class Message2byte {
213
213
  const editedText = this.editMessageText(this.messageValue, this.messageExtra);
214
214
 
215
215
  if (editedText && 'message_id' in editedText) {
216
- this.messageId = editedText.message_id as number;
216
+ // this.messageId = editedText.message_id as number;
217
217
  }
218
218
 
219
219
  return editedText;
220
220
  }
221
221
  } else {
222
- this.messageExtra.message_id = this.messageId;
222
+ // this.messageExtra.message_id = this.messageId;
223
223
 
224
224
  const messageEntity = await this.editMessageText(this.messageValue, this.messageExtra);
225
225
 
@@ -237,7 +237,7 @@ export default class Message2byte {
237
237
 
238
238
  const replyEntity = this.ctx.reply(this.messageValue, this.messageExtra);
239
239
 
240
- this.messageId = (await replyEntity).message_id;
240
+ // this.messageId = (await replyEntity).message_id;
241
241
 
242
242
  return replyEntity;
243
243
  }
@@ -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();
@@ -47,6 +47,16 @@ export class RunSectionRoute {
47
47
  return this;
48
48
  }
49
49
 
50
+ runAsCommand(): this {
51
+ this.runParams.runAsCallcackQuery = false;
52
+ return this;
53
+ }
54
+
55
+ runAsCallbackQuery(): this {
56
+ this.runParams.runAsCallcackQuery = true;
57
+ return this;
58
+ }
59
+
50
60
  getMethod(): string | null {
51
61
  return this.runParams.method;
52
62
  }
@@ -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
  }
@@ -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.10"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/node": "^20.19.8",