balebaazoo 1.2.3 → 1.4.0
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/README.md +1 -1
- package/dist/index.cjs +275 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +68 -8
- package/dist/index.d.ts +68 -8
- package/dist/index.js +270 -23
- package/dist/index.js.map +1 -1
- package/package.json +7 -2
package/dist/index.js
CHANGED
|
@@ -414,7 +414,13 @@ var Api = class {
|
|
|
414
414
|
return this.call("answerPreCheckoutQuery", asParams(params));
|
|
415
415
|
}
|
|
416
416
|
inquireTransaction(params) {
|
|
417
|
-
|
|
417
|
+
const transactionId = params.transaction_id || params.provider_payment_charge_id;
|
|
418
|
+
if (!transactionId) {
|
|
419
|
+
throw new Error("transaction_id is required for inquireTransaction");
|
|
420
|
+
}
|
|
421
|
+
return this.call("inquireTransaction", {
|
|
422
|
+
transaction_id: transactionId
|
|
423
|
+
});
|
|
418
424
|
}
|
|
419
425
|
};
|
|
420
426
|
function asParams(params) {
|
|
@@ -471,6 +477,13 @@ function matchUpdate(update) {
|
|
|
471
477
|
const matches = [];
|
|
472
478
|
if (update.message) {
|
|
473
479
|
matches.push("message");
|
|
480
|
+
if (update.message.animation) matches.push("message:animation");
|
|
481
|
+
if (update.message.new_chat_members?.length) {
|
|
482
|
+
matches.push("message:new_chat_members");
|
|
483
|
+
}
|
|
484
|
+
if (update.message.left_chat_member) {
|
|
485
|
+
matches.push("message:left_chat_member");
|
|
486
|
+
}
|
|
474
487
|
if (update.message.text !== void 0) matches.push("message:text");
|
|
475
488
|
if (update.message.photo) matches.push("message:photo");
|
|
476
489
|
if (update.message.document) matches.push("message:document");
|
|
@@ -530,20 +543,32 @@ function matchesChatType(ctx, chatType) {
|
|
|
530
543
|
|
|
531
544
|
// src/composer.ts
|
|
532
545
|
var Composer = class _Composer {
|
|
533
|
-
handler;
|
|
534
546
|
registered = [];
|
|
547
|
+
dirty = true;
|
|
548
|
+
snapshot = [];
|
|
549
|
+
cachedHandler;
|
|
535
550
|
constructor(...middleware) {
|
|
536
551
|
this.registered.push(...middleware.map(normalizeMiddleware));
|
|
537
|
-
this.
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
552
|
+
this.markDirty();
|
|
553
|
+
}
|
|
554
|
+
markDirty() {
|
|
555
|
+
this.dirty = true;
|
|
556
|
+
this.cachedHandler = void 0;
|
|
541
557
|
}
|
|
542
558
|
middleware() {
|
|
543
|
-
|
|
559
|
+
if (!this.cachedHandler || this.dirty) {
|
|
560
|
+
this.snapshot = [...this.registered];
|
|
561
|
+
this.cachedHandler = async (ctx, next) => {
|
|
562
|
+
await runMiddleware(this.snapshot, ctx);
|
|
563
|
+
await next();
|
|
564
|
+
};
|
|
565
|
+
this.dirty = false;
|
|
566
|
+
}
|
|
567
|
+
return this.cachedHandler;
|
|
544
568
|
}
|
|
545
569
|
use(...middleware) {
|
|
546
570
|
this.registered.push(...middleware.map(normalizeMiddleware));
|
|
571
|
+
this.markDirty();
|
|
547
572
|
return this;
|
|
548
573
|
}
|
|
549
574
|
on(filter, ...middleware) {
|
|
@@ -567,6 +592,7 @@ var Composer = class _Composer {
|
|
|
567
592
|
);
|
|
568
593
|
});
|
|
569
594
|
composer.registered.push(...middleware);
|
|
595
|
+
this.markDirty();
|
|
570
596
|
return this;
|
|
571
597
|
}
|
|
572
598
|
command(command, ...middleware) {
|
|
@@ -621,6 +647,17 @@ var Composer = class _Composer {
|
|
|
621
647
|
...middleware
|
|
622
648
|
);
|
|
623
649
|
}
|
|
650
|
+
static compose(...composers) {
|
|
651
|
+
const result = new _Composer();
|
|
652
|
+
for (const item of composers) {
|
|
653
|
+
if (item instanceof _Composer) {
|
|
654
|
+
result.use(item);
|
|
655
|
+
} else {
|
|
656
|
+
result.use(item);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
return result;
|
|
660
|
+
}
|
|
624
661
|
};
|
|
625
662
|
function escapeRegex(value) {
|
|
626
663
|
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
@@ -633,6 +670,8 @@ var Context = class {
|
|
|
633
670
|
botInfo;
|
|
634
671
|
callbackQueryAnswered = false;
|
|
635
672
|
_updateTypes;
|
|
673
|
+
session = {};
|
|
674
|
+
state = {};
|
|
636
675
|
constructor(options) {
|
|
637
676
|
this.api = options.api;
|
|
638
677
|
this.update = options.update;
|
|
@@ -731,6 +770,54 @@ var Context = class {
|
|
|
731
770
|
const chatId = this.requireChatId();
|
|
732
771
|
return this.api.sendInvoice({ chat_id: chatId, ...params });
|
|
733
772
|
}
|
|
773
|
+
targetMessage() {
|
|
774
|
+
return this.callbackQuery?.message ?? this.message;
|
|
775
|
+
}
|
|
776
|
+
async editMessageText(text, extra) {
|
|
777
|
+
const message = this.targetMessage();
|
|
778
|
+
if (!message) {
|
|
779
|
+
throw new Error("No message to edit in context");
|
|
780
|
+
}
|
|
781
|
+
return this.api.editMessageText({
|
|
782
|
+
chat_id: message.chat.id,
|
|
783
|
+
message_id: message.message_id,
|
|
784
|
+
text,
|
|
785
|
+
...extra
|
|
786
|
+
});
|
|
787
|
+
}
|
|
788
|
+
async deleteMessage(params) {
|
|
789
|
+
const message = this.targetMessage();
|
|
790
|
+
const chatId = params?.chat_id ?? message?.chat.id ?? this.chatId;
|
|
791
|
+
const messageId = params?.message_id ?? message?.message_id;
|
|
792
|
+
if (chatId === void 0 || messageId === void 0) {
|
|
793
|
+
throw new Error("No message to delete in context");
|
|
794
|
+
}
|
|
795
|
+
return this.api.deleteMessage({ chat_id: chatId, message_id: messageId });
|
|
796
|
+
}
|
|
797
|
+
async forwardMessage(toChatId, params) {
|
|
798
|
+
const message = this.targetMessage();
|
|
799
|
+
if (!message && !params?.from_chat_id) {
|
|
800
|
+
throw new Error("No source message in context");
|
|
801
|
+
}
|
|
802
|
+
return this.api.forwardMessage({
|
|
803
|
+
chat_id: toChatId,
|
|
804
|
+
from_chat_id: params?.from_chat_id ?? message.chat.id,
|
|
805
|
+
message_id: params?.message_id ?? message.message_id,
|
|
806
|
+
...params
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
async copyMessage(toChatId, params) {
|
|
810
|
+
const message = this.targetMessage();
|
|
811
|
+
if (!message && !params?.from_chat_id) {
|
|
812
|
+
throw new Error("No source message in context");
|
|
813
|
+
}
|
|
814
|
+
return this.api.copyMessage({
|
|
815
|
+
chat_id: toChatId,
|
|
816
|
+
from_chat_id: params?.from_chat_id ?? message.chat.id,
|
|
817
|
+
message_id: params?.message_id ?? message.message_id,
|
|
818
|
+
...params
|
|
819
|
+
});
|
|
820
|
+
}
|
|
734
821
|
async sendChatAction(action) {
|
|
735
822
|
const chatId = this.requireChatId();
|
|
736
823
|
return this.api.sendChatAction({ chat_id: chatId, action });
|
|
@@ -840,14 +927,7 @@ var PollingRunner = class {
|
|
|
840
927
|
{ signal }
|
|
841
928
|
);
|
|
842
929
|
this.backoffAttempt = 0;
|
|
843
|
-
|
|
844
|
-
this.offset = update.update_id + 1;
|
|
845
|
-
try {
|
|
846
|
-
await bot.handleUpdate(update);
|
|
847
|
-
} catch (error) {
|
|
848
|
-
onError(error);
|
|
849
|
-
}
|
|
850
|
-
}
|
|
930
|
+
await this.processUpdates(bot, updates, options, onError);
|
|
851
931
|
if (updates.length === 0 && signal.aborted) {
|
|
852
932
|
break;
|
|
853
933
|
}
|
|
@@ -866,6 +946,34 @@ var PollingRunner = class {
|
|
|
866
946
|
}
|
|
867
947
|
}
|
|
868
948
|
}
|
|
949
|
+
async processUpdates(bot, updates, options, onError) {
|
|
950
|
+
const concurrency = Math.max(1, options.concurrency ?? 1);
|
|
951
|
+
if (concurrency === 1) {
|
|
952
|
+
for (const update of updates) {
|
|
953
|
+
this.offset = update.update_id + 1;
|
|
954
|
+
try {
|
|
955
|
+
await bot.handleUpdate(update);
|
|
956
|
+
} catch (error) {
|
|
957
|
+
onError(error);
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
return;
|
|
961
|
+
}
|
|
962
|
+
let index = 0;
|
|
963
|
+
const workers = Array.from({ length: Math.min(concurrency, updates.length) }, async () => {
|
|
964
|
+
while (index < updates.length) {
|
|
965
|
+
const current = index++;
|
|
966
|
+
const update = updates[current];
|
|
967
|
+
this.offset = update.update_id + 1;
|
|
968
|
+
try {
|
|
969
|
+
await bot.handleUpdate(update);
|
|
970
|
+
} catch (error) {
|
|
971
|
+
onError(error);
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
});
|
|
975
|
+
await Promise.all(workers);
|
|
976
|
+
}
|
|
869
977
|
setOffset(offset) {
|
|
870
978
|
this.offset = offset;
|
|
871
979
|
}
|
|
@@ -933,7 +1041,7 @@ async function webhookFromJson(bot) {
|
|
|
933
1041
|
// src/bot.ts
|
|
934
1042
|
var Bot = class extends Composer {
|
|
935
1043
|
api;
|
|
936
|
-
|
|
1044
|
+
_botInfo;
|
|
937
1045
|
polling = new PollingRunner();
|
|
938
1046
|
autoAnswer;
|
|
939
1047
|
catchHandler;
|
|
@@ -941,17 +1049,23 @@ var Bot = class extends Composer {
|
|
|
941
1049
|
const api = new Api({ token, ...options });
|
|
942
1050
|
super();
|
|
943
1051
|
this.api = api;
|
|
944
|
-
this.
|
|
1052
|
+
this._botInfo = options.botInfo;
|
|
945
1053
|
this.autoAnswer = options.autoAnswerCallback ?? true;
|
|
946
1054
|
if (this.autoAnswer) {
|
|
947
1055
|
this.use(autoAnswerCallback());
|
|
948
1056
|
}
|
|
949
1057
|
}
|
|
1058
|
+
get telegram() {
|
|
1059
|
+
return this.api;
|
|
1060
|
+
}
|
|
1061
|
+
get botInfo() {
|
|
1062
|
+
return this._botInfo;
|
|
1063
|
+
}
|
|
950
1064
|
async init() {
|
|
951
|
-
if (!this.
|
|
952
|
-
this.
|
|
1065
|
+
if (!this._botInfo) {
|
|
1066
|
+
this._botInfo = await this.api.getMe();
|
|
953
1067
|
}
|
|
954
|
-
return this.
|
|
1068
|
+
return this._botInfo;
|
|
955
1069
|
}
|
|
956
1070
|
catch(handler) {
|
|
957
1071
|
this.catchHandler = handler;
|
|
@@ -974,7 +1088,7 @@ var Bot = class extends Composer {
|
|
|
974
1088
|
return new Context({
|
|
975
1089
|
api: this.api,
|
|
976
1090
|
update,
|
|
977
|
-
botInfo: this.
|
|
1091
|
+
botInfo: this._botInfo
|
|
978
1092
|
});
|
|
979
1093
|
}
|
|
980
1094
|
async start(options = {}) {
|
|
@@ -1005,7 +1119,7 @@ var Bot = class extends Composer {
|
|
|
1005
1119
|
}
|
|
1006
1120
|
if (options.onStart) {
|
|
1007
1121
|
try {
|
|
1008
|
-
await options.onStart(this.
|
|
1122
|
+
await options.onStart(this._botInfo);
|
|
1009
1123
|
} catch (error) {
|
|
1010
1124
|
onError(error);
|
|
1011
1125
|
return;
|
|
@@ -1016,6 +1130,13 @@ var Bot = class extends Composer {
|
|
|
1016
1130
|
launch(options) {
|
|
1017
1131
|
return this.start(options);
|
|
1018
1132
|
}
|
|
1133
|
+
async switchToPolling(options = {}) {
|
|
1134
|
+
await this.api.deleteWebhook();
|
|
1135
|
+
await this.start({
|
|
1136
|
+
...options,
|
|
1137
|
+
dropPendingUpdates: options.dropPendingUpdates ?? true
|
|
1138
|
+
});
|
|
1139
|
+
}
|
|
1019
1140
|
async stop() {
|
|
1020
1141
|
await this.polling.stop();
|
|
1021
1142
|
}
|
|
@@ -1136,6 +1257,132 @@ function setupGracefulShutdown(bot, options = {}) {
|
|
|
1136
1257
|
}
|
|
1137
1258
|
}
|
|
1138
1259
|
|
|
1139
|
-
|
|
1260
|
+
// src/scenes/index.ts
|
|
1261
|
+
var Scene = class extends Composer {
|
|
1262
|
+
id;
|
|
1263
|
+
enterMiddleware = [];
|
|
1264
|
+
leaveMiddleware = [];
|
|
1265
|
+
constructor(id, ...middleware) {
|
|
1266
|
+
super(...middleware);
|
|
1267
|
+
this.id = id;
|
|
1268
|
+
}
|
|
1269
|
+
enter(...middleware) {
|
|
1270
|
+
this.enterMiddleware.push(...middleware);
|
|
1271
|
+
return this;
|
|
1272
|
+
}
|
|
1273
|
+
leave(...middleware) {
|
|
1274
|
+
this.leaveMiddleware.push(...middleware);
|
|
1275
|
+
return this;
|
|
1276
|
+
}
|
|
1277
|
+
async runEnter(ctx) {
|
|
1278
|
+
await runMiddleware(this.enterMiddleware, ctx);
|
|
1279
|
+
}
|
|
1280
|
+
async runLeave(ctx) {
|
|
1281
|
+
await runMiddleware(this.leaveMiddleware, ctx);
|
|
1282
|
+
}
|
|
1283
|
+
};
|
|
1284
|
+
var Stage = class extends Composer {
|
|
1285
|
+
scenes = /* @__PURE__ */ new Map();
|
|
1286
|
+
register(...scenes) {
|
|
1287
|
+
for (const scene of scenes) {
|
|
1288
|
+
this.scenes.set(scene.id, scene);
|
|
1289
|
+
}
|
|
1290
|
+
return this;
|
|
1291
|
+
}
|
|
1292
|
+
getScene(id) {
|
|
1293
|
+
return this.scenes.get(id);
|
|
1294
|
+
}
|
|
1295
|
+
async enter(ctx, sceneId, state = {}) {
|
|
1296
|
+
const scene = this.scenes.get(sceneId);
|
|
1297
|
+
if (!scene) {
|
|
1298
|
+
throw new Error(`Scene "${sceneId}" is not registered`);
|
|
1299
|
+
}
|
|
1300
|
+
const session2 = ctx.session;
|
|
1301
|
+
const currentId = session2.__scene?.id;
|
|
1302
|
+
if (currentId && currentId !== sceneId) {
|
|
1303
|
+
const current = this.scenes.get(currentId);
|
|
1304
|
+
if (current) {
|
|
1305
|
+
await leaveScene(ctx, current);
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
await enterScene(ctx, sceneId, scene, state);
|
|
1309
|
+
}
|
|
1310
|
+
async leave(ctx) {
|
|
1311
|
+
const session2 = ctx.session;
|
|
1312
|
+
const sceneId = session2.__scene?.id;
|
|
1313
|
+
if (!sceneId) return;
|
|
1314
|
+
const scene = this.scenes.get(sceneId);
|
|
1315
|
+
if (scene) {
|
|
1316
|
+
await leaveScene(ctx, scene);
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
middleware() {
|
|
1320
|
+
const sceneMiddleware = super.middleware();
|
|
1321
|
+
return async (ctx, next) => {
|
|
1322
|
+
const session2 = ctx.session;
|
|
1323
|
+
const sceneId = session2.__scene?.id;
|
|
1324
|
+
if (!sceneId) {
|
|
1325
|
+
await sceneMiddleware(ctx, next);
|
|
1326
|
+
return;
|
|
1327
|
+
}
|
|
1328
|
+
const scene = this.scenes.get(sceneId);
|
|
1329
|
+
if (!scene) {
|
|
1330
|
+
delete session2.__scene;
|
|
1331
|
+
await sceneMiddleware(ctx, next);
|
|
1332
|
+
return;
|
|
1333
|
+
}
|
|
1334
|
+
await scene.middleware()(ctx, next);
|
|
1335
|
+
};
|
|
1336
|
+
}
|
|
1337
|
+
};
|
|
1338
|
+
async function enterScene(ctx, sceneId, scene, initialState = {}) {
|
|
1339
|
+
const session2 = ctx.session;
|
|
1340
|
+
session2.__scene = { ...initialState, id: sceneId };
|
|
1341
|
+
await scene.runEnter(ctx);
|
|
1342
|
+
}
|
|
1343
|
+
async function leaveScene(ctx, scene) {
|
|
1344
|
+
const session2 = ctx.session;
|
|
1345
|
+
await scene.runLeave(ctx);
|
|
1346
|
+
delete session2.__scene;
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
// src/session/memory.ts
|
|
1350
|
+
function memorySessionStore() {
|
|
1351
|
+
const data = /* @__PURE__ */ new Map();
|
|
1352
|
+
return {
|
|
1353
|
+
get: (key) => data.get(key),
|
|
1354
|
+
set: (key, value) => {
|
|
1355
|
+
data.set(key, value);
|
|
1356
|
+
},
|
|
1357
|
+
delete: (key) => {
|
|
1358
|
+
data.delete(key);
|
|
1359
|
+
}
|
|
1360
|
+
};
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
// src/session/index.ts
|
|
1364
|
+
function session(options) {
|
|
1365
|
+
const store = options.store ?? memorySessionStore();
|
|
1366
|
+
return async (ctx, next) => {
|
|
1367
|
+
const key = options.getSessionKey(ctx);
|
|
1368
|
+
if (!key) {
|
|
1369
|
+
await next();
|
|
1370
|
+
return;
|
|
1371
|
+
}
|
|
1372
|
+
let current = store.get(key) ?? options.defaultSession(ctx);
|
|
1373
|
+
Object.defineProperty(ctx, "session", {
|
|
1374
|
+
configurable: true,
|
|
1375
|
+
enumerable: true,
|
|
1376
|
+
get: () => current,
|
|
1377
|
+
set: (value) => {
|
|
1378
|
+
current = value;
|
|
1379
|
+
}
|
|
1380
|
+
});
|
|
1381
|
+
await next();
|
|
1382
|
+
store.set(key, current);
|
|
1383
|
+
};
|
|
1384
|
+
}
|
|
1385
|
+
|
|
1386
|
+
export { Api, BaleAPIError, BaleError, BaleNetworkError, Bot, Composer, Context, DEFAULT_API_BASE, InlineKeyboard, InputFile, PollingRunner, ReplyKeyboard, Scene, Stage, autoAnswerCallback, bold, createWebhookHandler, enterScene, errorHandler, extractCommand, isFilePath, isMiddlewareObject, italic, leaveScene, link, matchUpdate, matchesAnyFilter, matchesChatType, matchesFilter, md, memorySessionStore, normalizeMiddleware, removeKeyboard, runMiddleware, session, setupGracefulShutdown, spoiler, webhookFromJson };
|
|
1140
1387
|
//# sourceMappingURL=index.js.map
|
|
1141
1388
|
//# sourceMappingURL=index.js.map
|