@mtkruto/node 0.0.990 → 0.0.992

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 (254) hide show
  1. package/esm/1_utilities.d.ts +12 -0
  2. package/esm/1_utilities.js +12 -0
  3. package/esm/2_connection.d.ts +2 -0
  4. package/esm/2_connection.js +2 -0
  5. package/esm/2_tl.d.ts +10 -0
  6. package/esm/2_tl.js +10 -0
  7. package/esm/3_storage.d.ts +6 -0
  8. package/esm/3_storage.js +6 -0
  9. package/esm/3_transport.d.ts +4 -0
  10. package/esm/3_transport.js +4 -0
  11. package/esm/3_types.d.ts +35 -0
  12. package/esm/3_types.js +35 -0
  13. package/esm/{constants.d.ts → 4_constants.d.ts} +2 -2
  14. package/esm/{constants.js → 4_constants.js} +1 -1
  15. package/esm/5_client.d.ts +2 -0
  16. package/esm/5_client.js +2 -0
  17. package/esm/client/0_html.d.ts +1 -1
  18. package/esm/client/0_html.js +1 -1
  19. package/esm/client/0_message.d.ts +1 -2
  20. package/esm/client/0_message.js +7 -14
  21. package/esm/client/0_password.d.ts +2 -2
  22. package/esm/client/0_password.js +2 -4
  23. package/esm/client/0_utilities.d.ts +3 -5
  24. package/esm/client/0_utilities.js +3 -22
  25. package/esm/client/1_client_abstract.d.ts +2 -2
  26. package/esm/client/1_client_abstract.js +3 -3
  27. package/esm/client/2_client_plain.d.ts +3 -3
  28. package/esm/client/2_client_plain.js +13 -20
  29. package/esm/client/3_client.d.ts +43 -18
  30. package/esm/client/3_client.js +107 -46
  31. package/esm/connection/0_connection.d.ts +1 -1
  32. package/esm/connection/1_connection_web_socket.js +1 -1
  33. package/esm/mod.d.ts +7 -21
  34. package/esm/mod.js +7 -21
  35. package/esm/storage/0_storage.d.ts +4 -5
  36. package/esm/storage/0_storage.js +3 -8
  37. package/esm/storage/0_utilities.js +2 -2
  38. package/esm/storage/1_storage_local_storage.d.ts +1 -1
  39. package/esm/storage/1_storage_memory.d.ts +1 -1
  40. package/esm/storage/1_storage_session_storage.d.ts +1 -1
  41. package/esm/tl/0_tl_raw_reader.js +1 -1
  42. package/esm/tl/0_tl_raw_writer.js +1 -1
  43. package/esm/tl/1_tl_object.js +1 -1
  44. package/esm/tl/3_deserialize.js +1 -1
  45. package/esm/tl/3_utilities.d.ts +3 -0
  46. package/esm/tl/3_utilities.js +20 -0
  47. package/esm/tl/4_tl_writer.js +1 -1
  48. package/esm/transport/0_obfuscation.d.ts +2 -2
  49. package/esm/transport/0_obfuscation.js +1 -2
  50. package/esm/transport/0_transport.d.ts +1 -2
  51. package/esm/transport/1_transport_abridged.d.ts +1 -1
  52. package/esm/transport/1_transport_abridged.js +1 -1
  53. package/esm/transport/1_transport_intermediate.d.ts +1 -1
  54. package/esm/transport/1_transport_intermediate.js +1 -1
  55. package/esm/transport/2_transport_provider.d.ts +1 -1
  56. package/esm/transport/2_transport_provider.js +1 -1
  57. package/esm/types/!0_file_id.js +6 -9
  58. package/esm/types/!0_misc.d.ts +7 -0
  59. package/esm/types/!0_misc.js +1 -0
  60. package/esm/types/0_audio.d.ts +3 -3
  61. package/esm/types/0_audio.js +1 -1
  62. package/esm/types/0_chat_administrator_rights.d.ts +1 -1
  63. package/esm/types/0_chat_administrator_rights.js +1 -1
  64. package/esm/types/0_chat_photo.d.ts +3 -3
  65. package/esm/types/0_chat_photo.js +1 -1
  66. package/esm/types/0_color.d.ts +2 -0
  67. package/esm/types/0_color.js +16 -0
  68. package/esm/types/0_contact.d.ts +2 -2
  69. package/esm/types/0_dice.d.ts +1 -1
  70. package/esm/types/0_force_reply.d.ts +1 -1
  71. package/esm/types/0_force_reply.js +1 -1
  72. package/esm/types/0_location.d.ts +1 -1
  73. package/esm/types/0_location.js +1 -2
  74. package/esm/types/0_mask_position.d.ts +1 -1
  75. package/esm/types/0_message_entity.d.ts +5 -5
  76. package/esm/types/0_message_entity.js +1 -1
  77. package/esm/types/0_poll_option.d.ts +1 -1
  78. package/esm/types/0_reply_keyboard_remove.d.ts +1 -1
  79. package/esm/types/0_reply_keyboard_remove.js +2 -2
  80. package/esm/types/0_thumbnail.d.ts +2 -2
  81. package/esm/types/0_thumbnail.js +1 -1
  82. package/esm/types/0_venue.d.ts +1 -1
  83. package/esm/types/0_venue.js +1 -2
  84. package/esm/types/0_voice.d.ts +3 -3
  85. package/esm/types/0_web_app_info.d.ts +2 -2
  86. package/esm/types/1_animation.d.ts +3 -3
  87. package/esm/types/1_animation.js +1 -1
  88. package/esm/types/1_chat.d.ts +5 -4
  89. package/esm/types/1_chat.js +8 -9
  90. package/esm/types/1_document.d.ts +4 -4
  91. package/esm/types/1_document.js +1 -1
  92. package/esm/types/1_inline_keyboard_button.d.ts +4 -4
  93. package/esm/types/1_inline_keyboard_button.js +2 -2
  94. package/esm/types/1_keyboard_button.d.ts +8 -8
  95. package/esm/types/1_keyboard_button.js +2 -2
  96. package/esm/types/1_photo.d.ts +2 -2
  97. package/esm/types/1_photo.js +1 -1
  98. package/esm/types/1_poll.d.ts +8 -8
  99. package/esm/types/1_poll.js +1 -1
  100. package/esm/types/1_sticker.d.ts +3 -3
  101. package/esm/types/1_sticker.js +1 -2
  102. package/esm/types/1_user.d.ts +4 -3
  103. package/esm/types/1_user.js +4 -4
  104. package/esm/types/1_video.d.ts +3 -3
  105. package/esm/types/1_video.js +1 -1
  106. package/esm/types/1_video_note.d.ts +2 -2
  107. package/esm/types/1_video_note.js +1 -1
  108. package/esm/types/2_game.d.ts +3 -3
  109. package/esm/types/2_game.js +2 -3
  110. package/esm/types/2_inline_keyboard_markup.d.ts +2 -2
  111. package/esm/types/2_inline_keyboard_markup.js +1 -1
  112. package/esm/types/2_inline_query.d.ts +19 -0
  113. package/esm/types/2_inline_query.js +41 -0
  114. package/esm/types/2_reply_keyboard_markup.d.ts +2 -2
  115. package/esm/types/2_reply_keyboard_markup.js +1 -1
  116. package/esm/types/3_message.d.ts +23 -23
  117. package/esm/types/3_message.js +19 -18
  118. package/esm/types/4_callback_query.d.ts +14 -0
  119. package/esm/types/4_callback_query.js +24 -0
  120. package/esm/utilities/0_base64.js +1 -1
  121. package/esm/utilities/0_crypto.js +1 -1
  122. package/esm/utilities/0_queue.js +1 -1
  123. package/esm/utilities/1_auth.js +1 -1
  124. package/package.json +1 -1
  125. package/script/1_utilities.d.ts +12 -0
  126. package/script/1_utilities.js +28 -0
  127. package/script/2_connection.d.ts +2 -0
  128. package/script/2_connection.js +18 -0
  129. package/script/2_tl.d.ts +10 -0
  130. package/script/2_tl.js +40 -0
  131. package/script/3_storage.d.ts +6 -0
  132. package/script/3_storage.js +22 -0
  133. package/script/3_transport.d.ts +4 -0
  134. package/script/3_transport.js +20 -0
  135. package/script/3_types.d.ts +35 -0
  136. package/script/3_types.js +51 -0
  137. package/script/{constants.d.ts → 4_constants.d.ts} +2 -2
  138. package/script/{constants.js → 4_constants.js} +1 -1
  139. package/script/5_client.d.ts +2 -0
  140. package/script/5_client.js +18 -0
  141. package/script/client/0_html.d.ts +1 -1
  142. package/script/client/0_html.js +2 -2
  143. package/script/client/0_message.d.ts +1 -2
  144. package/script/client/0_message.js +32 -39
  145. package/script/client/0_password.d.ts +2 -2
  146. package/script/client/0_password.js +20 -22
  147. package/script/client/0_utilities.d.ts +3 -5
  148. package/script/client/0_utilities.js +24 -68
  149. package/script/client/1_client_abstract.d.ts +2 -2
  150. package/script/client/1_client_abstract.js +6 -6
  151. package/script/client/2_client_plain.d.ts +3 -3
  152. package/script/client/2_client_plain.js +45 -52
  153. package/script/client/3_client.d.ts +43 -18
  154. package/script/client/3_client.js +342 -305
  155. package/script/connection/0_connection.d.ts +1 -1
  156. package/script/connection/1_connection_web_socket.js +5 -5
  157. package/script/mod.d.ts +7 -21
  158. package/script/mod.js +17 -45
  159. package/script/storage/0_storage.d.ts +4 -5
  160. package/script/storage/0_storage.js +11 -39
  161. package/script/storage/0_utilities.js +6 -6
  162. package/script/storage/1_storage_local_storage.d.ts +1 -1
  163. package/script/storage/1_storage_memory.d.ts +1 -1
  164. package/script/storage/1_storage_session_storage.d.ts +1 -1
  165. package/script/tl/0_tl_raw_reader.js +6 -6
  166. package/script/tl/0_tl_raw_writer.js +7 -7
  167. package/script/tl/1_tl_object.js +3 -3
  168. package/script/tl/3_deserialize.js +2 -2
  169. package/script/tl/3_utilities.d.ts +3 -0
  170. package/script/tl/3_utilities.js +48 -0
  171. package/script/tl/4_tl_writer.js +2 -2
  172. package/script/transport/0_obfuscation.d.ts +2 -2
  173. package/script/transport/0_obfuscation.js +5 -6
  174. package/script/transport/0_transport.d.ts +1 -2
  175. package/script/transport/1_transport_abridged.d.ts +1 -1
  176. package/script/transport/1_transport_abridged.js +3 -3
  177. package/script/transport/1_transport_intermediate.d.ts +1 -1
  178. package/script/transport/1_transport_intermediate.js +3 -3
  179. package/script/transport/2_transport_provider.d.ts +1 -1
  180. package/script/transport/2_transport_provider.js +2 -2
  181. package/script/types/!0_file_id.js +24 -27
  182. package/script/types/!0_misc.d.ts +7 -0
  183. package/script/types/!0_misc.js +2 -0
  184. package/script/types/0_audio.d.ts +3 -3
  185. package/script/types/0_audio.js +2 -25
  186. package/script/types/0_chat_administrator_rights.d.ts +1 -1
  187. package/script/types/0_chat_administrator_rights.js +2 -25
  188. package/script/types/0_chat_photo.d.ts +3 -3
  189. package/script/types/0_chat_photo.js +2 -25
  190. package/script/types/0_color.d.ts +2 -0
  191. package/script/types/{!0_id_color.js → 0_color.js} +10 -10
  192. package/script/types/0_contact.d.ts +2 -2
  193. package/script/types/0_dice.d.ts +1 -1
  194. package/script/types/0_force_reply.d.ts +1 -1
  195. package/script/types/0_force_reply.js +2 -25
  196. package/script/types/0_location.d.ts +1 -1
  197. package/script/types/0_location.js +5 -29
  198. package/script/types/0_mask_position.d.ts +1 -1
  199. package/script/types/0_message_entity.d.ts +5 -5
  200. package/script/types/0_message_entity.js +39 -62
  201. package/script/types/0_poll_option.d.ts +1 -1
  202. package/script/types/0_reply_keyboard_remove.d.ts +1 -1
  203. package/script/types/0_reply_keyboard_remove.js +4 -27
  204. package/script/types/0_thumbnail.d.ts +2 -2
  205. package/script/types/0_thumbnail.js +2 -25
  206. package/script/types/0_venue.d.ts +1 -1
  207. package/script/types/0_venue.js +2 -26
  208. package/script/types/0_voice.d.ts +3 -3
  209. package/script/types/0_web_app_info.d.ts +2 -2
  210. package/script/types/1_animation.d.ts +3 -3
  211. package/script/types/1_animation.js +2 -25
  212. package/script/types/1_chat.d.ts +5 -4
  213. package/script/types/1_chat.js +20 -44
  214. package/script/types/1_document.d.ts +4 -4
  215. package/script/types/1_document.js +2 -25
  216. package/script/types/1_inline_keyboard_button.d.ts +4 -4
  217. package/script/types/1_inline_keyboard_button.js +19 -42
  218. package/script/types/1_keyboard_button.d.ts +8 -8
  219. package/script/types/1_keyboard_button.js +24 -47
  220. package/script/types/1_photo.d.ts +2 -2
  221. package/script/types/1_photo.js +4 -27
  222. package/script/types/1_poll.d.ts +8 -8
  223. package/script/types/1_poll.js +2 -2
  224. package/script/types/1_sticker.d.ts +3 -3
  225. package/script/types/1_sticker.js +6 -30
  226. package/script/types/1_user.d.ts +4 -3
  227. package/script/types/1_user.js +6 -29
  228. package/script/types/1_video.d.ts +3 -3
  229. package/script/types/1_video.js +2 -25
  230. package/script/types/1_video_note.d.ts +2 -2
  231. package/script/types/1_video_note.js +2 -25
  232. package/script/types/2_game.d.ts +3 -3
  233. package/script/types/2_game.js +6 -30
  234. package/script/types/2_inline_keyboard_markup.d.ts +2 -2
  235. package/script/types/2_inline_keyboard_markup.js +3 -26
  236. package/script/types/2_inline_query.d.ts +19 -0
  237. package/script/types/2_inline_query.js +45 -0
  238. package/script/types/2_reply_keyboard_markup.d.ts +2 -2
  239. package/script/types/2_reply_keyboard_markup.js +3 -26
  240. package/script/types/3_message.d.ts +23 -23
  241. package/script/types/3_message.js +97 -119
  242. package/script/types/4_callback_query.d.ts +14 -0
  243. package/script/types/4_callback_query.js +28 -0
  244. package/script/utilities/0_base64.js +3 -3
  245. package/script/utilities/0_crypto.js +3 -3
  246. package/script/utilities/0_queue.js +2 -2
  247. package/script/utilities/1_auth.js +5 -5
  248. package/esm/types/!0_id_color.d.ts +0 -1
  249. package/esm/types/!0_id_color.js +0 -16
  250. package/script/types/!0_id_color.d.ts +0 -1
  251. /package/esm/{deps.d.ts → 0_deps.d.ts} +0 -0
  252. /package/esm/{deps.js → 0_deps.js} +0 -0
  253. /package/script/{deps.d.ts → 0_deps.d.ts} +0 -0
  254. /package/script/{deps.js → 0_deps.js} +0 -0
