@mtkruto/node 0.78.0 → 0.78.1

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 (129) hide show
  1. package/esm/0_errors.d.ts +1 -0
  2. package/esm/0_errors.d.ts.map +1 -1
  3. package/esm/0_errors.js +5 -0
  4. package/esm/3_types.d.ts +3 -0
  5. package/esm/3_types.d.ts.map +1 -1
  6. package/esm/3_types.js +3 -0
  7. package/esm/4_constants.d.ts +1 -0
  8. package/esm/4_constants.d.ts.map +1 -1
  9. package/esm/4_constants.js +2 -0
  10. package/esm/5_client.d.ts +6 -1
  11. package/esm/5_client.d.ts.map +1 -1
  12. package/esm/5_client.js +6 -1
  13. package/esm/_dnt.polyfills.d.ts +13 -13
  14. package/esm/_dnt.polyfills.d.ts.map +1 -1
  15. package/esm/_dnt.polyfills.js +11 -11
  16. package/esm/client/0_worker_request.d.ts +26 -0
  17. package/esm/client/0_worker_request.d.ts.map +1 -0
  18. package/esm/client/0_worker_request.js +20 -0
  19. package/esm/client/0_worker_response.d.ts +39 -0
  20. package/esm/client/0_worker_response.d.ts.map +1 -0
  21. package/esm/client/0_worker_response.js +20 -0
  22. package/esm/client/1_client_generic.d.ts +1602 -0
  23. package/esm/client/1_client_generic.d.ts.map +1 -0
  24. package/esm/client/1_client_generic.js +24 -0
  25. package/esm/client/2_account_manager.d.ts +6 -1
  26. package/esm/client/2_account_manager.d.ts.map +1 -1
  27. package/esm/client/2_account_manager.js +84 -0
  28. package/esm/client/2_context.d.ts +114 -0
  29. package/esm/client/2_context.d.ts.map +1 -0
  30. package/esm/client/2_context.js +453 -0
  31. package/esm/client/2_file_manager.d.ts.map +1 -1
  32. package/esm/client/2_file_manager.js +3 -4
  33. package/esm/client/2_sign_in.d.ts +25 -0
  34. package/esm/client/2_sign_in.d.ts.map +1 -0
  35. package/esm/client/2_sign_in.js +114 -0
  36. package/esm/client/{0_filters.d.ts → 3_filters.d.ts} +6 -3
  37. package/esm/client/3_filters.d.ts.map +1 -0
  38. package/esm/client/3_filters_test.d.ts.map +1 -0
  39. package/esm/client/{1_composer.d.ts → 4_composer.d.ts} +12 -11
  40. package/esm/client/4_composer.d.ts.map +1 -0
  41. package/esm/client/{1_composer.js → 4_composer.js} +17 -7
  42. package/esm/client/4_composer_test.d.ts.map +1 -0
  43. package/esm/client/5_client.d.ts +43 -179
  44. package/esm/client/5_client.d.ts.map +1 -1
  45. package/esm/client/5_client.js +141 -595
  46. package/esm/client/5_client_dispatcher.d.ts +1660 -0
  47. package/esm/client/5_client_dispatcher.d.ts.map +1 -0
  48. package/esm/client/5_client_dispatcher.js +2141 -0
  49. package/esm/client/6_client_worker.d.ts +27 -0
  50. package/esm/client/6_client_worker.d.ts.map +1 -0
  51. package/esm/client/6_client_worker.js +51 -0
  52. package/esm/types/0_bot_token_check_result.d.ts +29 -0
  53. package/esm/types/0_bot_token_check_result.d.ts.map +1 -0
  54. package/esm/types/0_bot_token_check_result.js +20 -0
  55. package/esm/types/0_code_check_result.d.ts +37 -0
  56. package/esm/types/0_code_check_result.d.ts.map +1 -0
  57. package/esm/types/0_code_check_result.js +20 -0
  58. package/esm/types/0_password_check_result.d.ts +33 -0
  59. package/esm/types/0_password_check_result.d.ts.map +1 -0
  60. package/esm/types/0_password_check_result.js +20 -0
  61. package/package.json +1 -1
  62. package/script/0_errors.d.ts +1 -0
  63. package/script/0_errors.d.ts.map +1 -1
  64. package/script/0_errors.js +5 -0
  65. package/script/3_types.d.ts +3 -0
  66. package/script/3_types.d.ts.map +1 -1
  67. package/script/3_types.js +3 -0
  68. package/script/4_constants.d.ts +1 -0
  69. package/script/4_constants.d.ts.map +1 -1
  70. package/script/4_constants.js +3 -1
  71. package/script/5_client.d.ts +6 -1
  72. package/script/5_client.d.ts.map +1 -1
  73. package/script/5_client.js +6 -1
  74. package/script/_dnt.polyfills.d.ts +13 -13
  75. package/script/_dnt.polyfills.d.ts.map +1 -1
  76. package/script/_dnt.polyfills.js +11 -11
  77. package/script/client/0_worker_request.d.ts +26 -0
  78. package/script/client/0_worker_request.d.ts.map +1 -0
  79. package/script/client/0_worker_request.js +21 -0
  80. package/script/client/0_worker_response.d.ts +39 -0
  81. package/script/client/0_worker_response.d.ts.map +1 -0
  82. package/script/client/0_worker_response.js +21 -0
  83. package/script/client/1_client_generic.d.ts +1602 -0
  84. package/script/client/1_client_generic.d.ts.map +1 -0
  85. package/script/client/1_client_generic.js +28 -0
  86. package/script/client/2_account_manager.d.ts +6 -1
  87. package/script/client/2_account_manager.d.ts.map +1 -1
  88. package/script/client/2_account_manager.js +84 -0
  89. package/script/client/2_context.d.ts +114 -0
  90. package/script/client/2_context.d.ts.map +1 -0
  91. package/script/client/2_context.js +457 -0
  92. package/script/client/2_file_manager.d.ts.map +1 -1
  93. package/script/client/2_file_manager.js +2 -3
  94. package/script/client/2_sign_in.d.ts +25 -0
  95. package/script/client/2_sign_in.d.ts.map +1 -0
  96. package/script/client/2_sign_in.js +118 -0
  97. package/script/client/{0_filters.d.ts → 3_filters.d.ts} +6 -3
  98. package/script/client/3_filters.d.ts.map +1 -0
  99. package/script/client/3_filters_test.d.ts.map +1 -0
  100. package/script/client/{1_composer.d.ts → 4_composer.d.ts} +12 -11
  101. package/script/client/4_composer.d.ts.map +1 -0
  102. package/script/client/{1_composer.js → 4_composer.js} +17 -7
  103. package/script/client/4_composer_test.d.ts.map +1 -0
  104. package/script/client/5_client.d.ts +43 -179
  105. package/script/client/5_client.d.ts.map +1 -1
  106. package/script/client/5_client.js +140 -594
  107. package/script/client/5_client_dispatcher.d.ts +1660 -0
  108. package/script/client/5_client_dispatcher.d.ts.map +1 -0
  109. package/script/client/5_client_dispatcher.js +2178 -0
  110. package/script/client/6_client_worker.d.ts +27 -0
  111. package/script/client/6_client_worker.d.ts.map +1 -0
  112. package/script/client/6_client_worker.js +55 -0
  113. package/script/types/0_bot_token_check_result.d.ts +29 -0
  114. package/script/types/0_bot_token_check_result.d.ts.map +1 -0
  115. package/script/types/0_bot_token_check_result.js +21 -0
  116. package/script/types/0_code_check_result.d.ts +37 -0
  117. package/script/types/0_code_check_result.d.ts.map +1 -0
  118. package/script/types/0_code_check_result.js +21 -0
  119. package/script/types/0_password_check_result.d.ts +33 -0
  120. package/script/types/0_password_check_result.d.ts.map +1 -0
  121. package/script/types/0_password_check_result.js +21 -0
  122. package/esm/client/0_filters.d.ts.map +0 -1
  123. package/esm/client/0_filters_test.d.ts.map +0 -1
  124. package/esm/client/1_composer.d.ts.map +0 -1
  125. package/script/client/0_filters.d.ts.map +0 -1
  126. package/script/client/0_filters_test.d.ts.map +0 -1
  127. package/script/client/1_composer.d.ts.map +0 -1
  128. /package/esm/client/{0_filters.js → 3_filters.js} +0 -0
  129. /package/script/client/{0_filters.js → 3_filters.js} +0 -0
