@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 +1 -1
- package/src/core/App.ts +63 -46
- package/src/illumination/Message2Byte.ts +16 -15
- package/src/illumination/Message2bytePool.ts +6 -0
- package/src/illumination/RunSectionRoute.ts +15 -5
- package/src/illumination/Section.ts +11 -1
- package/src/types.ts +1 -1
- package/templates/bot/package.json +2 -2
package/package.json
CHANGED
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
|
|
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(
|
|
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
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
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
|
|
802
|
-
let
|
|
803
|
-
let
|
|
821
|
+
let isRestoredSection = false;
|
|
822
|
+
let runnedSection : RunnedSection | undefined = undefined;
|
|
823
|
+
let createdNewSectionInstance = false;
|
|
804
824
|
|
|
805
825
|
if (this.config.keepSectionInstances) {
|
|
806
|
-
|
|
807
|
-
if (
|
|
808
|
-
runnedSection
|
|
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
|
-
|
|
840
|
+
createdNewSectionInstance = true;
|
|
812
841
|
}
|
|
813
|
-
|
|
814
|
-
|
|
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 (
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
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 (
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
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 (
|
|
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
|
|
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 (
|
|
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 &&
|
|
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 &&
|
|
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
|
-
|
|
223
|
+
try {
|
|
224
|
+
const messageEntity = await this.editMessageText(this.messageValue, this.messageExtra);
|
|
225
225
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
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
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
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.
|
|
24
|
+
"@2byte/tgbot-framework": "^1.0.11"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/node": "^20.19.8",
|