@@ -1,28 +1,11 @@
1
- import { debug, gunzip, Mutex } from "../deps.js";
2
- import { ACK_THRESHOLD, APP_VERSION, CHANNEL_DIFFERENCE_LIMIT_BOT, CHANNEL_DIFFERENCE_LIMIT_USER, DEVICE_MODEL, LANG_CODE, LANG_PACK, LAYER, MAX_CHANNEL_ID, MAX_CHAT_ID, STICKER_SET_NAME_TTL, SYSTEM_LANG_CODE, SYSTEM_VERSION, USERNAME_TTL, ZERO_CHANNEL_ID } from "../constants.js";
3
- import { drop, mustPrompt, mustPromptOneOf } from "../utilities/1_misc.js";
4
- import { bigIntFromBuffer, getRandomBigInt, getRandomId } from "../utilities/0_bigint.js";
5
- import { getChannelChatId, hasChannelPts, hasPts, peerToChatId } from "./0_utilities.js";
6
- import { UNREACHABLE } from "../utilities/0_control.js";
7
- import { Queue } from "../utilities/0_queue.js";
8
- import { sha1 } from "../utilities/0_hash.js";
9
- import { TLError } from "../tl/0_tl_raw_reader.js";
10
- import { as } from "../tl/1_tl_object.js";
11
- import * as types from "../tl/2_types.js";
12
- import { TLReader } from "../tl/3_tl_reader.js";
13
- import * as functions from "../tl/3_functions.js";
14
- import { RPCResult } from "../tl/5_rpc_result.js";
15
- import { Message as Message_ } from "../tl/6_message.js"; // MTProto API message
16
- import { MessageContainer } from "../tl/7_message_container.js";
17
- import { FileID, FileType, ThumbnailSource } from "../types/!0_file_id.js";
18
- import { replyKeyboardRemoveToTlObject } from "../types/0_reply_keyboard_remove.js";
19
- import { messageEntityToTlObject } from "../types/0_message_entity.js";
20
- import { forceReplyToTlObject } from "../types/0_force_reply.js";
21
- import { constructUser } from "../types/1_user.js";
22
- import { inlineKeyboardMarkupToTlObject } from "../types/2_inline_keyboard_markup.js";
23
- import { replyKeyboardMarkupToTlObject } from "../types/2_reply_keyboard_markup.js";
24
- import { constructMessage } from "../types/3_message.js"; // high-level wrapper for Telegram API's message
25
- import { StorageMemory } from "../storage/1_storage_memory.js";
1
+ import { debug, gunzip, Mutex } from "../0_deps.js";
2
+ import { bigIntFromBuffer, drop, getRandomBigInt, getRandomId, mustPrompt, mustPromptOneOf, Queue, sha1, UNREACHABLE } from "../1_utilities.js";
3
+ import { as, functions, getChannelChatId, Message as Message_, // MTProto API message
4
+ MessageContainer, peerToChatId, RPCResult, TLError, TLReader, types, } from "../2_tl.js";
5
+ import { StorageMemory } from "../3_storage.js";
6
+ import { constructCallbackQuery, constructInlineQuery, constructMessage, constructUser, FileID, FileType, forceReplyToTlObject, inlineKeyboardMarkupToTlObject, messageEntityToTlObject, replyKeyboardMarkupToTlObject, replyKeyboardRemoveToTlObject, ThumbnailSource, } from "../3_types.js";
7
+ import { ACK_THRESHOLD, APP_VERSION, CHANNEL_DIFFERENCE_LIMIT_BOT, CHANNEL_DIFFERENCE_LIMIT_USER, DEVICE_MODEL, LANG_CODE, LANG_PACK, LAYER, MAX_CHANNEL_ID, MAX_CHAT_ID, STICKER_SET_NAME_TTL, SYSTEM_LANG_CODE, SYSTEM_VERSION, USERNAME_TTL, ZERO_CHANNEL_ID } from "../4_constants.js";
8
+ import { isChannelPtsUpdate, isPtsUpdate } from "./0_utilities.js";
26
9
  import { decryptMessage, encryptMessage, getMessageId } from "./0_message.js";