@@ -19,20 +19,17 @@
19
19
  */
20
20
  import { delay, MINUTE, SECOND, unreachable } from "../0_deps.js";
21
21
  import { AccessError, ConnectionError, InputError } from "../0_errors.js";
22
- import { cleanObject, drop, getLogger, mustPrompt, mustPromptOneOf, Mutex, ZERO_CHANNEL_ID } from "../1_utilities.js";
22
+ import { drop, getLogger, Mutex, ZERO_CHANNEL_ID } from "../1_utilities.js";
23
23
  import { StorageMemory } from "../2_storage.js";
24
- import { Api, Mtproto, toJSON } from "../2_tl.js";
24
+ import { Api, Mtproto } from "../2_tl.js";
25
25
  import { getDcId } from "../3_transport.js";
26
26
  import { constructChatP, constructUser2 } from "../3_types.js";
27
27
  import { APP_VERSION, DEVICE_MODEL, INITIAL_DC, LANG_CODE, LANG_PACK, MAX_CHANNEL_ID, MAX_CHAT_ID, SYSTEM_LANG_CODE, SYSTEM_VERSION, USERNAME_TTL } from "../4_constants.js";
28
- import { AuthKeyUnregistered, FloodWait, Migrate, PasswordHashInvalid, PhoneNumberInvalid, SessionPasswordNeeded, SessionRevoked } from "../4_errors.js";
29
- import { PhoneCodeInvalid } from "../4_errors.js";
28
+ import { AuthKeyUnregistered, FloodWait, Migrate, SessionRevoked } from "../4_errors.js";
30
29
  import { peerToChatId } from "../tl/2_telegram.js";
31
30
  import { AbortableLoop } from "./0_abortable_loop.js";
32
- import { checkPassword } from "./0_password.js";
33
31
  import { StorageOperations } from "./0_storage_operations.js";
34
- import { canBeInputChannel, canBeInputUser, DOWNLOAD_POOL_SIZE, getUsername, resolve, toInputChannel, toInputUser } from "./0_utilities.js";
35
- import { Composer as Composer_ } from "./1_composer.js";
32
+ import { canBeInputChannel, canBeInputUser, DOWNLOAD_POOL_SIZE, getUsername, toInputChannel, toInputUser } from "./0_utilities.js";
36
33
  import { AccountManager } from "./2_account_manager.js";
37
34
  import { BotInfoManager } from "./2_bot_info_manager.js";
38
35
  import { BusinessConnectionManager } from "./2_business_connection_manager.js";
@@ -41,6 +38,7 @@ import { FileManager } from "./2_file_manager.js";
41
38
  import { NetworkStatisticsManager } from "./2_network_statistics_manager.js";
42
39
  import { PaymentManager } from "./2_payment_manager.js";
43
40
  import { ReactionManager } from "./2_reaction_manager.js";
41
+ import { signIn } from "./2_sign_in.js";
44
42
  import { TranslationsManager } from "./2_translations_manager.js";
45
43
  import { UpdateManager } from "./2_update_manager.js";
46
44
  import { ClientEncryptedPool } from "./3_client_encrypted_pool.js";
@@ -49,18 +47,17 @@ import { VideoChatManager } from "./3_video_chat_manager.js";
49
47
  import { CallbackQueryManager } from "./4_callback_query_manager.js";
50
48
  import { ChatListManager } from "./4_chat_list_manager.js";
51
49
  import { ChatManager } from "./4_chat_manager.js";
