@mtkruto/node 0.1.140 → 0.1.142

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.
Files changed (45) hide show
  1. package/esm/4_constants.d.ts +1 -1
  2. package/esm/4_constants.js +1 -1
  3. package/esm/5_client.d.ts +1 -0
  4. package/esm/5_client.js +1 -0
  5. package/esm/client/0_types.d.ts +1 -0
  6. package/esm/client/1_reaction_manager.d.ts +9 -0
  7. package/esm/client/1_reaction_manager.js +84 -0
  8. package/esm/client/2_message_manager.d.ts +13 -1
  9. package/esm/client/2_message_manager.js +130 -29
  10. package/esm/client/3_callback_query_manager.d.ts +16 -0
  11. package/esm/client/3_callback_query_manager.js +36 -0
  12. package/esm/client/3_chat_list_manager.d.ts +5 -9
  13. package/esm/client/3_chat_list_manager.js +190 -103
  14. package/esm/client/3_inline_query_manager.d.ts +16 -0
  15. package/esm/client/3_inline_query_manager.js +48 -0
  16. package/esm/client/4_client.d.ts +10 -10
  17. package/esm/client/4_client.js +128 -315
  18. package/esm/client/5_session_string.d.ts +3 -0
  19. package/esm/client/5_session_string.js +22 -0
  20. package/esm/connection/1_connection_web_socket.js +1 -1
  21. package/package.json +1 -1
  22. package/script/4_constants.d.ts +1 -1
  23. package/script/4_constants.js +1 -1
  24. package/script/5_client.d.ts +1 -0
  25. package/script/5_client.js +1 -0
  26. package/script/client/0_types.d.ts +1 -0
  27. package/script/client/1_reaction_manager.d.ts +9 -0
  28. package/script/client/1_reaction_manager.js +88 -0
  29. package/script/client/2_message_manager.d.ts +13 -1
  30. package/script/client/2_message_manager.js +129 -28
  31. package/script/client/3_callback_query_manager.d.ts +16 -0
  32. package/script/client/3_callback_query_manager.js +40 -0
  33. package/script/client/3_chat_list_manager.d.ts +5 -9
  34. package/script/client/3_chat_list_manager.js +187 -100
  35. package/script/client/3_inline_query_manager.d.ts +16 -0
  36. package/script/client/3_inline_query_manager.js +52 -0
  37. package/script/client/4_client.d.ts +10 -10
  38. package/script/client/4_client.js +126 -313
  39. package/script/client/5_session_string.d.ts +3 -0
  40. package/script/client/5_session_string.js +27 -0
  41. package/script/connection/1_connection_web_socket.js +1 -1
  42. package/esm/client/3_reaction_manager.d.ts +0 -16
  43. package/esm/client/3_reaction_manager.js +0 -61
  44. package/script/client/3_reaction_manager.d.ts +0 -16
  45. package/script/client/3_reaction_manager.js +0 -65
@@ -9,12 +9,12 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _Client_instances, _Client_auth, _Client_sessionId, _Client_state, _Client_promises, _Client_toAcknowledge, _Client_guaranteeUpdateDelivery, _Client_updateManager, _Client_fileManager, _Client_messageManager, _Client_chatListManager, _Client_reactionManager, _Client_parseMode, _Client_publicKeys, _Client_autoStart, _Client_ignoreOutgoing, _Client_prefixes, _Client_namespaceProxies, _Client_constructContext, _Client_propagateConnectionState, _Client_lastPropagatedConnectionState, _Client_storageInited, _Client_setAuth, _Client_authKeyWasCreated, _Client_connectMutex, _Client_connectionInited, _Client_initConnection, _Client_lastPropagatedAuthorizationState, _Client_propagateAuthorizationState, _Client_selfId, _Client_getSelfId, _Client_receiveLoop, _Client_pingLoopAbortSignal, _Client_pingInterval, _Client_lastUpdates, _Client_pingLoop, _Client_pingLoopStarted, _Client_autoStarted, _Client_lastMsgId, _Client_invoke, _Client_handleInvokeError, _Client_getUserAccessHash, _Client_getChannelAccessHash, _Client_getInputPeerInner, _Client_parseText, _Client_lastGetMe, _Client_getMe, _Client_handleCtxUpdate, _Client_queueHandleCtxUpdate, _Client_handleUpdate, _Client_usernameResolver, _Client_handle, _Client_setMyInfo, _Client_getMyInfo;
12
+ var _Client_instances, _Client_auth, _Client_sessionId, _Client_state, _Client_promises, _Client_toAcknowledge, _Client_guaranteeUpdateDelivery, _Client_updateManager, _Client_fileManager, _Client_reactionManager, _Client_messageManager, _Client_callbackQueryManager, _Client_inlineQueryManager, _Client_chatListManager, _Client_parseMode, _Client_publicKeys, _Client_autoStart, _Client_ignoreOutgoing, _Client_prefixes, _Client_namespaceProxies, _Client_constructContext, _Client_propagateConnectionState, _Client_lastPropagatedConnectionState, _Client_setAuth, _Client_storageInited, _Client_initStorage, _Client_connectMutex, _Client_connectionInited, _Client_initConnection, _Client_lastPropagatedAuthorizationState, _Client_propagateAuthorizationState, _Client_selfId, _Client_getSelfId, _Client_receiveLoop, _Client_pingLoopAbortSignal, _Client_pingInterval, _Client_lastUpdates, _Client_pingLoop, _Client_pingLoopStarted, _Client_autoStarted, _Client_lastMsgId, _Client_invoke, _Client_handleInvokeError, _Client_getUserAccessHash, _Client_getChannelAccessHash, _Client_getInputPeerInner, _Client_parseText, _Client_lastGetMe, _Client_getMe, _Client_handleCtxUpdate, _Client_queueHandleCtxUpdate, _Client_handleUpdate, _Client_handle, _Client_setMyInfo, _Client_getMyInfo;
13
13
  import { debug, gunzip, Mutex } from "../0_deps.js";