27
10
  import { checkPassword } from "./0_password.js";
28
11
  import { parseHtml } from "./0_html.js";
@@ -36,6 +19,7 @@ const dRecv = debug("Client/receiveLoop");
36
19
  export const getEntity = Symbol();
37
20
  export const getStickerSetName = Symbol();
38
21
  export const handleMigrationError = Symbol();
22
+ export const getMessageWithReply = Symbol();
39
23
  export const restartAuth = Symbol();
40
24
  export var ParseMode;
41
25
  (function (ParseMode) {
@@ -204,6 +188,12 @@ export class Client extends ClientAbstract {
204
188
  writable: true,
205
189
  value: false
206
190
  });
191
+ Object.defineProperty(this, "lastPropagatedAuthorizationState", {
192
+ enumerable: true,
193
+ configurable: true,
194
+ writable: true,
195
+ value: null
196
+ });
207
197
  Object.defineProperty(this, "autoStarted", {
208
198
  enumerable: true,
209
199
  configurable: true,
@@ -278,11 +268,11 @@ export class Client extends ClientAbstract {
278
268
  * Before establishing the connection, the session is saved.
279
269
  */
280
270
  async connect() {
271
+ if (this.connected) {
272
+ return;
273
+ }
281
274
  const release = await this.connectMutex.acquire();
282
275
  try {
283
- if (this.connected) {
284
- return;
285
- }
286
276
  if (!this.storageInited) {
287
277
  await this.storage.init();
288
278
  this.storageInited = true;
@@ -362,6 +352,12 @@ export class Client extends ClientAbstract {
362
352
  d("connection inited");
363
353
  }
364
354
  }
355
+ async propagateAuthorizationState(authorized) {
356
+ if (this.lastPropagatedAuthorizationState != authorized) {
357
+ await this.handler({ authorizationState: { authorized } }, resolve);
358
+ this.lastPropagatedAuthorizationState = authorized;
359
+ }
360
+ }
365
361
  /**
366
362
  * Calls [initConnection](1) and authorizes the client with one of the following:
367
363
  *
@@ -399,6 +395,7 @@ export class Client extends ClientAbstract {
399
395
  await this.initConnection();
400
396
  try {
401
397
  await this.fetchState("authorize");
398
+ await this.propagateAuthorizationState(true);
402
399
  d("already authorized");
403
400
  return;
404
401
  }
@@ -432,6 +429,7 @@ export class Client extends ClientAbstract {
432
429
  }
433
430
  }
434
431
  dAuth("authorized as bot");
432
+ await this.propagateAuthorizationState(true);
435
433
  await this.fetchState("authorize");
436
434
  return;
437
435
  }
@@ -493,6 +491,7 @@ export class Client extends ClientAbstract {
493
491
  }));
494
492
  await this.storage.setAccountType("user");
495
493
  dAuth("authorized as user");
494
+ await this.propagateAuthorizationState(true);
496
495
  await this.fetchState("authorize");
497
496
  return;
498
497
  }
@@ -520,6 +519,7 @@ export class Client extends ClientAbstract {
520
519
  await this.invoke(new functions.AuthCheckPassword({ password: input }));
521
520
  await this.storage.setAccountType("user");
522
521
  dAuth("authorized as user");
522
+ await this.propagateAuthorizationState(true);
523
523
  await this.fetchState("authorize");
524
524
  break;
525
525
  }
@@ -542,6 +542,7 @@ export class Client extends ClientAbstract {
542
542
  try {
543
543
  await this.fetchState("authorize");
544
544
  d("already authorized");
545
+ await this.propagateAuthorizationState(true);
545
546
  return;
546
547
  }
547
548
  catch (err) {
@@ -684,9 +685,18 @@ export class Client extends ClientAbstract {
684
685
  if (noWait) {
685
686
  return;
686
687
  }
687
- const result = await new Promise((resolve, reject) => {
688
- this.promises.set(message.id, { resolve, reject });
689
- });
688
+ let result;
689
+ try {
690
+ result = await new Promise((resolve, reject) => {
691
+ this.promises.set(message.id, { resolve, reject });
692
+ });
693
+ }
694
+ catch (err) {
695
+ if (err instanceof types.RPCError && err.errorMessage == "AUTH_KEY_UNREGISTERED") {
696
+ await this.propagateAuthorizationState(false);
697
+ }
698
+ throw err;
699
+ }
690
700
  if (result instanceof types.BadServerSalt) {
691
701
  return await this.invoke(function_);
692
702
  }
@@ -794,7 +804,7 @@ export class Client extends ClientAbstract {
794
804
  let originalPts = null;
795
805
  const channelPtsMap = new Map();
796
806
  for (const update of updates) {
797
- if (hasPts(update)) {
807
+ if (isPtsUpdate(update)) {
798
808
  if (update.pts == 0) {
799
809
  continue;
800
810
  }
@@ -808,7 +818,7 @@ export class Client extends ClientAbstract {
808
818
  localState.pts = update.pts;
809
819
  }
810
820
  }
811
- else if (hasChannelPts(update)) {
821
+ else if (isChannelPtsUpdate(update)) {
812
822
  if (update.pts == 0) {
813
823
  continue;
814
824
  }
@@ -1002,7 +1012,28 @@ export class Client extends ClientAbstract {
1002
1012
  }
1003
1013
  }
1004
1014
  }
1015
+ async getUserAccessHash(userId) {
1016
+ const users = await this.invoke(new functions.UsersGetUsers({ id: [new types.InputUser({ userId, accessHash: 0n })] }));
1017
+ return users[0][as](types.User).accessHash ?? 0n;
1018
+ }
1019
+ async getChannelAccessHash(channelId) {
1020
+ const channels = await this.invoke(new functions.ChannelsGetChannels({ id: [new types.InputChannel({ channelId, accessHash: 0n })] }));
1021
+ return channels.chats[0][as](types.Channel).accessHash ?? 0n;
1022
+ }
1005
1023
  async getInputPeer(id) {
1024
+ const inputPeer = await this.getInputPeerInner(id);
1025
+ if (inputPeer instanceof types.InputPeerUser || inputPeer instanceof types.InputPeerChannel && inputPeer.accessHash == 0n && await this.storage.getAccountType() == "bot") {
1026
+ if ("channelId" in inputPeer) {
1027
+ inputPeer.accessHash = await this.getChannelAccessHash(inputPeer.channelId);
1028
+ }
1029
+ else {
1030
+ inputPeer.accessHash = await this.getUserAccessHash(inputPeer.userId);
1031
+ await this.storage.setUserAccessHash(inputPeer.userId, inputPeer.accessHash);
1032
+ }
1033
+ }
1034
+ return inputPeer;
1035
+ }
1036
+ async getInputPeerInner(id) {
1006
1037
  if (typeof id === "string") {
1007
1038
  if (!id.startsWith("@")) {
1008
1039
  throw new Error("Expected username to start with @");
@@ -1107,9 +1138,13 @@ export class Client extends ClientAbstract {
1107
1138
  result instanceof types.ChatlistsExportedInvites ||
1108
1139
  result instanceof types.ChatlistsChatlistInviteAlready ||
1109
1140
  result instanceof types.ChatlistsChatlistInvite ||
1110
- result instanceof types.ChatlistsChatlistUpdates) {
1141
+ result instanceof types.ChatlistsChatlistUpdates ||
1142
+ result instanceof types.MessagesChats ||
1143
+ result instanceof types.MessagesChatsSlice) {
1111
1144
  await this.processChats(result.chats);
1112
- await this.processUsers(result.users);
1145
+ if ("users" in result) {
1146
+ await this.processUsers(result.users);
1147
+ }
1113
1148
  }
1114
1149
  if (result instanceof types.MessagesMessages) {
1115
1150
  for (const message of result.messages) {
@@ -1253,13 +1288,7 @@ export class Client extends ClientAbstract {
1253
1288
  const noWebpage = params?.disableWebPagePreview ? true : undefined;
1254
1289
  await this.invoke(new functions.MessagesEditMessage({ id, peer, entities, message, noWebpage, replyMarkup }));
1255
1290
  }
1256
- /**
1257
- * Retrieve multiple messages.
1258
- *
1259
- * @param chatId The identifier of the chat to retrieve the messages from.
1260
- * @param messageIds The identifiers of the messages to retrieve.
1261
- */
1262
- async getMessages(chatId_, messageIds) {
1291
+ async getMessagesInner(chatId_, messageIds) {
1263
1292
  const peer = await this.getInputPeer(chatId_);
1264
1293
  let messages_ = new Array();
1265
1294
  const chatId = peerToChatId(peer);
@@ -1290,10 +1319,25 @@ export class Client extends ClientAbstract {
1290
1319
  }
1291
1320
  const messages = new Array();
1292
1321
  for (const message_ of messages_) {
1293
- messages.push(await constructMessage(message_, this[getEntity].bind(this), null, this[getStickerSetName].bind(this)));
1322
+ const message = await constructMessage(message_, this[getEntity].bind(this), null, this[getStickerSetName].bind(this));
1323
+ const isReplyToMessage = message_ instanceof types.Message && message_.replyTo instanceof types.MessageReplyHeader;
1324
+ messages.push({ message, isReplyToMessage });
1294
1325
  }
1295
1326
  return messages;
1296
1327
  }
1328
+ /**
1329
+ * Retrieve multiple messages.
1330
+ *
1331
+ * @param chatId The identifier of the chat to retrieve the messages from.
1332
+ * @param messageIds The identifiers of the messages to retrieve.
1333
+ */
1334
+ async getMessages(chatId_, messageIds) {
1335
+ return await this.getMessagesInner(chatId_, messageIds).then((v) => v.map((v) => v.message));
1336
+ }
1337
+ async [getMessageWithReply](chatId, messageId) {
1338
+ const messages = await this.getMessagesInner(chatId, [messageId]);
1339
+ return messages[0]?.message ?? null;
1340
+ }
1297
1341
  /**
1298
1342
  * Retrieve a single message.
1299
1343
  *
@@ -1473,6 +1517,12 @@ export class Client extends ClientAbstract {
1473
1517
  await this.handler({ deletedMessages: deletedMessages }, resolve);
1474
1518
  }
1475
1519
  }
1520
+ if (update instanceof types.UpdateBotCallbackQuery || update instanceof types.UpdateInlineBotCallbackQuery) {
1521
+ await this.handler({ callbackQuery: await constructCallbackQuery(update, this[getEntity].bind(this), this[getMessageWithReply].bind(this)) }, resolve);
1522
+ }
1523
+ else if (update instanceof types.UpdateBotInlineQuery) {
1524
+ await this.handler({ inlineQuery: await constructInlineQuery(update, this[getEntity].bind(this)) }, resolve);
1525
+ }
1476
1526
  }
1477
1527
  use(middleware) {
1478
1528
  const handler = this.handler;
@@ -1486,9 +1536,7 @@ export class Client extends ClientAbstract {
1486
1536
  });
1487
1537
  };
1488
1538
  }
1489
- on(
1490
- // deno-lint-ignore no-explicit-any
1491
- filter, handler) {
1539
+ on(filter, handler) {
1492
1540
  const type = typeof filter === "string" ? filter : filter[0];
1493
1541
  const keys = Array.isArray(filter) ? filter.slice(1) : [];
1494
1542
  this.use((update, next) => {
@@ -1511,5 +1559,18 @@ export class Client extends ClientAbstract {
1511
1559
  }
1512
1560
  });
1513
1561
  }
1562
+ /**
1563
+ * Answer a callback query. Bot-only.
1564
+ *
1565
+ * @param id ID of the callback query to answer.
1566
+ */
1567
+ async answerCallbackQuery(id, params) {
1568
+ await this.invoke(new functions.MessagesSetBotCallbackAnswer({
1569
+ queryId: BigInt(id),
1570
+ cacheTime: params?.cacheTime ?? 0,
1571
+ message: params?.text,
1572
+ alert: params?.alert ? true : undefined,
1573
+ }));
1574
+ }
1514
1575
  }
1515
1576
  const resolve = () => Promise.resolve();
@@ -1,4 +1,4 @@
1
- import { MaybePromise } from "../utilities/0_types.js";
1
+ import { MaybePromise } from "../1_utilities.js";
2
2
  declare abstract class Foundation {
3
3
  abstract get connected(): boolean;
4
4
  stateChangeHandler?: (connected: boolean) => void;
@@ -1,5 +1,5 @@
1
1
  import * as dntShim from "../_dnt.shims.js";
2
- import { debug, Mutex } from "../deps.js";
2
+ import { debug, Mutex } from "../0_deps.js";
3
3
  import { ConnectionUnframed } from "./0_connection.js";
4
4
  const d = debug("ConnectionWebSocket");
5
5
  export class ConnectionWebSocket extends ConnectionUnframed {
package/esm/mod.d.ts CHANGED
@@ -1,26 +1,12 @@
1
- import { getRandomId } from "./utilities/0_bigint.js";
1
+ import { getRandomId } from "./1_utilities.js";
2
2
  import { checkPassword } from "./client/0_password.js";
3
3
  export declare const utils: {
4
4
  checkPassword: typeof checkPassword;
5
5
  getRandomId: typeof getRandomId;
6
6
  };
7
- export { as, serialize } from "./tl/1_tl_object.js";
8
- export * as types from "./tl/2_types.js";
9
- export * as functions from "./tl/3_functions.js";
10
- export * from "./tl/5_rpc_result.js";
11
- export * from "./tl/6_message.js";
12
- export * from "./tl/7_message_container.js";
13
- export * from "./client/2_client_plain.js";
14
- export * from "./client/3_client.js";
15
- export * from "./storage/0_storage.js";
16
- export * from "./storage/1_storage_memory.js";
17
- export * from "./storage/1_storage_local_storage.js";
18
- export * from "./storage/1_storage_session_storage.js";
19
- export * from "./storage/1_storage_indexed_db.js";
20
- export * from "./transport/1_transport_abridged.js";
21
- export * from "./transport/1_transport_intermediate.js";
22
- export * from "./transport/0_transport.js";
23
- export * from "./transport/2_transport_provider.js";
24
- export * from "./connection/0_connection.js";
25
- export * from "./connection/1_connection_web_socket.js";
26
- export { APP_VERSION, DEVICE_MODEL, INITIAL_DC, LANG_CODE, LANG_PACK, LAYER, SYSTEM_LANG_CODE, SYSTEM_VERSION } from "./constants.js";
7
+ export * from "./2_tl.js";
8
+ export * from "./2_connection.js";
9
+ export * from "./3_storage.js";
10
+ export * from "./3_transport.js";
11
+ export { APP_VERSION, DEVICE_MODEL, INITIAL_DC, LANG_CODE, LANG_PACK, LAYER, SYSTEM_LANG_CODE, SYSTEM_VERSION } from "./4_constants.js";
12
+ export * from "./5_client.js";
package/esm/mod.js CHANGED
@@ -1,23 +1,9 @@
1
- import { getRandomId } from "./utilities/0_bigint.js";
1
+ import { getRandomId } from "./1_utilities.js";
2
2
  import { checkPassword } from "./client/0_password.js";
3
3
  export const utils = { checkPassword, getRandomId };
4
- export { as, serialize } from "./tl/1_tl_object.js";
5
- export * as types from "./tl/2_types.js";
6
- export * as functions from "./tl/3_functions.js";
7
- export * from "./tl/5_rpc_result.js";
8
- export * from "./tl/6_message.js";
9
- export * from "./tl/7_message_container.js";
10
- export * from "./client/2_client_plain.js";
11
- export * from "./client/3_client.js";
12
- export * from "./storage/0_storage.js";
13
- export * from "./storage/1_storage_memory.js";
14
- export * from "./storage/1_storage_local_storage.js";
15
- export * from "./storage/1_storage_session_storage.js";
16
- export * from "./storage/1_storage_indexed_db.js";
17
- export * from "./transport/1_transport_abridged.js";
18
- export * from "./transport/1_transport_intermediate.js";
19
- export * from "./transport/0_transport.js";
20
- export * from "./transport/2_transport_provider.js";
21
- export * from "./connection/0_connection.js";
22
- export * from "./connection/1_connection_web_socket.js";
23
- export { APP_VERSION, DEVICE_MODEL, INITIAL_DC, LANG_CODE, LANG_PACK, LAYER, SYSTEM_LANG_CODE, SYSTEM_VERSION } from "./constants.js";
4
+ export * from "./2_tl.js";
5
+ export * from "./2_connection.js";
6
+ export * from "./3_storage.js";
7
+ export * from "./3_transport.js";
8
+ export { APP_VERSION, DEVICE_MODEL, INITIAL_DC, LANG_CODE, LANG_PACK, LAYER, SYSTEM_LANG_CODE, SYSTEM_VERSION } from "./4_constants.js";
9
+ export * from "./5_client.js";
@@ -1,7 +1,6 @@
1
- import { MaybePromise } from "../utilities/0_types.js";
2
- import { DC } from "../transport/2_transport_provider.js";
3
- import { TLObject } from "../tl/1_tl_object.js";
4
- import * as types from "../tl/2_types.js";
1
+ import { MaybePromise } from "../1_utilities.js";
2
+ import { TLObject, types } from "../2_tl.js";
3
+ import { DC } from "../3_transport.js";
5
4
  export type StorageKeyPart = string | number | bigint | Uint8Array;
6
5
  export declare abstract class Storage {
7
6
  private _authKeyId;
@@ -21,7 +20,7 @@ export declare abstract class Storage {
21
20
  updateUsernames(type: "user" | "channel", id: bigint, usernames: string[]): Promise<void>;
22
21
  getUsername(username: string): Promise<["channel" | "user", bigint, Date] | null>;
23
22
  setTlObject(key: readonly StorageKeyPart[], value: TLObject | null): Promise<void>;
24
- getTLObject(key: readonly StorageKeyPart[]): Promise<import("../tl/3_tl_reader.js").ReadObject | null>;
23
+ getTLObject(key: readonly StorageKeyPart[]): Promise<import("../2_tl.js").ReadObject | null>;
25
24
  setState(state: types.UpdatesState): Promise<void>;
26
25
  getState(): Promise<types.UpdatesState | null>;
27
26
  setMessage(chatId: number, messageId: number, message: types.TypeMessage | null): Promise<void>;
@@ -1,11 +1,6 @@
1
- import { UNREACHABLE } from "../utilities/0_control.js";
2
- import { sha1 } from "../utilities/0_hash.js";
3
- import { bigIntFromBuffer } from "../utilities/0_bigint.js";
4
- import { serialize } from "../tl/1_tl_object.js";
5
- import * as types from "../tl/2_types.js";
6
- import { TLReader } from "../tl/3_tl_reader.js";
7
- import { rleDecode, rleEncode } from "../utilities/0_rle.js";
8
- import { ZERO_CHANNEL_ID } from "../constants.js";
1
+ import { bigIntFromBuffer, rleDecode, rleEncode, sha1, UNREACHABLE } from "../1_utilities.js";
2
+ import { serialize, TLReader, types } from "../2_tl.js";
3
+ import { ZERO_CHANNEL_ID } from "../4_constants.js";
9
4
  const KPARTS__DC = ["dc"];
10
5
  const KPARTS__AUTH_KEY = ["authKey"];
11
6
  const KPARTS__CHANNEL_ACCESS_HASH = (v) => ["channelAccessHash", v];
@@ -1,5 +1,5 @@
1
- import { base64Decode, base64Encode } from "../deps.js";
2
- import { UNREACHABLE } from "../utilities/0_control.js";
1
+ import { base64Decode, base64Encode } from "../0_deps.js";
2
+ import { UNREACHABLE } from "../1_utilities.js";
3
3
  export var ValueType;
4
4
  (function (ValueType) {
5
5
  ValueType[ValueType["Boolean"] = 0] = "Boolean";
@@ -1,4 +1,4 @@
1
- import { MaybePromise } from "../utilities/0_types.js";
1
+ import { MaybePromise } from "../1_utilities.js";
2
2
  import { Storage, StorageKeyPart } from "./0_storage.js";
3
3
  export declare class StorageLocalStorage extends Storage implements Storage {
4
4
  private readonly prefix;
@@ -1,4 +1,4 @@
1
- import { MaybePromise } from "../utilities/0_types.js";
1
+ import { MaybePromise } from "../1_utilities.js";
2
2
  import { Storage, StorageKeyPart } from "./0_storage.js";
3
3
  export declare class StorageMemory extends Storage implements Storage {
4
4
  protected map: Map<string, unknown>;
@@ -1,4 +1,4 @@
1
- import { MaybePromise } from "../utilities/0_types.js";
1
+ import { MaybePromise } from "../1_utilities.js";
2
2
  import { Storage, StorageKeyPart } from "./0_storage.js";
3
3
  export declare class StorageSessionStorage extends Storage implements Storage {
4
4
  private readonly prefix;
@@ -1,4 +1,4 @@
1
- import { bigIntFromBuffer } from "../utilities/0_bigint.js";
1
+ import { bigIntFromBuffer } from "../1_utilities.js";
2
2
  export class TLError extends Error {
3
3
  }
4
4
  export class TLRawReader {
@@ -1,4 +1,4 @@
1
- import { bufferFromBigInt, concat } from "../utilities/0_buffer.js";
1
+ import { bufferFromBigInt, concat } from "../1_utilities.js";
2
2
  export class TLRawWriter {
3
3
  constructor() {
4
4
  Object.defineProperty(this, "_buffer", {
@@ -1,4 +1,4 @@
1
- import { assertEquals, assertFalse } from "../deps.js";
1
+ import { assertEquals, assertFalse } from "../0_deps.js";
2
2
  import { TLRawWriter } from "./0_tl_raw_writer.js";
3
3
  export const flags = Symbol("flags");
4
4
  export const id = Symbol("id");
@@ -1,4 +1,4 @@
1
- import { assertEquals } from "../deps.js";
1
+ import { assertEquals } from "../0_deps.js";
2
2
  import { analyzeOptionalParam, flags, isOptionalParam, isTLObjectConstructor, paramDesc } from "./1_tl_object.js";
3
3
  import { map } from "./2_types.js";
4
4
  function deserializeSingleParam(reader, type, ntype) {
@@ -0,0 +1,3 @@
1
+ import * as types from "./2_types.js";
2
+ export declare function getChannelChatId(channelId: bigint): number;
3
+ export declare function peerToChatId(peer: types.TypePeer | types.TypeInputPeer): number;
@@ -0,0 +1,20 @@
1
+ import { UNREACHABLE } from "../1_utilities.js";
2
+ import { ZERO_CHANNEL_ID } from "../4_constants.js";
3
+ import * as types from "./2_types.js";
4
+ export function getChannelChatId(channelId) {
5
+ return ZERO_CHANNEL_ID + -Number(channelId);
6
+ }
7
+ export function peerToChatId(peer) {
8
+ if (peer instanceof types.PeerUser || peer instanceof types.InputPeerUser) {
9
+ return Number(peer.userId);
10
+ }
11
+ else if (peer instanceof types.PeerChat || peer instanceof types.InputPeerChat) {
12
+ return -Number(peer.chatId);
13
+ }
14
+ else if (peer instanceof types.PeerChannel || peer instanceof types.InputPeerChannel) {
15
+ return getChannelChatId(peer.channelId);
16
+ }
17
+ else {
18
+ UNREACHABLE();
19
+ }
20
+ }
@@ -1,4 +1,4 @@
1
- import { VECTOR_CONSTRUCTOR } from "../constants.js";
1
+ import { VECTOR_CONSTRUCTOR } from "../4_constants.js";
2
2
  import { TLRawWriter } from "./0_tl_raw_writer.js";
3
3
  import { serialize } from "./1_tl_object.js";
4
4
  export class TLWriter extends TLRawWriter {
@@ -1,5 +1,5 @@
1
- import { CTR } from "../utilities/0_crypto.js";
2
- import { Connection } from "../connection/0_connection.js";
1
+ import { CTR } from "../1_utilities.js";
2
+ import { Connection } from "../2_connection.js";
3
3
  export declare function getObfuscationParameters(protocol: number, connection: Connection): Promise<{
4
4
  encryptionCTR: CTR;
5
5
  decryptionCTR: CTR;
@@ -1,6 +1,5 @@
1
1
  import * as dntShim from "../_dnt.shims.js";
2
- import { bufferFromBigInt, concat } from "../utilities/0_buffer.js";
3
- import { CTR } from "../utilities/0_crypto.js";
2
+ import { bufferFromBigInt, concat, CTR } from "../1_utilities.js";
4
3
  export async function getObfuscationParameters(protocol, connection) {
5
4
  const dc = 0xFCFF;
6
5
  let init;
@@ -1,5 +1,4 @@
1
- import { MaybePromise } from "../utilities/0_types.js";
2
- import { CTR } from "../utilities/0_crypto.js";
1
+ import { CTR, MaybePromise } from "../1_utilities.js";
3
2
  export declare abstract class Transport {
4
3
  protected initialized: boolean;
5
4
  protected obfuscationParameters: {
@@ -1,4 +1,4 @@
1
- import { Connection } from "../connection/0_connection.js";
1
+ import { Connection } from "../2_connection.js";
2
2
  import { Transport } from "./0_transport.js";
3
3
  export declare class TransportAbridged extends Transport implements Transport {
4
4
  private readonly connection;
@@ -1,4 +1,4 @@
1
- import { bufferFromBigInt, concat } from "../utilities/0_buffer.js";
1
+ import { bufferFromBigInt, concat } from "../1_utilities.js";
2
2
  import { getObfuscationParameters } from "./0_obfuscation.js";
3
3
  import { Transport } from "./0_transport.js";
4
4
  export class TransportAbridged extends Transport {
@@ -1,4 +1,4 @@
1
- import { Connection } from "../connection/0_connection.js";
1
+ import { Connection } from "../2_connection.js";
2
2
  import { Transport } from "./0_transport.js";
3
3
  export declare class TransportIntermediate extends Transport implements Transport {
4
4
  private readonly connection;
@@ -1,4 +1,4 @@
1
- import { bufferFromBigInt, concat } from "../utilities/0_buffer.js";
1
+ import { bufferFromBigInt, concat } from "../1_utilities.js";
2
2
  import { getObfuscationParameters } from "./0_obfuscation.js";
3
3
  import { Transport } from "./0_transport.js";
4
4
  export class TransportIntermediate extends Transport {
@@ -1,4 +1,4 @@
1
- import { Connection } from "../connection/0_connection.js";
1
+ import { Connection } from "../2_connection.js";
2
2
  import { Transport } from "./0_transport.js";
3
3
  export type DC = "1" | "2" | "3" | "4" | "5" | "1-test" | "2-test" | "3-test";
4
4
  export interface TransportProviderParams {
@@ -1,4 +1,4 @@
1
- import { ConnectionWebSocket } from "../connection/1_connection_web_socket.js";
1
+ import { ConnectionWebSocket } from "../2_connection.js";
2
2
  import { TransportIntermediate } from "./1_transport_intermediate.js";
3
3
  const dcToNameMap = {
4
4
  "1": "pluto",
@@ -1,9 +1,6 @@
1
1
  // Direct port from Pyrogram
2
- import { base64DecodeUrlSafe, base64EncodeUrlSafe } from "../utilities/0_base64.js";
3
- import { UNREACHABLE } from "../utilities/0_control.js";
4
- import { rleDecode, rleEncode } from "../utilities/0_rle.js";
5
- import { TLRawWriter } from "../tl/0_tl_raw_writer.js";
6
- import { TLRawReader } from "../tl/0_tl_raw_reader.js";
2
+ import { base64DecodeUrlSafe, base64EncodeUrlSafe, rleDecode, rleEncode, UNREACHABLE } from "../1_utilities.js";
3
+ import { TLReader, TLWriter } from "../2_tl.js";
7
4
  export var FileType;
8
5
  (function (FileType) {
9
6
  FileType[FileType["Thumbnail"] = 0] = "Thumbnail";
@@ -96,7 +93,7 @@ class FileID {
96
93
  minor = decoded[decoded.length - 2];
97
94
  buffer = decoded.slice(0, -2);
98
95
  }
99
- const reader = new TLRawReader(buffer);
96
+ const reader = new TLReader(buffer);
100
97
  let fileType = reader.readInt32();
101
98
  const dcId = reader.readInt32();
102
99
  const hasWebLocation = Boolean(fileType & WEB_LOCATION_FLAG);
@@ -159,7 +156,7 @@ class FileID {
159
156
  encode(major, minor) {
160
157
  major ??= this.major;
161
158
  minor ??= this.minor;
162
- const writer = new TLRawWriter();
159
+ const writer = new TLWriter();
163
160
  let fileType = this.fileType;
164
161
  if (this.params.url) {
165
162
  fileType |= WEB_LOCATION_FLAG;
@@ -274,7 +271,7 @@ export class FileUniqueID {
274
271
  });
275
272
  }
276
273
  static decode(fileId) {
277
- const reader = new TLRawReader(rleDecode(base64DecodeUrlSafe(fileId)));
274
+ const reader = new TLReader(rleDecode(base64DecodeUrlSafe(fileId)));
278
275
  const fileUniqueType = reader.readInt32();
279
276
  switch (fileUniqueType) {
280
277
  case FileUniqueType.Web: {
@@ -295,7 +292,7 @@ export class FileUniqueID {
295
292
  }
296
293
  }
297
294
  encode() {
298
- const writer = new TLRawWriter();
295
+ const writer = new TLWriter();
299
296
  writer.writeInt32(this.fileUniqueType);
300
297
  switch (this.fileUniqueType) {
301
298
  case FileUniqueType.Web:
@@ -0,0 +1,7 @@
1
+ import { MaybePromise } from "../1_utilities.js";
2
+ import { types } from "../2_tl.js";
3
+ export interface EntityGetter {
4
+ (peer: types.PeerUser): MaybePromise<types.User | null>;
5
+ (peer: types.PeerChat): MaybePromise<types.Chat | null>;
6
+ (peer: types.PeerChannel): MaybePromise<types.Channel | null>;
7
+ }
@@ -0,0 +1 @@
1
+ export {};