50
+ import { Composer } from "./4_composer.js";
52
51
  import { ForumManager } from "./4_forum_manager.js";
53
52
  import { GiftManager } from "./4_gift_manager.js";
54
53
  import { InlineQueryManager } from "./4_inline_query_manager.js";
55
54
  import { LinkPreviewManager } from "./4_link_preview_manager.js";
56
55
  import { PollManager } from "./4_poll_manager.js";
57
56
  import { StoryManager } from "./4_story_manager.js";
58
- export class Composer extends Composer_ {
59
- }
57
+ export { restartAuth } from "./2_sign_in.js";
60
58
  function skipInvoke() {
61
59
  return (_ctx, next) => next();
62
60
  }
63
- export const restartAuth = Symbol("restartAuth");
64
61
  export const handleMigrationError = Symbol("handleMigrationError");
65
62
  // global Client ID counter for logs
66
63
  let id = 0;
@@ -183,9 +180,6 @@ export class Client extends Composer {
183
180
  this.systemVersion = params?.systemVersion ?? SYSTEM_VERSION;
184
181
  this.#publicKeys = params?.publicKeys;
185
182
  this.#outgoingMessages = params?.outgoingMessages ?? false;
186
- if (params?.prefixes) {
187
- this.prefixes = params?.prefixes;
188
- }
189
183
  this.#guaranteeUpdateDelivery = params?.guaranteeUpdateDelivery ?? false;
190
184
  const L = this.#L = getLogger("Client").client(id++);
191
185
  this.#LsignIn = L.branch("signIn");
@@ -316,432 +310,6 @@ export class Client extends Composer {
316
310
  get disconnected() {
317
311
  return this.#client?.disconnected ?? true;
318
312
  }
319
- #constructContext = async (update) => {
320
- const mustGetMsg = (ctx) => {
321
- if (ctx.msg !== undefined) {
322
- return { chatId: ctx.msg.chat.id, messageId: ctx.msg.id, businessConnectionId: ctx.msg.businessConnectionId, senderId: ctx.msg.from?.id, userId: ctx.msg.from?.id };
323
- }
324
- const reactions = "messageInteractions" in update ? update.messageInteractions : undefined;
325
- if (reactions !== undefined) {
326
- return { chatId: reactions.chatId, messageId: reactions.messageId };
327
- }
328
- else {
329
- unreachable();
330
- }
331
- };
332
- const mustGetChatId = (ctx) => {
333
- if (ctx.chat) {
334
- return ctx.chat.id;
335
- }
336
- else {
337
- unreachable();
338
- }
339
- };
340
- const mustGetUserId = (ctx) => {
341
- if (ctx.msg?.from) {
342
- return ctx.msg.from.id;
343
- }
344
- else if ("callbackQuery" in update) {
345
- return update.callbackQuery.from.id;
346
- }
347
- else if ("chosenInlineResult" in update) {
348
- return update.chosenInlineResult.from.id;
349
- }
350
- else {
351
- unreachable();
352
- }
353
- };
354
- const mustGetInlineMsgId = () => {
355
- if ("chosenInlineResult" in update) {
356
- if (update.chosenInlineResult.inlineMessageId) {
357
- return update.chosenInlineResult.inlineMessageId;
358
- }
359
- }
360
- else if ("callbackQuery" in update) {
361
- if (update.callbackQuery.inlineMessageId) {
362
- return update.callbackQuery.inlineMessageId;
363
- }
364
- }
365
- unreachable();
366
- };
367
- const getReplyTo = (quote, chatId, messageId) => {
368
- if ("story" in update) {
369
- return { chatId: update.story.chat.id, storyId: update.story.id };
370
- }
371
- const isPrivate = chatId > 0;
372
- const shouldQuote = quote === undefined ? !isPrivate : quote;
373
- return shouldQuote ? { messageId } : undefined;
374
- };
375
- if (this.#lastGetMe === null && !("connectionState" in update) && (!("authorizationState" in update) || ("authorizationState" in update && update.authorizationState.isAuthorized))) {
376
- await this.#getMe();
377
- }
378
- const context = {
379
- ...update,
380
- client: this,
381
- get me() {
382
- return this.client.#lastGetMe === null ? undefined : this.client.#lastGetMe;
383
- },
384
- get msg() {
385
- return "message" in update ? update.message : "editedMessage" in update ? update.editedMessage : "scheduledMessage" in update ? update.scheduledMessage : "callbackQuery" in update ? update.callbackQuery.message : undefined;
386
- },
387
- get chat() {
388
- return this.msg?.chat ?? ("messageReactions" in update ? update.messageReactions.chat : "messageReactionCount" in update ? update.messageReactionCount.chat : "chatMember" in update ? update.chatMember.chat : "myChatMember" in update ? update.myChatMember.chat : "joinRequest" in update ? update.joinRequest.chat : "story" in update ? update.story.chat : undefined);
389
- },
390
- get from() {
391
- const from = "callbackQuery" in update ? update.callbackQuery.from : "inlineQuery" in update ? update.inlineQuery.from : "chatMember" in update ? update.chatMember.from : "myChatMember" in update ? update.myChatMember.from : "messageReactions" in update ? update.messageReactions.user : "preCheckoutQuery" in update ? update.preCheckoutQuery.from : "joinRequest" in update ? update.joinRequest.from : "businessConnection" in update ? update.businessConnection.user : "pollAnswer" in update ? update.pollAnswer.from : this.msg?.from;
392
- return from;
393
- },
394
- toJSON() {
395
- if ("update" in update) {
396
- return { update: toJSON(update.update) };
397
- }
398
- else {
399
- return update;
400
- }
401
- },
402
- reply(text, params) {
403
- const { chatId, messageId, businessConnectionId } = mustGetMsg(this);
404
- const replyTo = getReplyTo(params?.quote, chatId, messageId);
405
- return this.client.sendMessage(chatId, text, { ...params, replyTo, businessConnectionId });
406
- },
407
- replyPoll(question, options, params) {
408
- const { chatId, messageId, businessConnectionId } = mustGetMsg(this);
409
- const replyTo = getReplyTo(params?.quote, chatId, messageId);
410
- return this.client.sendPoll(chatId, question, options, { ...params, replyTo, businessConnectionId });
411
- },
412
- replyPhoto(photo, params) {
413
- const { chatId, messageId, businessConnectionId } = mustGetMsg(this);
414
- const replyTo = getReplyTo(params?.quote, chatId, messageId);
415
- return this.client.sendPhoto(chatId, photo, { ...params, replyTo, businessConnectionId });
416
- },
417
- replyMediaGroup(media, params) {
418
- const { chatId, messageId, businessConnectionId } = mustGetMsg(this);
419
- const replyTo = getReplyTo(params?.quote, chatId, messageId);
420
- return this.client.sendMediaGroup(chatId, media, { ...params, replyTo, businessConnectionId });
421
- },
422
- replyInvoice(title, description, payload, currency, prices, params) {
423
- const { chatId, messageId, businessConnectionId } = mustGetMsg(this);
424
- const replyTo = getReplyTo(params?.quote, chatId, messageId);
425
- return this.client.sendInvoice(chatId, title, description, payload, currency, prices, { ...params, replyTo, businessConnectionId });
426
- },
427
- replyDocument(document, params) {
428
- const { chatId, messageId, businessConnectionId } = mustGetMsg(this);
429
- const replyTo = getReplyTo(params?.quote, chatId, messageId);
430
- return this.client.sendDocument(chatId, document, { ...params, replyTo, businessConnectionId });
431
- },
432
- replySticker(sticker, params) {
433
- const { chatId, messageId, businessConnectionId } = mustGetMsg(this);
434
- const replyTo = getReplyTo(params?.quote, chatId, messageId);
435
- return this.client.sendSticker(chatId, sticker, { ...params, replyTo, businessConnectionId });
436
- },
437
- replyContact(firstName, number, params) {
438
- const { chatId, messageId, businessConnectionId } = mustGetMsg(this);
439
- const replyTo = getReplyTo(params?.quote, chatId, messageId);
440
- return this.client.sendContact(chatId, firstName, number, { ...params, replyTo, businessConnectionId });
441
- },
442
- replyLocation(latitude, longitude, params) {
443
- const { chatId, messageId, businessConnectionId } = mustGetMsg(this);
444
- const replyTo = getReplyTo(params?.quote, chatId, messageId);
445
- return this.client.sendLocation(chatId, latitude, longitude, { ...params, replyTo, businessConnectionId });
446
- },
447
- replyDice(params) {
448
- const { chatId, messageId, businessConnectionId } = mustGetMsg(this);
449
- const replyTo = getReplyTo(params?.quote, chatId, messageId);
450
- return this.client.sendDice(chatId, { ...params, replyTo, businessConnectionId });
451
- },
452
- replyVenue(latitude, longitude, title, address, params) {
453
- const { chatId, messageId, businessConnectionId } = mustGetMsg(this);
454
- const replyTo = getReplyTo(params?.quote, chatId, messageId);
455
- return this.client.sendVenue(chatId, latitude, longitude, title, address, { ...params, replyTo, businessConnectionId });
456
- },
457
- replyVideo(video, params) {
458
- const { chatId, messageId, businessConnectionId } = mustGetMsg(this);
459
- const replyTo = getReplyTo(params?.quote, chatId, messageId);
460
- return this.client.sendVideo(chatId, video, { ...params, replyTo, businessConnectionId });
461
- },
462
- replyAnimation(document, params) {
463
- const { chatId, messageId, businessConnectionId } = mustGetMsg(this);
464
- const replyTo = getReplyTo(params?.quote, chatId, messageId);
465
- return this.client.sendAnimation(chatId, document, { ...params, replyTo, businessConnectionId });
466
- },
467
- replyVoice(document, params) {
468
- const { chatId, messageId, businessConnectionId } = mustGetMsg(this);
469
- const replyTo = getReplyTo(params?.quote, chatId, messageId);
470
- return this.client.sendVoice(chatId, document, { ...params, replyTo, businessConnectionId });
471
- },
472
- replyAudio(document, params) {
473
- const { chatId, messageId, businessConnectionId } = mustGetMsg(this);
474
- const replyTo = getReplyTo(params?.quote, chatId, messageId);
475
- return this.client.sendAudio(chatId, document, { ...params, replyTo, businessConnectionId });
476
- },
477
- replyVideoNote(videoNote, params) {
478
- const { chatId, messageId, businessConnectionId } = mustGetMsg(this);
479
- const replyTo = getReplyTo(params?.quote, chatId, messageId);
480
- return this.client.sendVideoNote(chatId, videoNote, { ...params, replyTo, businessConnectionId });
481
- },
482
- delete() {
483
- const { chatId, messageId } = mustGetMsg(this);
484
- return this.client.deleteMessage(chatId, messageId);
485
- },
486
- forward(to, params) {
487
- const { chatId, messageId } = mustGetMsg(this);
488
- return this.client.forwardMessage(chatId, to, messageId, params);
489
- },
490
- pin(params) {
491
- const { chatId, messageId, businessConnectionId } = mustGetMsg(this);
492
- return this.client.pinMessage(chatId, messageId, { ...params, businessConnectionId });
493
- },
494
- unpin() {
495
- const { chatId, messageId, businessConnectionId } = mustGetMsg(this);
496
- return this.client.unpinMessage(chatId, messageId, { businessConnectionId });
497
- },
498
- banSender(params) {
499
- const { chatId, senderId } = mustGetMsg(this);
500
- if (!senderId) {
501
- unreachable();
502
- }
503
- return this.client.banChatMember(chatId, senderId, params);
504
- },
505
- kickSender() {
506
- const { chatId, senderId } = mustGetMsg(this);
507
- if (!senderId) {
508
- unreachable();
509
- }
510
- return this.client.kickChatMember(chatId, senderId);
511
- },
512
- setSenderRights(params) {
513
- const { chatId, senderId } = mustGetMsg(this);
514
- if (!senderId) {
515
- unreachable();
516
- }
517
- return this.client.setChatMemberRights(chatId, senderId, params);
518
- },
519
- getChatAdministrators() {
520
- const chatId = mustGetChatId(this);
521
- return this.client.getChatAdministrators(chatId);
522
- },
523
- react(reactions, params) {
524
- const { chatId, messageId } = mustGetMsg(this);
525
- return this.client.setReactions(chatId, messageId, reactions, params);
526
- },
527
- answerCallbackQuery(params) {
528
- if (!("callbackQuery" in update)) {
529
- unreachable();
530
- }
531
- return this.client.answerCallbackQuery(update.callbackQuery.id, params);
532
- },
533
- answerInlineQuery(results, params) {
534
- if (!("inlineQuery" in update)) {
535
- unreachable();
536
- }
537
- return this.client.answerInlineQuery(update.inlineQuery.id, results, params);
538
- },
539
- sendChatAction(chatAction, params) {
540
- const chatId = mustGetChatId(this);
541
- return this.client.sendChatAction(chatId, chatAction, params);
542
- },
543
- editInlineMessageText(text, params) {
544
- const inlineMessageId = mustGetInlineMsgId();
545
- return this.client.editInlineMessageText(inlineMessageId, text, params);
546
- },
547
- editInlineMessageMedia(media, params) {
548
- const inlineMessageId = mustGetInlineMsgId();
549
- return this.client.editInlineMessageMedia(inlineMessageId, media, params);
550
- },
551
- editInlineMessageCaption(params) {
552
- const inlineMessageId = mustGetInlineMsgId();
553
- return this.client.editInlineMessageCaption(inlineMessageId, params);
554
- },
555
- editInlineMessageLiveLocation(latitude, longitude, params) {
556
- const inlineMessageId = mustGetInlineMsgId();
557
- return this.client.editInlineMessageLiveLocation(inlineMessageId, latitude, longitude, params);
558
- },
559
- editInlineMessageReplyMarkup(params) {
560
- const inlineMessageId = mustGetInlineMsgId();
561
- return this.client.editInlineMessageReplyMarkup(inlineMessageId, params);
562
- },
563
- editMessageText(messageId, text, params) {
564
- const chatId = mustGetChatId(this);
565
- return this.client.editMessageText(chatId, messageId, text, params);
566
- },
567
- editMessageCaption(messageId, params) {
568
- const chatId = mustGetChatId(this);
569
- return this.client.editMessageCaption(chatId, messageId, params);
570
- },
571
- editMessageMedia(messageId, media, params) {
572
- const chatId = mustGetChatId(this);
573
- return this.client.editMessageMedia(chatId, messageId, media, params);
574
- },
575
- editMessageLiveLocation(messageId, latitude, longitude, params) {
576
- const chatId = mustGetChatId(this);
577
- return this.client.editMessageLiveLocation(chatId, messageId, latitude, longitude, params);
578
- },
579
- editMessageReplyMarkup(messageId, params) {
580
- const chatId = mustGetChatId(this);
581
- return this.client.editMessageReplyMarkup(chatId, messageId, params);
582
- },
583
- getMessage(messageId) {
584
- const chatId = mustGetChatId(this);
585
- return this.client.getMessage(chatId, messageId);
586
- },
587
- getMessages(messageIds) {
588
- const chatId = mustGetChatId(this);
589
- return this.client.getMessages(chatId, messageIds);
590
- },
591
- forwardMessage(to, messageId, params) {
592
- const chatId = mustGetChatId(this);
593
- return this.client.forwardMessage(chatId, to, messageId, params);
594
- },
595
- forwardMessages(to, messageIds, params) {
596
- const chatId = mustGetChatId(this);
597
- return this.client.forwardMessages(chatId, to, messageIds, params);
598
- },
599
- deleteMessage(messageId, params) {
600
- const chatId = mustGetChatId(this);
601
- return this.client.deleteMessage(chatId, messageId, params);
602
- },
603
- deleteMessages(messageIds, params) {
604
- const chatId = mustGetChatId(this);
605
- return this.client.deleteMessages(chatId, messageIds, params);
606
- },
607
- pinMessage(messageId, params) {
608
- const chatId = mustGetChatId(this);
609
- return this.client.pinMessage(chatId, messageId, params);
610
- },
611
- unpinMessage(messageId) {
612
- const chatId = mustGetChatId(this);
613
- return this.client.unpinMessage(chatId, messageId);
614
- },
615
- unpinMessages() {
616
- const chatId = mustGetChatId(this);
617
- return this.client.unpinMessages(chatId);
618
- },
619
- setAvailableReactions(availableReactions) {
620
- const chatId = mustGetChatId(this);
621
- return this.client.setAvailableReactions(chatId, availableReactions);
622
- },
623
- addReaction(messageId, reaction, params) {
624
- const chatId = mustGetChatId(this);
625
- return this.client.addReaction(chatId, messageId, reaction, params);
626
- },
627
- removeReaction(messageId, reaction) {
628
- const chatId = mustGetChatId(this);
629
- return this.client.removeReaction(chatId, messageId, reaction);
630
- },
631
- setReactions(messageId, reactions, params) {
632
- const chatId = mustGetChatId(this);
633
- return this.client.setReactions(chatId, messageId, reactions, params);
634
- },
635
- read() {
636
- const { chatId, messageId } = mustGetMsg(this);
637
- return this.client.readMessages(chatId, messageId);
638
- },
639
- setChatPhoto(photo, params) {
640
- const chatId = mustGetChatId(this);
641
- return this.client.setChatPhoto(chatId, photo, params);
642
- },
643
- deleteChatPhoto() {
644
- const chatId = mustGetChatId(this);
645
- return this.client.deleteChatPhoto(chatId);
646
- },
647
- banChatMember(memberId, params) {
648
- const chatId = mustGetChatId(this);
649
- return this.client.banChatMember(chatId, memberId, params);
650
- },
651
- unbanChatMember(memberId) {
652
- const chatId = mustGetChatId(this);
653
- return this.client.unbanChatMember(chatId, memberId);
654
- },
655
- kickChatMember(memberId) {
656
- const chatId = mustGetChatId(this);
657
- return this.client.kickChatMember(chatId, memberId);
658
- },
659
- setChatMemberRights(memberId, params) {
660
- const chatId = mustGetChatId(this);
661
- return this.client.setChatMemberRights(chatId, memberId, params);
662
- },
663
- promoteChatMember(userId, params) {
664
- const chatId = mustGetChatId(this);
665
- return this.client.promoteChatMember(chatId, userId, params);
666
- },
667
- deleteChatMemberMessages(userId) {
668
- const chatId = mustGetChatId(this);
669
- return this.client.deleteChatMemberMessages(chatId, userId);
670
- },
671
- searchMessages(params) {
672
- const chatId = mustGetChatId(this);
673
- params ??= {};
674
- params.chatId = chatId;
675
- return this.client.searchMessages(params);
676
- },
677
- setBoostsRequiredToCircumventRestrictions(boosts) {
678
- const chatId = mustGetChatId(this);
679
- return this.client.setBoostsRequiredToCircumventRestrictions(chatId, boosts);
680
- },
681
- createInviteLink(params) {
682
- const chatId = mustGetChatId(this);
683
- return this.client.createInviteLink(chatId, params);
684
- },
685
- getCreatedInviteLinks(params) {
686
- const chatId = mustGetChatId(this);
687
- return this.client.getCreatedInviteLinks(chatId, params);
688
- },
689
- leaveChat() {
690
- const chatId = mustGetChatId(this);
691
- return this.client.leaveChat(chatId);
692
- },
693
- blockUser() {
694
- return this.client.blockUser(mustGetUserId(this));
695
- },
696
- unblockUser() {
697
- return this.client.unblockUser(mustGetUserId(this));
698
- },
699
- getChatMember(userId) {
700
- const chatId = mustGetChatId(this);
701
- return this.client.getChatMember(chatId, userId);
702
- },
703
- getChatMembers(params) {
704
- const chatId = mustGetChatId(this);
705
- return this.client.getChatMembers(chatId, params);
706
- },
707
- setChatStickerSet(setName) {
708
- const chatId = mustGetChatId(this);
709
- return this.client.setChatStickerSet(chatId, setName);
710
- },
711
- deleteChatStickerSet() {
712
- const chatId = mustGetChatId(this);
713
- return this.client.deleteChatStickerSet(chatId);
714
- },
715
- getBusinessConnection() {
716
- const { businessConnectionId } = mustGetMsg(this);
717
- if (!businessConnectionId) {
718
- unreachable();
719
- }
720
- return this.client.getBusinessConnection(businessConnectionId);
721
- },
722
- answerPreCheckoutQuery(ok, params) {
723
- if (!("preCheckoutQuery" in update)) {
724
- unreachable();
725
- }
726
- return this.client.answerPreCheckoutQuery(update.preCheckoutQuery.id, ok, params);
727
- },
728
- approveJoinRequest() {
729
- const { chatId, userId } = mustGetMsg(this);
730
- if (!userId) {
731
- unreachable();
732
- }
733
- return this.client.approveJoinRequest(chatId, userId);
734
- },
735
- declineJoinRequest() {
736
- const { chatId, userId } = mustGetMsg(this);
737
- if (!userId) {
738
- unreachable();
739
- }
740
- return this.client.declineJoinRequest(chatId, userId);
741
- },
742
- };
743
- return cleanObject(context);
744
- };
745
313
  #propagateConnectionState(connectionState) {
746
314
  this.#queueHandleCtxUpdate({ connectionState });
747
315
  this.#lastPropagatedConnectionState = connectionState;
@@ -905,172 +473,127 @@ export class Client extends Composer {
905
473
  }, (err) => {
906
474
  this.#LstorageWriteLoop.error(err);
907
475
  });