14
14
  import { bigIntFromBuffer, cleanObject, drop, getRandomBigInt, getRandomId, mustPrompt, mustPromptOneOf, sha1, toUnixTimestamp, UNREACHABLE, ZERO_CHANNEL_ID } from "../1_utilities.js";
15
- import { as, functions, getChannelChatId, Message_, MessageContainer, name, peerToChatId, RPCResult, TLError, TLReader, types } from "../2_tl.js";
15
+ import { as, functions, Message_, MessageContainer, name, RPCResult, TLError, TLReader, types } from "../2_tl.js";
16
16
  import { StorageMemory } from "../3_storage.js";
17
- import { botCommandScopeToTlObject, constructCallbackQuery, constructChat2, constructChat3, constructChosenInlineResult, constructInlineQuery, constructMessageReaction, constructMessageReactionCount, constructMessageReactions, constructUser, inlineQueryResultToTlObject, messageEntityToTlObject } from "../3_types.js";
17
+ import { botCommandScopeToTlObject, constructUser, messageEntityToTlObject } from "../3_types.js";
18
18
  import { ACK_THRESHOLD, APP_VERSION, DEVICE_MODEL, LANG_CODE, LANG_PACK, LAYER, MAX_CHANNEL_ID, MAX_CHAT_ID, SYSTEM_LANG_CODE, SYSTEM_VERSION, USERNAME_TTL } from "../4_constants.js";
19
19
  import { AuthKeyUnregistered, FloodWait, Migrate, PasswordHashInvalid, PhoneNumberInvalid, SessionPasswordNeeded, upgradeInstance } from "../4_errors.js";
20
20
  import { ClientAbstract } from "./0_client_abstract.js";
@@ -26,11 +26,13 @@ import { ConnectionError } from "./0_types.js";
26
26
  import { getFileContents, getUsername, resolve } from "./0_utilities.js";
27
27
  import { Composer, concat, flatten, skip } from "./1_composer.js";
28
28
  import { FileManager } from "./1_file_manager.js";
29
+ import { ReactionManager } from "./1_reaction_manager.js";
29
30
  import { UpdateManager } from "./1_update_manager.js";
30
31
  import { ClientPlain } from "./2_client_plain.js";
31
32
  import { MessageManager } from "./2_message_manager.js";
33
+ import { CallbackQueryManager } from "./3_callback_query_manager.js";
32
34
  import { ChatListManager } from "./3_chat_list_manager.js";
33
- import { ReactionManager } from "./3_reaction_manager.js";
35
+ import { InlineQueryManager } from "./3_inline_query_manager.js";
34
36
  const d = debug("Client");
35
37
  const dAuth = debug("Client/authorize");
36
38
  const dRecv = debug("Client/receiveLoop");
@@ -72,9 +74,11 @@ export class Client extends ClientAbstract {
72
74
  _Client_guaranteeUpdateDelivery.set(this, void 0);
73
75
  _Client_updateManager.set(this, void 0);
74
76
  _Client_fileManager.set(this, void 0);
77
+ _Client_reactionManager.set(this, void 0);
75
78
  _Client_messageManager.set(this, void 0);
79
+ _Client_callbackQueryManager.set(this, void 0);
80
+ _Client_inlineQueryManager.set(this, void 0);
76
81
  _Client_chatListManager.set(this, void 0);
77
- _Client_reactionManager.set(this, void 0);
78
82
  Object.defineProperty(this, "storage", {
79
83
  enumerable: true,
80
84
  configurable: true,
@@ -447,7 +451,6 @@ export class Client extends ClientAbstract {
447
451
  }).bind(this)
448
452
  });
449
453
  _Client_storageInited.set(this, false);
450
- _Client_authKeyWasCreated.set(this, true);
451
454
  _Client_connectMutex.set(this, new Mutex());
452
455
  _Client_connectionInited.set(this, false);
453
456
  _Client_lastPropagatedAuthorizationState.set(this, null);
@@ -485,10 +488,6 @@ export class Client extends ClientAbstract {
485
488
  })
486
489
  });
487
490
  _Client_lastGetMe.set(this, null);
488
- _Client_usernameResolver.set(this, async (v) => {
489
- const inputPeer = await this.getInputPeer(v).then((v) => v[as](types.InputPeerUser));
490
- return new types.InputUser(inputPeer);
491
- });
492
491
  //#region Composer
493
492
  _Client_handle.set(this, skip);
494
493
  this.storage = storage ?? new StorageMemory();
@@ -546,7 +545,7 @@ export class Client extends ClientAbstract {
546
545
  api: client.api,
547
546
  connect: async () => {
548
547
  await client.connect();
549
- if (dcId != this.dcId) {
548
+ if (dcId && dcId != this.dcId) {
550
549
  let dc = String(dcId);
551
550
  if (this.dcId < 0) {
552
551
  dc += "-test";
@@ -558,12 +557,15 @@ export class Client extends ClientAbstract {
558
557
  disconnect: client.disconnect.bind(client),
559
558
  };
560
559
  },
560
+ ignoreOutgoing: __classPrivateFieldGet(this, _Client_ignoreOutgoing, "f"),
561
561
  };
562
562
  __classPrivateFieldSet(this, _Client_updateManager, new UpdateManager(c), "f");
563
563
  __classPrivateFieldSet(this, _Client_fileManager, new FileManager(c), "f");
564
+ __classPrivateFieldSet(this, _Client_reactionManager, new ReactionManager(c), "f");
564
565
  __classPrivateFieldSet(this, _Client_messageManager, new MessageManager({ ...c, fileManager: __classPrivateFieldGet(this, _Client_fileManager, "f") }), "f");
566
+ __classPrivateFieldSet(this, _Client_callbackQueryManager, new CallbackQueryManager({ ...c, messageManager: __classPrivateFieldGet(this, _Client_messageManager, "f") }), "f");
567
+ __classPrivateFieldSet(this, _Client_inlineQueryManager, new InlineQueryManager({ ...c, messageManager: __classPrivateFieldGet(this, _Client_messageManager, "f") }), "f");
565
568
  __classPrivateFieldSet(this, _Client_chatListManager, new ChatListManager({ ...c, messageManager: __classPrivateFieldGet(this, _Client_messageManager, "f") }), "f");
566
- __classPrivateFieldSet(this, _Client_reactionManager, new ReactionManager({ ...c, messageManager: __classPrivateFieldGet(this, _Client_messageManager, "f") }), "f");
567
569
  __classPrivateFieldGet(this, _Client_updateManager, "f").setUpdateHandler(__classPrivateFieldGet(this, _Client_instances, "m", _Client_handleUpdate).bind(this));
568
570
  const transportProvider = this.transportProvider;
569
571
  this.transportProvider = (params) => {
@@ -628,10 +630,7 @@ export class Client extends ClientAbstract {
628
630
  * @param dc The DC to change to.
629
631
  */
630
632
  async setDc(dc) {
631
- if (!__classPrivateFieldGet(this, _Client_storageInited, "f")) {
632
- await this.storage.initialize();
633
- __classPrivateFieldSet(this, _Client_storageInited, true, "f");
634
- }
633
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_initStorage).call(this);
635
634
  if (await this.storage.getDc() != dc) {
636
635
  await this.storage.setDc(dc);
637
636
  await this.storage.setAuthKey(null);
@@ -650,13 +649,7 @@ export class Client extends ClientAbstract {
650
649
  }
651
650
  const release = await __classPrivateFieldGet(this, _Client_connectMutex, "f").acquire();
652
651
  try {
653
- if (!__classPrivateFieldGet(this, _Client_storageInited, "f")) {
654
- await this.storage.initialize();
655
- if (!__classPrivateFieldGet(this, _Client_guaranteeUpdateDelivery, "f")) {
656
- await this.storage.deleteUpdates();
657
- }
658
- __classPrivateFieldSet(this, _Client_storageInited, true, "f");
659
- }
652
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_initStorage).call(this);
660
653
  const authKey = await this.storage.getAuthKey();
661
654
  if (authKey == null) {
662
655
  const plain = new ClientPlain({ initialDc: this.initialDc, transportProvider: this.transportProvider, cdn: this.cdn, publicKeys: __classPrivateFieldGet(this, _Client_publicKeys, "f") });
@@ -677,7 +670,6 @@ export class Client extends ClientAbstract {
677
670
  __classPrivateFieldGet(this, _Client_state, "f").salt = await this.storage.getServerSalt() ?? 0n;
678
671
  }
679
672
  await __classPrivateFieldGet(this, _Client_instances, "m", _Client_setAuth).call(this, authKey);
680
- __classPrivateFieldSet(this, _Client_authKeyWasCreated, false, "f");
681
673
  }
682
674
  const dc = await this.storage.getDc();
683
675
  if (dc != null) {
@@ -697,13 +689,21 @@ export class Client extends ClientAbstract {
697
689
  release();
698
690
  }
699
691
  }
700
- async [(_Client_auth = new WeakMap(), _Client_sessionId = new WeakMap(), _Client_state = new WeakMap(), _Client_promises = new WeakMap(), _Client_toAcknowledge = new WeakMap(), _Client_guaranteeUpdateDelivery = new WeakMap(), _Client_updateManager = new WeakMap(), _Client_fileManager = new WeakMap(), _Client_messageManager = new WeakMap(), _Client_chatListManager = new WeakMap(), _Client_reactionManager = new WeakMap(), _Client_parseMode = new WeakMap(), _Client_publicKeys = new WeakMap(), _Client_autoStart = new WeakMap(), _Client_ignoreOutgoing = new WeakMap(), _Client_prefixes = new WeakMap(), _Client_namespaceProxies = new WeakMap(), _Client_constructContext = new WeakMap(), _Client_lastPropagatedConnectionState = new WeakMap(), _Client_storageInited = new WeakMap(), _Client_authKeyWasCreated = new WeakMap(), _Client_connectMutex = new WeakMap(), _Client_connectionInited = new WeakMap(), _Client_lastPropagatedAuthorizationState = new WeakMap(), _Client_selfId = new WeakMap(), _Client_pingLoopAbortSignal = new WeakMap(), _Client_pingInterval = new WeakMap(), _Client_lastUpdates = new WeakMap(), _Client_pingLoopStarted = new WeakMap(), _Client_autoStarted = new WeakMap(), _Client_lastMsgId = new WeakMap(), _Client_handleInvokeError = new WeakMap(), _Client_lastGetMe = new WeakMap(), _Client_usernameResolver = new WeakMap(), _Client_handle = new WeakMap(), _Client_instances = new WeakSet(), _Client_propagateConnectionState = function _Client_propagateConnectionState(connectionState) {
692
+ async [(_Client_auth = new WeakMap(), _Client_sessionId = new WeakMap(), _Client_state = new WeakMap(), _Client_promises = new WeakMap(), _Client_toAcknowledge = new WeakMap(), _Client_guaranteeUpdateDelivery = new WeakMap(), _Client_updateManager = new WeakMap(), _Client_fileManager = new WeakMap(), _Client_reactionManager = new WeakMap(), _Client_messageManager = new WeakMap(), _Client_callbackQueryManager = new WeakMap(), _Client_inlineQueryManager = new WeakMap(), _Client_chatListManager = new WeakMap(), _Client_parseMode = new WeakMap(), _Client_publicKeys = new WeakMap(), _Client_autoStart = new WeakMap(), _Client_ignoreOutgoing = new WeakMap(), _Client_prefixes = new WeakMap(), _Client_namespaceProxies = new WeakMap(), _Client_constructContext = new WeakMap(), _Client_lastPropagatedConnectionState = new WeakMap(), _Client_storageInited = new WeakMap(), _Client_connectMutex = new WeakMap(), _Client_connectionInited = new WeakMap(), _Client_lastPropagatedAuthorizationState = new WeakMap(), _Client_selfId = new WeakMap(), _Client_pingLoopAbortSignal = new WeakMap(), _Client_pingInterval = new WeakMap(), _Client_lastUpdates = new WeakMap(), _Client_pingLoopStarted = new WeakMap(), _Client_autoStarted = new WeakMap(), _Client_lastMsgId = new WeakMap(), _Client_handleInvokeError = new WeakMap(), _Client_lastGetMe = new WeakMap(), _Client_handle = new WeakMap(), _Client_instances = new WeakSet(), _Client_propagateConnectionState = function _Client_propagateConnectionState(connectionState) {
701
693
  __classPrivateFieldGet(this, _Client_instances, "m", _Client_queueHandleCtxUpdate).call(this, { connectionState });
702
694
  __classPrivateFieldSet(this, _Client_lastPropagatedConnectionState, connectionState, "f");
703
695
  }, _Client_setAuth = async function _Client_setAuth(key) {
704
696
  const hash = await sha1(key);
705
697
  const id = bigIntFromBuffer(hash.slice(-8), true, false);
706
698
  __classPrivateFieldSet(this, _Client_auth, { key, id }, "f");
699
+ }, _Client_initStorage = async function _Client_initStorage() {
700
+ if (!__classPrivateFieldGet(this, _Client_storageInited, "f")) {
701
+ await this.storage.initialize();
702
+ if (!__classPrivateFieldGet(this, _Client_guaranteeUpdateDelivery, "f")) {
703
+ await this.storage.deleteUpdates();
704
+ }
705
+ __classPrivateFieldSet(this, _Client_storageInited, true, "f");
706
+ }
707
707
  }, handleMigrationError)](err) {
708
708
  let newDc = String(err.dc);
709
709
  if (Math.abs(this.dcId) >= 10000) {
@@ -897,16 +897,11 @@ export class Client extends ClientAbstract {
897
897
  }
898
898
  }
899
899
  /**
900
- * Same as calling `.connect()` followed by `.authorize(params)` if the session didn't have an auth key.
900
+ * Same as calling `.connect()` followed by `.authorize(params)`.
901
901
  */
902
902
  async start(params) {
903
903
  await this.connect();
904
904
  await __classPrivateFieldGet(this, _Client_instances, "m", _Client_initConnection).call(this);
905
- if (!__classPrivateFieldGet(this, _Client_authKeyWasCreated, "f")) {
906
- drop(__classPrivateFieldGet(this, _Client_updateManager, "f").fetchState("start"));
907
- drop(__classPrivateFieldGet(this, _Client_updateManager, "f").recoverUpdateGap("start"));
908
- return;
909
- }
910
905
  await this.authorize(params);
911
906
  }
912
907
  /**
@@ -1166,6 +1161,10 @@ export class Client extends ClientAbstract {
1166
1161
  const channels = await this.api.channels.getChannels({ id: [new types.InputChannel({ channel_id: channelId, access_hash: 0n })] });
1167
1162
  return channels.chats[0][as](types.Channel).access_hash ?? 0n;
1168
1163
  }, _Client_getInputPeerInner = async function _Client_getInputPeerInner(id) {
1164
+ const idn = Number(id);
1165
+ if (!isNaN(idn)) {
1166
+ id = idn;
1167
+ }
1169
1168
  if (typeof id === "string") {
1170
1169
  id = getUsername(id);
1171
1170
  let userId = 0n;
@@ -1344,6 +1343,77 @@ export class Client extends ClientAbstract {
1344
1343
  __classPrivateFieldSet(this, _Client_lastGetMe, user, "f");
1345
1344
  return user;
1346
1345
  }
1346
+ use(...middleware) {
1347
+ const composer = new Composer(...middleware);
1348
+ __classPrivateFieldSet(this, _Client_handle, concat(__classPrivateFieldGet(this, _Client_handle, "f"), flatten(composer)), "f");
1349
+ return composer;
1350
+ }
1351
+ branch(predicate, trueHandler_, falseHandler_) {
1352
+ const trueHandler = flatten(trueHandler_);
1353
+ const falseHandler = flatten(falseHandler_);
1354
+ return this.use(async (upd, next) => {
1355
+ if (await predicate(upd)) {
1356
+ await trueHandler(upd, next);
1357
+ }
1358
+ else {
1359
+ await falseHandler(upd, next);
1360
+ }
1361
+ });
1362
+ }
1363
+ filter(predicate, ...middleware) {
1364
+ const composer = new Composer(...middleware);
1365
+ this.branch(predicate, composer, skip);
1366
+ return composer;
1367
+ }
1368
+ on(filter, ...middleawre) {
1369
+ return this.filter((ctx) => {
1370
+ return match(filter, ctx);
1371
+ }, ...middleawre);
1372
+ }
1373
+ command(commands, ...middleawre) {
1374
+ const commands__ = typeof commands === "object" && "names" in commands ? commands.names : commands;
1375
+ const commands_ = Array.isArray(commands__) ? commands__ : [commands__];
1376
+ const prefixes_ = typeof commands === "object" && "prefixes" in commands ? commands.prefixes : (__classPrivateFieldGet(this, _Client_prefixes, "f") ?? []);
1377
+ const prefixes = Array.isArray(prefixes_) ? prefixes_ : [prefixes_];
1378
+ for (const left of prefixes) {
1379
+ for (const right of prefixes) {
1380
+ if (left == right) {
1381
+ continue;
1382
+ }
1383
+ if (left.startsWith(right) || right.startsWith(left)) {
1384
+ throw new Error("Intersecting prefixes");
1385
+ }
1386
+ }
1387
+ }
1388
+ return this.on("message:text").filter((ctx) => {
1389
+ const prefixes_ = prefixes.length == 0 ? [!ctx.me?.isBot ? "\\" : "/"] : prefixes;
1390
+ if (prefixes_.length == 0) {
1391
+ return false;
1392
+ }
1393
+ const cmd = ctx.message.text.split(/\s/, 1)[0];
1394
+ const prefix = prefixes_.find((v) => cmd.startsWith(v));
1395
+ if (prefix === undefined) {
1396
+ return false;
1397
+ }
1398
+ if (cmd.includes("@")) {
1399
+ const username = cmd.split("@", 2)[1];
1400
+ if (username.toLowerCase() !== ctx.me.username?.toLowerCase()) {
1401
+ return false;
1402
+ }
1403
+ }
1404
+ const command_ = cmd.split("@", 1)[0].split(prefix, 2)[1].toLowerCase();
1405
+ for (const command of commands_) {
1406
+ if (typeof command === "string" && (command.toLowerCase() == command_)) {
1407
+ return true;
1408
+ }
1409
+ else if (command instanceof RegExp && command.test(command_)) {
1410
+ return true;
1411
+ }
1412
+ }
1413
+ return false;
1414
+ }, ...middleawre);
1415
+ }
1416
+ //#endregion
1347
1417
  /**
1348
1418
  * Answer a callback query. Bot-only.
1349
1419
  *
@@ -1351,13 +1421,7 @@ export class Client extends ClientAbstract {
1351
1421
  * @param id ID of the callback query to answer.
1352
1422
  */
1353
1423
  async answerCallbackQuery(id, params) {
1354
- await this.storage.assertBot("answerCallbackQuery");
1355
- await this.api.messages.setBotCallbackAnswer({
1356
- query_id: BigInt(id),
1357
- cache_time: params?.cacheTime ?? 0,
1358
- message: params?.text,
1359
- alert: params?.alert ? true : undefined,
1360
- });
1424
+ await __classPrivateFieldGet(this, _Client_callbackQueryManager, "f").answerCallbackQuery(id, params);
1361
1425
  }
1362
1426
  /**
1363
1427
  * Send a poll.
@@ -1460,86 +1524,7 @@ export class Client extends ClientAbstract {
1460
1524
  * @param results The results to answer with.
1461
1525
  */
1462
1526
  async answerInlineQuery(id, results, params) {
1463
- await this.api.messages.setInlineBotResults({
1464
- query_id: BigInt(id),
1465
- results: await Promise.all(results.map((v) => inlineQueryResultToTlObject(v, __classPrivateFieldGet(this, _Client_instances, "m", _Client_parseText).bind(this), __classPrivateFieldGet(this, _Client_usernameResolver, "f").bind(this)))),
1466
- cache_time: params?.cacheTime ?? 300,
1467
- private: params?.isPersonal ? true : undefined,
1468
- switch_webview: params?.button && params.button.webApp ? new types.InlineBotWebView({ text: params.button.text, url: params.button.webApp.url }) : undefined,
1469
- switch_pm: params?.button && params.button.startParameter ? new types.InlineBotSwitchPM({ text: params.button.text, start_param: params.button.startParameter }) : undefined,
1470
- gallery: params?.isGallery ? true : undefined,
1471
- next_offset: params?.nextOffset,
1472
- });
1473
- }
1474
- use(...middleware) {
1475
- const composer = new Composer(...middleware);
1476
- __classPrivateFieldSet(this, _Client_handle, concat(__classPrivateFieldGet(this, _Client_handle, "f"), flatten(composer)), "f");
1477
- return composer;
1478
- }
1479
- branch(predicate, trueHandler_, falseHandler_) {
1480
- const trueHandler = flatten(trueHandler_);
1481
- const falseHandler = flatten(falseHandler_);
1482
- return this.use(async (upd, next) => {
1483
- if (await predicate(upd)) {
1484
- await trueHandler(upd, next);
1485
- }
1486
- else {
1487
- await falseHandler(upd, next);
1488
- }
1489
- });
1490
- }
1491
- filter(predicate, ...middleware) {
1492
- const composer = new Composer(...middleware);
1493
- this.branch(predicate, composer, skip);
1494
- return composer;
1495
- }
1496
- on(filter, ...middleawre) {
1497
- return this.filter((ctx) => {
1498
- return match(filter, ctx);
1499
- }, ...middleawre);
1500
- }
1501
- command(commands, ...middleawre) {
1502
- const commands__ = typeof commands === "object" && "names" in commands ? commands.names : commands;
1503
- const commands_ = Array.isArray(commands__) ? commands__ : [commands__];
1504
- const prefixes_ = typeof commands === "object" && "prefixes" in commands ? commands.prefixes : (__classPrivateFieldGet(this, _Client_prefixes, "f") ?? []);
1505
- const prefixes = Array.isArray(prefixes_) ? prefixes_ : [prefixes_];
1506
- for (const left of prefixes) {
1507
- for (const right of prefixes) {
1508
- if (left == right) {
1509
- continue;
1510
- }
1511
- if (left.startsWith(right) || right.startsWith(left)) {
1512
- throw new Error("Intersecting prefixes");
1513
- }
1514
- }
1515
- }
1516
- return this.on("message:text").filter((ctx) => {
1517
- const prefixes_ = prefixes.length == 0 ? [!ctx.me?.isBot ? "\\" : "/"] : prefixes;
1518
- if (prefixes_.length == 0) {
1519
- return false;
1520
- }
1521
- const cmd = ctx.message.text.split(/\s/, 1)[0];
1522
- const prefix = prefixes_.find((v) => cmd.startsWith(v));
1523
- if (prefix === undefined) {
1524
- return false;
1525
- }
1526
- if (cmd.includes("@")) {
1527
- const username = cmd.split("@", 2)[1];
1528
- if (username.toLowerCase() !== ctx.me.username?.toLowerCase()) {
1529
- return false;
1530
- }
1531
- }
1532
- const command_ = cmd.split("@", 1)[0].split(prefix, 2)[1].toLowerCase();
1533
- for (const command of commands_) {
1534
- if (typeof command === "string" && (command.toLowerCase() == command_)) {
1535
- return true;
1536
- }
1537
- else if (command instanceof RegExp && command.test(command_)) {
1538
- return true;
1539
- }
1540
- }
1541
- return false;
1542
- }, ...middleawre);
1527
+ await __classPrivateFieldGet(this, _Client_inlineQueryManager, "f").answerInlineQuery(id, results, params);
1543
1528
  }
1544
1529
  /**
1545
1530
  * Set the bot's description in the given language. Bot-only.
@@ -1765,69 +1750,7 @@ export class Client extends ClientAbstract {
1765
1750
  * @method
1766
1751
  */
1767
1752
  async getChat(chatId) {
1768
- if (await this.storage.getAccountType() == "user") {
1769
- let maybeChatId = null;
1770
- if (typeof chatId === "number") {
1771
- maybeChatId = chatId;
1772
- }
1773
- else if (typeof chatId === "string") {
1774
- maybeChatId = __classPrivateFieldGet(this, _Client_chatListManager, "f").tryGetChatId(getUsername(chatId));
1775
- }
1776
- else {
1777
- UNREACHABLE();
1778
- }
1779
- if (maybeChatId != null) {
1780
- const [chat] = __classPrivateFieldGet(this, _Client_chatListManager, "f").getChatAnywhere(maybeChatId);
1781
- if (chat !== undefined) {
1782
- return chat;
1783
- }
1784
- }
1785
- }
1786
- let inputPeer = null;
1787
- if (typeof chatId === "number") {
1788
- const chat = await constructChat3(chatId, -1, undefined, this[getEntity].bind(this));
1789
- if (chat != null) {
1790
- return chat;
1791
- }
1792
- }
1793
- else {
1794
- inputPeer = await this.getInputPeer(chatId);
1795
- const chatId_ = peerToChatId(inputPeer);
1796
- const chat = await constructChat3(chatId_, -1, undefined, this[getEntity].bind(this));
1797
- if (chat != null) {
1798
- return chat;
1799
- }
1800
- }
1801
- if (inputPeer == null) {
1802
- inputPeer = await this.getInputPeer(chatId);
1803
- }
1804
- if (inputPeer instanceof types.InputPeerChat) {
1805
- const chats = await this.api.messages.getChats({ id: [inputPeer.chat_id] }).then((v) => v[as](types.messages.Chats));
1806
- const chat = chats.chats[0];
1807
- if (chat instanceof types.ChatEmpty) {
1808
- UNREACHABLE();
1809
- }
1810
- return constructChat2(chat, -1, undefined);
1811
- }
1812
- else if (inputPeer instanceof types.InputPeerChannel) {
1813
- const channels = await this.api.channels.getChannels({ id: [new types.InputChannel(inputPeer)] });
1814
- const channel = channels.chats[0];
1815
- if (channel instanceof types.ChatEmpty) {
1816
- UNREACHABLE();
1817
- }
1818
- return constructChat2(channel, -1, undefined);
1819
- }
1820
- else if (inputPeer instanceof types.InputPeerUser) {
1821
- const users = await this.api.users.getUsers({ id: [new types.InputUser(inputPeer)] });
1822
- const user = users[0];
1823
- if (user instanceof types.UserEmpty) {
1824
- UNREACHABLE();
1825
- }
1826
- return constructChat2(user, -1, undefined);
1827
- }
1828
- else {
1829
- UNREACHABLE();
1830
- }
1753
+ return await __classPrivateFieldGet(this, _Client_chatListManager, "f").getChat(chatId);
1831
1754
  }
1832
1755
  /**
1833
1756
  * Get chat history. User-only.
@@ -1855,7 +1778,7 @@ export class Client extends ClientAbstract {
1855
1778
  * @param availableReactions The new available reactions.
1856
1779
  */
1857
1780
  async setAvailableReactions(chatId, availableReactions) {
1858
- await __classPrivateFieldGet(this, _Client_reactionManager, "f").setAvailableReactions(chatId, availableReactions);
1781
+ await __classPrivateFieldGet(this, _Client_messageManager, "f").setAvailableReactions(chatId, availableReactions);
1859
1782
  }
1860
1783
  /**
1861
1784
  * Change reactions made to a message.
@@ -1866,7 +1789,7 @@ export class Client extends ClientAbstract {
1866
1789
  * @param reactions The new reactions.
1867
1790
  */
1868
1791
  async setReactions(chatId, messageId, reactions, params) {
1869
- await __classPrivateFieldGet(this, _Client_reactionManager, "f").setReactions(chatId, messageId, reactions, params);
1792
+ await __classPrivateFieldGet(this, _Client_messageManager, "f").setReactions(chatId, messageId, reactions, params);
1870
1793
  }
1871
1794
  /**
1872
1795
  * Make a reaction to a message.
@@ -1877,7 +1800,7 @@ export class Client extends ClientAbstract {
1877
1800
  * @param reaction The reaction to add.
1878
1801
  */
1879
1802
  async addReaction(chatId, messageId, reaction, params) {
1880
- await __classPrivateFieldGet(this, _Client_reactionManager, "f").addReaction(chatId, messageId, reaction, params);
1803
+ await __classPrivateFieldGet(this, _Client_messageManager, "f").addReaction(chatId, messageId, reaction, params);
1881
1804
  }
1882
1805
  /**
1883
1806
  * Undo a reaction made to a message.
@@ -1888,7 +1811,7 @@ export class Client extends ClientAbstract {
1888
1811
  * @param reaction The reaction to remove.
1889
1812
  */
1890
1813
  async removeReaction(chatId, messageId, reaction) {
1891
- await __classPrivateFieldGet(this, _Client_reactionManager, "f").removeReaction(chatId, messageId, reaction);
1814
+ await __classPrivateFieldGet(this, _Client_messageManager, "f").removeReaction(chatId, messageId, reaction);
1892
1815
  }
1893
1816
  /**
1894
1817
  * Set a chat's photo.
@@ -2134,151 +2057,41 @@ _Client_parseText = function _Client_parseText(text, params) {
2134
2057
  await this.storage.setEntity(entity);
2135
2058
  }
2136
2059
  }
2137
- if (update instanceof types.UpdateNewMessage || update instanceof types.UpdateNewChannelMessage || update instanceof types.UpdateEditMessage || update instanceof types.UpdateEditChannelMessage) {
2138
- if (update.message instanceof types.Message || update.message instanceof types.MessageService) {
2139
- const chatId = peerToChatId(update.message.peer_id);
2140
- await this.storage.setMessage(chatId, update.message.id, update.message);
2141
- promises.push(__classPrivateFieldGet(this, _Client_chatListManager, "f").reassignChatLastMessage(chatId));
2142
- }
2143
- }
2144
- if (update instanceof types.UpdateMessageReactions) {
2145
- const chatId = peerToChatId(update.peer);
2146
- const message = await this.storage.getMessage(chatId, update.msg_id);
2147
- if (message instanceof types.Message) {
2148
- message.reactions = update.reactions;
2149
- await this.storage.setMessage(chatId, update.msg_id, message);
2150
- const views = message.views ?? 0;
2151
- const forwards = message.forwards ?? 0;
2152
- const recentReactions = update.reactions.recent_reactions ?? [];
2153
- const reactions = update.reactions.results.map((v) => constructMessageReaction(v, recentReactions));
2154
- promises.push(__classPrivateFieldGet(this, _Client_instances, "m", _Client_handleCtxUpdate).call(this, { messageInteractions: { chatId, messageId: update.msg_id, reactions, views, forwards } }));
2155
- }
2156
- }
2157
- else if (update instanceof types.UpdateChannelMessageViews || update instanceof types.UpdateChannelMessageForwards) {
2158
- const chatId = peerToChatId(new types.PeerChannel(update));
2159
- const message = await this.storage.getMessage(chatId, update.id);
2160
- if (message instanceof types.Message) {
2161
- if ("views" in update) {
2162
- message.views = update.views;
2163
- }
2164
- if ("forwards" in update) {
2165
- message.forwards = update.forwards;
2166
- }
2167
- const views = message.views ?? 0;
2168
- const forwards = message.forwards ?? 0;
2169
- const recentReactions = message.reactions?.recent_reactions ?? [];
2170
- const reactions = message.reactions?.results.map((v) => constructMessageReaction(v, recentReactions)) ?? [];
2171
- promises.push(__classPrivateFieldGet(this, _Client_instances, "m", _Client_handleCtxUpdate).call(this, { messageInteractions: { chatId, messageId: update.id, reactions, views, forwards } }));
2172
- }
2173
- }
2174
- if (update instanceof types.UpdateNewMessage ||
2175
- update instanceof types.UpdateNewChannelMessage ||
2176
- update instanceof types.UpdateEditMessage ||
2177
- update instanceof types.UpdateEditChannelMessage) {
2178
- if (!(update.message instanceof types.MessageEmpty)) {
2179
- const isOutgoing = update.message.out;
2180
- let shouldIgnore = isOutgoing ? (await this.storage.getAccountType()) == "user" ? false : true : false;
2181
- if (__classPrivateFieldGet(this, _Client_ignoreOutgoing, "f") != null && isOutgoing) {
2182
- shouldIgnore = __classPrivateFieldGet(this, _Client_ignoreOutgoing, "f");
2183
- }
2184
- if (!shouldIgnore) {
2185
- const message = await __classPrivateFieldGet(this, _Client_messageManager, "f").constructMessage(update.message);
2186
- promises.push((async () => {
2187
- if (update instanceof types.UpdateNewMessage || update instanceof types.UpdateNewChannelMessage) {
2188
- await __classPrivateFieldGet(this, _Client_instances, "m", _Client_handleCtxUpdate).call(this, { message });
2189
- }
2190
- else {
2191
- await __classPrivateFieldGet(this, _Client_instances, "m", _Client_handleCtxUpdate).call(this, { editedMessage: message });
2192
- }
2193
- })());
2194
- }
2195
- }
2196
- }
2197
- if (update instanceof types.UpdateDeleteMessages) {
2198
- const deletedMessages = new Array();
2199
- for (const messageId of update.messages) {
2200
- const chatId = await this.storage.getMessageChat(messageId);
2201
- if (chatId) {
2202
- deletedMessages.push({ chatId, messageId });
2203
- }
2204
- }
2205
- if (deletedMessages.length > 0) {
2060
+ if (MessageManager.canHandleUpdate(update)) {
2061
+ const update_ = await __classPrivateFieldGet(this, _Client_messageManager, "f").handleUpdate(update);
2062
+ if (update_) {
2206
2063
  promises.push((async () => {
2207
2064
  try {
2208
- await __classPrivateFieldGet(this, _Client_instances, "m", _Client_handleCtxUpdate).call(this, { deletedMessages });
2065
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_handleCtxUpdate).call(this, update_);
2209
2066
  }
2210
2067
  finally {
2211
- for (const { chatId, messageId } of deletedMessages) {
2212
- await this.storage.setMessage(chatId, messageId, null);
2213
- await __classPrivateFieldGet(this, _Client_chatListManager, "f").reassignChatLastMessage(chatId);
2214
- }
2215
- }
2216
- })());
2217
- }
2218
- }
2219
- else if (update instanceof types.UpdateDeleteChannelMessages) {
2220
- const chatId = getChannelChatId(update.channel_id);
2221
- const deletedMessages = new Array();
2222
- for (const messageId of update.messages) {
2223
- const message = await this.storage.getMessage(chatId, messageId);
2224
- if (message != null) {
2225
- deletedMessages.push({ chatId, messageId });
2226
- }
2227
- }
2228
- if (deletedMessages.length > 0) {
2229
- promises.push((async () => {
2230
- try {
2231
- await __classPrivateFieldGet(this, _Client_instances, "m", _Client_handleCtxUpdate).call(this, { deletedMessages });
2232
- }
2233
- finally {
2234
- for (const { chatId, messageId } of deletedMessages) {
2235
- await this.storage.setMessage(chatId, messageId, null);
2236
- await __classPrivateFieldGet(this, _Client_chatListManager, "f").reassignChatLastMessage(chatId);
2068
+ if ("deletedMessages" in update_) {
2069
+ for (const { chatId, messageId } of update_.deletedMessages) {
2070
+ await this.storage.setMessage(chatId, messageId, null);
2071
+ await __classPrivateFieldGet(this, _Client_chatListManager, "f").reassignChatLastMessage(chatId);
2072
+ }
2237
2073
  }
2238
2074
  }
2239
2075
  })());
2240
2076
  }
2241
2077
  }
2242
- if (update instanceof types.UpdateBotCallbackQuery || update instanceof types.UpdateInlineBotCallbackQuery) {
2243
- promises.push(__classPrivateFieldGet(this, _Client_instances, "m", _Client_handleCtxUpdate).call(this, { callbackQuery: await constructCallbackQuery(update, this[getEntity].bind(this), __classPrivateFieldGet(this, _Client_messageManager, "f").getMessageWithReply.bind(this)) }));
2244
- }
2245
- else if (update instanceof types.UpdateBotInlineQuery) {
2246
- promises.push(__classPrivateFieldGet(this, _Client_instances, "m", _Client_handleCtxUpdate).call(this, { inlineQuery: await constructInlineQuery(update, this[getEntity].bind(this)) }));
2078
+ if (CallbackQueryManager.canHandleUpdate(update)) {
2079
+ promises.push(__classPrivateFieldGet(this, _Client_instances, "m", _Client_handleCtxUpdate).call(this, await __classPrivateFieldGet(this, _Client_callbackQueryManager, "f").handleUpdate(update)));
2247
2080
  }
2248
- else if (update instanceof types.UpdateBotInlineSend) {
2249
- promises.push(__classPrivateFieldGet(this, _Client_instances, "m", _Client_handleCtxUpdate).call(this, { chosenInlineResult: await constructChosenInlineResult(update, this[getEntity].bind(this)) }));
2081
+ if (InlineQueryManager.canHandleUpdate(update)) {
2082
+ promises.push(__classPrivateFieldGet(this, _Client_instances, "m", _Client_handleCtxUpdate).call(this, await __classPrivateFieldGet(this, _Client_inlineQueryManager, "f").handleUpdate(update)));
2250
2083
  }
2251
- else if (update instanceof types.UpdateBotMessageReactions) {
2252
- const messageReactionCount = await constructMessageReactionCount(update, this[getEntity].bind(this));
2253
- if (messageReactionCount) {
2254
- promises.push(__classPrivateFieldGet(this, _Client_instances, "m", _Client_handleCtxUpdate).call(this, { messageReactionCount }));
2084
+ if (ReactionManager.canHandleUpdate(update)) {
2085
+ const upd = await __classPrivateFieldGet(this, _Client_reactionManager, "f").handleUpdate(update);
2086
+ if (upd) {
2087
+ promises.push(__classPrivateFieldGet(this, _Client_instances, "m", _Client_handleCtxUpdate).call(this, upd));
2255
2088
  }
2256
2089
  }
2257
- else if (update instanceof types.UpdateBotMessageReaction) {
2258
- const messageReactions = await constructMessageReactions(update, this[getEntity].bind(this));
2259
- if (messageReactions) {
2260
- promises.push(__classPrivateFieldGet(this, _Client_instances, "m", _Client_handleCtxUpdate).call(this, { messageReactions }));
2261
- }
2262
- }
2263
- if (update instanceof types.UpdatePinnedDialogs) {
2264
- await __classPrivateFieldGet(this, _Client_chatListManager, "f").handleUpdatePinnedDialogs(update);
2265
- }
2266
- else if (update instanceof types.UpdateFolderPeers) {
2267
- await __classPrivateFieldGet(this, _Client_chatListManager, "f").handelUpdateFolderPeers(update);
2268
- }
2269
- if (update instanceof types.UpdateChannel) {
2270
- await __classPrivateFieldGet(this, _Client_chatListManager, "f").handleUpdateChannel(update);
2271
- }
2272
- else if (update instanceof types.UpdateChat) {
2273
- await __classPrivateFieldGet(this, _Client_chatListManager, "f").handleUpdateChat(update);
2274
- }
2275
- else if (update instanceof types.UpdateUser || update instanceof types.UpdateUserName) {
2276
- await __classPrivateFieldGet(this, _Client_chatListManager, "f").handleUpdateUser(update);
2090
+ if (ChatListManager.canHandleUpdate(update)) {
2091
+ await __classPrivateFieldGet(this, _Client_chatListManager, "f").handleUpdate(update);
2277
2092
  }
2278
2093
  return () => Promise.all(promises);
2279
- }, _Client_setMyInfo =
2280
- //#endregion
2281
- async function _Client_setMyInfo(info) {
2094
+ }, _Client_setMyInfo = async function _Client_setMyInfo(info) {
2282
2095
  await this.api.bots.setBotInfo({ bot: new types.InputUserSelf(), ...info });
2283
2096
  }, _Client_getMyInfo = function _Client_getMyInfo(languageCode) {
2284
2097
  return this.api.bots.getBotInfo({ bot: new types.InputUserSelf(), lang_code: languageCode ?? "" });
@@ -0,0 +1,3 @@
1
+ import { Client } from "./4_client.js";
2
+ export declare function exportSessionString(client: Client): Promise<string>;
3
+ export declare function importSessionString(client: Client, string: string): Promise<void>;