908
- /**
909
- * Signs in using the provided parameters if not already signed in.
910
- * If no parameters are provided, the credentials will be prompted in runtime.
911
- *
912
- * Notes:
913
- * 1. Requires the `apiId` and `apiHash` paramters to be passed when constructing the client.
914
- * 3. Reconnects the client to the appropriate DC in case of MIGRATE_X errors.
915
- */
916
- async signIn(params) {
476
+ async #checkAuthorization() {
477
+ if (this.#lastGetMe) {
478
+ return this.#lastGetMe;
479
+ }
917
480
  try {
918
- await this.#updateManager.fetchState("signIn");
919
- await this.#getMe();
481
+ await this.#updateManager.fetchState("#checkAuthorization");
482
+ const me = await this.#getMe();
920
483
  await this.#propagateAuthorizationState(true);
921
- drop(this.#updateManager.recoverUpdateGap("signIn"));
922
- this.#LsignIn.debug("already signed in");
923
- return;
484
+ drop(this.#updateManager.recoverUpdateGap("#checkAuthorization"));
485
+ return me;
924
486
  }
925
487
  catch (err) {
926
488
  if (!(err instanceof AuthKeyUnregistered) && !(err instanceof SessionRevoked)) {
927
489
  throw err;
928
490
  }
929
491
  }
930
- if (!this.#apiId) {
931
- throw new InputError("apiId not set");
492
+ }
493
+ /**
494
+ * Send a user verification code.
495
+ *
496
+ * @param phoneNumber The phone number to send the code to.
497
+ * @method ac
498
+ */
499
+ async sendCode(phoneNumber) {
500
+ const me = await this.#checkAuthorization();
501
+ if (me) {
502
+ return;
932
503
  }
933
- if (!this.#apiHash) {
934
- throw new InputError("apiHash not set");
504
+ try {
505
+ await this.#accountManager.sendCode(phoneNumber, this.#apiId, this.#apiHash);
935
506
  }
936
- if (typeof params === "undefined") {
937
- const loginType = mustPromptOneOf("Do you want to sign in as a bot [b] or as a user [u]?", ["b", "u"]);
938
- if (loginType === "b") {
939
- params = { botToken: mustPrompt("Bot token:") };
507
+ catch (err) {
508
+ if (err instanceof Migrate) {
509
+ await this[handleMigrationError](err);
510
+ await this.#accountManager.sendCode(phoneNumber, this.#apiId, this.#apiHash);
940
511
  }
941
512
  else {
942
- params = { phone: () => mustPrompt("Phone number:"), code: () => mustPrompt("Verification code:"), password: () => mustPrompt("Password:") };
513
+ throw err;
943
514
  }
944
515
  }
945
- this.#LsignIn.debug("authorizing with", typeof params === "string" ? "bot token" : Api.is("auth.exportedAuthorization", params) ? "exported authorization" : "AuthorizeUserParams");
946
- if (params && "botToken" in params) {
947
- while (true) {
948
- try {
949
- const auth = await this.invoke({ _: "auth.importBotAuthorization", api_id: this.#apiId, api_hash: this.#apiHash, bot_auth_token: params.botToken, flags: 0 });
516
+ }
517
+ /**
518
+ * Check if a code entered by the user was the same as the verification code.
519
+ *
520
+ * @param code A code entered by the user.
521
+ * @method ac
522
+ */
523
+ async checkCode(code) {
524
+ const result = await this.#accountManager.checkCode(code);
525
+ if (result.type === "signed_in") {
526
+ await this.storage.auth.update((v) => {
527
+ v.userId = result.userId;
528
+ v.isBot = false;
529
+ });
530
+ this.#LsignIn.debug("signed in as user");
531
+ await this.#propagateAuthorizationState(true);
532
+ await this.#updateManager.fetchState("checkCode");
533
+ }
534
+ return result;
535
+ }
536
+ /**
537
+ * Get the user account password's hint.
538
+ *
539
+ * @method ac
540
+ */
541
+ async getPasswordHint() {
542
+ return await this.#accountManager.getPasswordHint();
543
+ }
544
+ /**
545
+ * Check whether a password entered by the user is the same as the account's one.
546
+ *
547
+ * @param password The password to check
548
+ * @returns The result of the check.
549
+ */
550
+ async checkPassword(password) {
551
+ const result = await this.#accountManager.checkPassword(password);
552
+ if (result.type === "signed_in") {
553
+ await this.storage.auth.update((v) => {
554
+ v.userId = result.userId;
555
+ v.isBot = false;
556
+ });
557
+ await this.storage.commit(true);
558
+ this.#LsignIn.debug("signed in as user");
559
+ await this.#propagateAuthorizationState(true);
560
+ await this.#updateManager.fetchState("checkPassword");
561
+ }
562
+ return result;
563
+ }
564
+ /**
565
+ * Check whether a bot token is valid.
566
+ *
567
+ * @param password The password to check
568
+ * @returns The result of the check.
569
+ */
570
+ async checkBotToken(botToken) {
571
+ const me = await this.#checkAuthorization();
572
+ if (me) {
573
+ return {
574
+ type: "signed_in",
575
+ userId: me.id,
576
+ };
577
+ }
578
+ while (true) {
579
+ try {
580
+ const result = await this.#accountManager.checkBotToken(botToken, this.#apiId, this.#apiHash);
581
+ if (result.type === "signed_in") {
950
582
  await this.storage.auth.update((v) => {
951
- v.userId = Number(Api.as("auth.authorization", auth).user.id);
583
+ v.userId = result.userId;
952
584
  v.isBot = true;
953
585
  });
954
586
  await this.storage.commit(true);
955
- break;
956
- }
957
- catch (err) {
958
- if (err instanceof Migrate) {
959
- await this[handleMigrationError](err);
960
- continue;
961
- }
962
- else {
963
- throw err;
964
- }
965
- }
966
- }
967
- this.#LsignIn.debug("authorized as bot");
968
- await this.#propagateAuthorizationState(true);
969
- await this.#updateManager.fetchState("authorize");
970
- return;
971
- }
972
- auth: while (true) {
973
- try {
974
- let phone;
975
- let sentCode;
976
- while (true) {
977
- try {
978
- phone = typeof params.phone === "string" ? params.phone : await params.phone();
979
- const sendCode = () => this.invoke({
980
- _: "auth.sendCode",
981
- phone_number: phone,
982
- api_id: this.#apiId,
983
- api_hash: this.#apiHash,
984
- settings: { _: "codeSettings" },
985
- }).then((v) => Api.as("auth.sentCode", v));
986
- try {
987
- sentCode = await sendCode();
988
- }
989
- catch (err) {
990
- if (err instanceof Migrate) {
991
- await this[handleMigrationError](err);
992
- sentCode = await sendCode();
993
- }
994
- else {
995
- throw err;
996
- }
997
- }
998
- break;
999
- }
1000
- catch (err) {
1001
- if (err instanceof PhoneNumberInvalid) {
1002
- continue;
1003
- }
1004
- else {
1005
- throw err;
1006
- }
1007
- }
1008
- }
1009
- this.#LsignIn.debug("verification code sent");
1010
- let err;
1011
- code: while (true) {
1012
- const code = typeof params.code === "string" ? params.code : await params.code();
1013
- try {
1014
- const auth = await this.invoke({
1015
- _: "auth.signIn",
1016
- phone_number: phone,
1017
- phone_code: code,
1018
- phone_code_hash: sentCode.phone_code_hash,
1019
- });
1020
- await this.storage.auth.update((v) => {
1021
- v.userId = Number(Api.as("auth.authorization", auth).user.id);
1022
- v.isBot = false;
1023
- });
1024
- this.#LsignIn.debug("signed in as user");
1025
- await this.#propagateAuthorizationState(true);
1026
- await this.#updateManager.fetchState("signIn");
1027
- return;
1028
- }
1029
- catch (err_) {
1030
- if (err_ instanceof PhoneCodeInvalid) {
1031
- continue code;
1032
- }
1033
- else {
1034
- err = err_;
1035
- break code;
1036
- }
1037
- }
1038
- }
1039
- if (!(err instanceof SessionPasswordNeeded)) {
1040
- throw err;
1041
- }
1042
- password: while (true) {
1043
- const ap = await this.invoke({ _: "account.getPassword" });
1044
- if (!(Api.is("passwordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow", ap.current_algo))) {
1045
- throw new Error(`Handling ${ap.current_algo?._} not implemented`);
1046
- }
1047
- try {
1048
- const password = typeof params.password === "string" ? params.password : await params.password(ap.hint ?? null);
1049
- const input = await checkPassword(password, ap);
1050
- const auth = await this.invoke({ _: "auth.checkPassword", password: input });
1051
- await this.storage.auth.update((v) => {
1052
- v.userId = Number(Api.as("auth.authorization", auth).user.id);
1053
- v.isBot = false;
1054
- });
1055
- await this.storage.commit(true);
1056
- this.#LsignIn.debug("signed in as user");
1057
- await this.#propagateAuthorizationState(true);
1058
- await this.#updateManager.fetchState("signIn");
1059
- return;
1060
- }
1061
- catch (err) {
1062
- if (err instanceof PasswordHashInvalid) {
1063
- continue password;
1064
- }
1065
- else {
1066
- throw err;
1067
- }
1068
- }
587
+ this.#LsignIn.debug("signed in as bot");
588
+ await this.#propagateAuthorizationState(true);
589
+ await this.#updateManager.fetchState("checkBotToken");
1069
590
  }
591
+ return result;
1070
592
  }
1071
593
  catch (err) {
1072
- if (err === restartAuth) {
1073
- continue auth;
594
+ if (err instanceof Migrate) {
595
+ await this[handleMigrationError](err);
596
+ continue;
1074
597
  }
1075
598
  else {
1076
599
  throw err;
@@ -1078,6 +601,17 @@ export class Client extends Composer {
1078
601
  }
1079
602
  }
1080
603
  }
604
+ /**
605
+ * Signs in using the provided parameters if not already signed in.
606
+ * If no parameters are provided, the credentials will be prompted in runtime.
607
+ *
608
+ * Notes:
609
+ * 1. Requires the `apiId` and `apiHash` paramters to be passed when constructing the client.
610
+ * 3. Reconnects the client to the appropriate DC in case of MIGRATE_X errors.
611
+ */
612
+ async signIn(params) {
613
+ await signIn(this, this.#LsignIn, params);
614
+ }
1081
615
  async signOut() {
1082
616
  try {
1083
617
  await Promise.all([
@@ -1469,7 +1003,7 @@ export class Client extends Composer {
1469
1003
  return;
1470
1004
  }
1471
1005
  try {
1472
- await this.middleware()(await this.#constructContext(update), resolve);
1006
+ await this.handleUpdate(this, update);
1473
1007
  }
1474
1008
  catch (err) {
1475
1009
  this.#L.error("Failed to handle update:", err);
@@ -1585,11 +1119,22 @@ export class Client extends Composer {
1585
1119
  return this.#lastGetMe;
1586
1120
  }
1587
1121
  else {
1588
- const user = await this.getMe();
1122
+ const user = await this.#getMeInner();
1589
1123
  this.#lastGetMe = user;
1590
1124
  return user;
1591
1125
  }
1592
1126
  }
1127
+ async #getMeInner() {
1128
+ let chatP = (await this[getPeer]({ _: "peerUser", user_id: BigInt(await this.#getSelfId()) }))?.[0] ?? null;
1129
+ if (chatP === null) {
1130
+ const users = await this.invoke({ _: "users.getUsers", id: [{ _: "inputUserSelf" }] });
1131
+ chatP = constructChatP(Api.as("user", users[0]));
1132
+ await this.storage.setIsPremium(chatP.isPremium);
1133
+ }
1134
+ const user = constructUser2(chatP);
1135
+ this.#lastGetMe = user;
1136
+ return user;
1137
+ }
1593
1138
  #previouslyConnected = false;
1594
1139
  #lastConnectionState = false;
1595
1140
  #onConnectionStateChange(connected) {
@@ -1614,15 +1159,16 @@ export class Client extends Composer {
1614
1159
  * @returns Information on the currently authorized user.
1615
1160
  */
1616
1161
  async getMe() {
1617
- let chatP = (await this[getPeer]({ _: "peerUser", user_id: BigInt(await this.#getSelfId()) }))?.[0] ?? null;
1618
- if (chatP === null) {
1619
- const users = await this.invoke({ _: "users.getUsers", id: [{ _: "inputUserSelf" }] });
1620
- chatP = constructChatP(Api.as("user", users[0]));
1621
- await this.storage.setIsPremium(chatP.isPremium);
1162
+ if (this.#lastGetMe === null) {
1163
+ const me = await this.#checkAuthorization();
1164
+ if (!me) {
1165
+ throw new InputError("Not signed in.");
1166
+ }
1167
+ else {
1168
+ return me;
1169
+ }
1622
1170
  }
1623
- const user = constructUser2(chatP);
1624
- this.#lastGetMe = user;
1625
- return user;
1171
+ return await this.#getMeInner();
1626
1172
  }
1627
1173
  /**
1628
1174
  * Show a username in the current account, a bot account, a supergroup, or a channel's profile. User-only.
@@ -2376,7 +1922,7 @@ export class Client extends Composer {
2376
1922
  * @method fs
2377
1923
  * @param fileId The identifier of a file.
2378
1924
  * @example ```ts
2379
- * const chunk = await client.download(fileId, { chunkSize: 256 * 1024 });
1925
+ * const chunk = await client.downloadChunk(fileId, { chunkSize: 256 * 1024 });
2380
1926
  * ```
2381
1927
  * @returns The downloaded chunk.
2382
1928
  * @cache file