teleproto 1.214.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (222) hide show
  1. package/CryptoFile.d.ts +2 -0
  2. package/CryptoFile.js +37 -0
  3. package/Helpers.d.ts +150 -0
  4. package/Helpers.js +477 -0
  5. package/LICENSE +23 -0
  6. package/Password.d.ts +14 -0
  7. package/Password.js +270 -0
  8. package/README.md +98 -0
  9. package/Utils.d.ts +227 -0
  10. package/Utils.js +1247 -0
  11. package/Version.d.ts +1 -0
  12. package/Version.js +4 -0
  13. package/client/2fa.d.ts +48 -0
  14. package/client/2fa.js +108 -0
  15. package/client/TelegramClient.d.ts +1043 -0
  16. package/client/TelegramClient.js +1181 -0
  17. package/client/account.d.ts +0 -0
  18. package/client/account.js +1 -0
  19. package/client/auth.d.ts +92 -0
  20. package/client/auth.js +388 -0
  21. package/client/bots.d.ts +6 -0
  22. package/client/bots.js +23 -0
  23. package/client/buttons.d.ts +4 -0
  24. package/client/buttons.js +86 -0
  25. package/client/chats.d.ts +45 -0
  26. package/client/chats.js +350 -0
  27. package/client/dialogs.d.ts +52 -0
  28. package/client/dialogs.js +174 -0
  29. package/client/downloads.d.ts +156 -0
  30. package/client/downloads.js +614 -0
  31. package/client/fs.d.ts +1 -0
  32. package/client/fs.js +17 -0
  33. package/client/index.d.ts +15 -0
  34. package/client/index.js +64 -0
  35. package/client/messageParse.d.ts +17 -0
  36. package/client/messageParse.js +191 -0
  37. package/client/messages.d.ts +263 -0
  38. package/client/messages.js +813 -0
  39. package/client/os.d.ts +2 -0
  40. package/client/os.js +37 -0
  41. package/client/path.d.ts +2 -0
  42. package/client/path.js +7 -0
  43. package/client/telegramBaseClient.d.ts +237 -0
  44. package/client/telegramBaseClient.js +368 -0
  45. package/client/updates.d.ts +33 -0
  46. package/client/updates.js +260 -0
  47. package/client/uploads.d.ts +136 -0
  48. package/client/uploads.js +514 -0
  49. package/client/users.d.ts +29 -0
  50. package/client/users.js +490 -0
  51. package/crypto/AuthKey.d.ts +19 -0
  52. package/crypto/AuthKey.js +76 -0
  53. package/crypto/CTR.d.ts +5 -0
  54. package/crypto/CTR.js +16 -0
  55. package/crypto/Factorizator.d.ts +19 -0
  56. package/crypto/Factorizator.js +74 -0
  57. package/crypto/IGE.d.ts +11 -0
  58. package/crypto/IGE.js +115 -0
  59. package/crypto/RSA.d.ts +14 -0
  60. package/crypto/RSA.js +70 -0
  61. package/crypto/converters.d.ts +19 -0
  62. package/crypto/converters.js +52 -0
  63. package/crypto/crypto.d.ts +19 -0
  64. package/crypto/crypto.js +58 -0
  65. package/crypto/words.d.ts +6 -0
  66. package/crypto/words.js +47 -0
  67. package/define.d.ts +77 -0
  68. package/entityCache.d.ts +7 -0
  69. package/entityCache.js +79 -0
  70. package/errors/Common.d.ts +71 -0
  71. package/errors/Common.js +122 -0
  72. package/errors/RPCBaseErrors.d.ts +84 -0
  73. package/errors/RPCBaseErrors.js +134 -0
  74. package/errors/RPCErrorList.d.ts +37 -0
  75. package/errors/RPCErrorList.js +117 -0
  76. package/errors/index.d.ts +11 -0
  77. package/errors/index.js +32 -0
  78. package/events/Album.d.ts +36 -0
  79. package/events/Album.js +104 -0
  80. package/events/CallbackQuery.d.ts +73 -0
  81. package/events/CallbackQuery.js +193 -0
  82. package/events/DeletedMessage.d.ts +45 -0
  83. package/events/DeletedMessage.js +63 -0
  84. package/events/EditedMessage.d.ts +30 -0
  85. package/events/EditedMessage.js +41 -0
  86. package/events/NewMessage.d.ts +89 -0
  87. package/events/NewMessage.js +182 -0
  88. package/events/Raw.d.ts +29 -0
  89. package/events/Raw.js +43 -0
  90. package/events/common.d.ts +66 -0
  91. package/events/common.js +141 -0
  92. package/events/index.d.ts +2 -0
  93. package/events/index.js +8 -0
  94. package/extensions/AsyncQueue.d.ts +10 -0
  95. package/extensions/AsyncQueue.js +32 -0
  96. package/extensions/BinaryReader.d.ts +99 -0
  97. package/extensions/BinaryReader.js +242 -0
  98. package/extensions/BinaryWriter.d.ts +6 -0
  99. package/extensions/BinaryWriter.js +15 -0
  100. package/extensions/Deferred.d.ts +8 -0
  101. package/extensions/Deferred.js +16 -0
  102. package/extensions/Logger.d.ts +54 -0
  103. package/extensions/Logger.js +128 -0
  104. package/extensions/MessagePacker.d.ts +21 -0
  105. package/extensions/MessagePacker.js +154 -0
  106. package/extensions/PendingState.d.ts +12 -0
  107. package/extensions/PendingState.js +29 -0
  108. package/extensions/PromisedNetSockets.d.ts +24 -0
  109. package/extensions/PromisedNetSockets.js +192 -0
  110. package/extensions/PromisedWebSockets.d.ts +18 -0
  111. package/extensions/PromisedWebSockets.js +124 -0
  112. package/extensions/html.d.ts +5 -0
  113. package/extensions/html.js +228 -0
  114. package/extensions/index.d.ts +7 -0
  115. package/extensions/index.js +17 -0
  116. package/extensions/markdown.d.ts +5 -0
  117. package/extensions/markdown.js +76 -0
  118. package/extensions/markdownv2.d.ts +5 -0
  119. package/extensions/markdownv2.js +51 -0
  120. package/extensions/net.d.ts +1 -0
  121. package/extensions/net.js +17 -0
  122. package/extensions/socks.d.ts +1 -0
  123. package/extensions/socks.js +17 -0
  124. package/index.d.ts +14 -0
  125. package/index.js +62 -0
  126. package/inspect.d.ts +1 -0
  127. package/inspect.js +5 -0
  128. package/network/Authenticator.d.ts +12 -0
  129. package/network/Authenticator.js +192 -0
  130. package/network/MTProtoPlainSender.d.ts +19 -0
  131. package/network/MTProtoPlainSender.js +74 -0
  132. package/network/MTProtoSender.d.ts +290 -0
  133. package/network/MTProtoSender.js +873 -0
  134. package/network/MTProtoState.d.ts +102 -0
  135. package/network/MTProtoState.js +267 -0
  136. package/network/RequestState.d.ts +18 -0
  137. package/network/RequestState.js +35 -0
  138. package/network/connection/Connection.d.ts +69 -0
  139. package/network/connection/Connection.js +162 -0
  140. package/network/connection/TCPAbridged.d.ts +19 -0
  141. package/network/connection/TCPAbridged.js +58 -0
  142. package/network/connection/TCPFull.d.ts +16 -0
  143. package/network/connection/TCPFull.js +61 -0
  144. package/network/connection/TCPMTProxy.d.ts +49 -0
  145. package/network/connection/TCPMTProxy.js +121 -0
  146. package/network/connection/TCPObfuscated.d.ts +18 -0
  147. package/network/connection/TCPObfuscated.js +78 -0
  148. package/network/connection/index.d.ts +4 -0
  149. package/network/connection/index.js +11 -0
  150. package/network/index.d.ts +11 -0
  151. package/network/index.js +23 -0
  152. package/package.json +50 -0
  153. package/requestIter.d.ts +24 -0
  154. package/requestIter.js +109 -0
  155. package/sessions/Abstract.d.ts +103 -0
  156. package/sessions/Abstract.js +6 -0
  157. package/sessions/Memory.d.ts +38 -0
  158. package/sessions/Memory.js +272 -0
  159. package/sessions/StoreSession.d.ts +14 -0
  160. package/sessions/StoreSession.js +77 -0
  161. package/sessions/StringSession.d.ts +32 -0
  162. package/sessions/StringSession.js +116 -0
  163. package/sessions/index.d.ts +4 -0
  164. package/sessions/index.js +11 -0
  165. package/sessions/localStorage.d.ts +1 -0
  166. package/sessions/localStorage.js +4 -0
  167. package/tl/AllTLObjects.d.ts +3 -0
  168. package/tl/AllTLObjects.js +17 -0
  169. package/tl/MTProtoRequest.d.ts +18 -0
  170. package/tl/MTProtoRequest.js +38 -0
  171. package/tl/api.d.ts +32488 -0
  172. package/tl/api.js +507 -0
  173. package/tl/apiTl.d.ts +2 -0
  174. package/tl/apiTl.js +2209 -0
  175. package/tl/core/GZIPPacked.d.ts +15 -0
  176. package/tl/core/GZIPPacked.js +51 -0
  177. package/tl/core/MessageContainer.d.ts +12 -0
  178. package/tl/core/MessageContainer.js +42 -0
  179. package/tl/core/RPCResult.d.ts +14 -0
  180. package/tl/core/RPCResult.js +32 -0
  181. package/tl/core/TLMessage.d.ts +10 -0
  182. package/tl/core/TLMessage.js +14 -0
  183. package/tl/core/index.d.ts +6 -0
  184. package/tl/core/index.js +16 -0
  185. package/tl/custom/button.d.ts +24 -0
  186. package/tl/custom/button.js +78 -0
  187. package/tl/custom/chatGetter.d.ts +29 -0
  188. package/tl/custom/chatGetter.js +116 -0
  189. package/tl/custom/dialog.d.ts +30 -0
  190. package/tl/custom/dialog.js +40 -0
  191. package/tl/custom/draft.d.ts +21 -0
  192. package/tl/custom/draft.js +48 -0
  193. package/tl/custom/file.d.ts +21 -0
  194. package/tl/custom/file.js +68 -0
  195. package/tl/custom/forward.d.ts +15 -0
  196. package/tl/custom/forward.js +47 -0
  197. package/tl/custom/index.d.ts +1 -0
  198. package/tl/custom/index.js +5 -0
  199. package/tl/custom/inlineResult.d.ts +32 -0
  200. package/tl/custom/inlineResult.js +87 -0
  201. package/tl/custom/inlineResults.d.ts +20 -0
  202. package/tl/custom/inlineResults.js +26 -0
  203. package/tl/custom/message.d.ts +427 -0
  204. package/tl/custom/message.js +716 -0
  205. package/tl/custom/messageButton.d.ts +54 -0
  206. package/tl/custom/messageButton.js +152 -0
  207. package/tl/custom/senderGetter.d.ts +28 -0
  208. package/tl/custom/senderGetter.js +55 -0
  209. package/tl/generateModule.d.ts +1 -0
  210. package/tl/generateModule.js +17 -0
  211. package/tl/generationHelpers.d.ts +11 -0
  212. package/tl/generationHelpers.js +289 -0
  213. package/tl/index.d.ts +3 -0
  214. package/tl/index.js +10 -0
  215. package/tl/patched/index.d.ts +2 -0
  216. package/tl/patched/index.js +76 -0
  217. package/tl/schemaTl.d.ts +2 -0
  218. package/tl/schemaTl.js +64 -0
  219. package/tl/types-generator/generate.d.ts +1 -0
  220. package/tl/types-generator/generate.js +84 -0
  221. package/tl/types-generator/template.d.ts +6 -0
  222. package/tl/types-generator/template.js +257 -0
package/Utils.js ADDED
@@ -0,0 +1,1247 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getFileInfo = getFileInfo;
7
+ exports.chunks = chunks;
8
+ exports.getInputPeer = getInputPeer;
9
+ exports._photoSizeByteCount = _photoSizeByteCount;
10
+ exports._getEntityPair = _getEntityPair;
11
+ exports.getInnerText = getInnerText;
12
+ exports.getInputChannel = getInputChannel;
13
+ exports.getInputUser = getInputUser;
14
+ exports.getInputMessage = getInputMessage;
15
+ exports.getInputChatPhoto = getInputChatPhoto;
16
+ exports.strippedPhotoToJpg = strippedPhotoToJpg;
17
+ exports.getInputPhoto = getInputPhoto;
18
+ exports.getInputDocument = getInputDocument;
19
+ exports.isAudio = isAudio;
20
+ exports.isImage = isImage;
21
+ exports.getExtension = getExtension;
22
+ exports.getAttributes = getAttributes;
23
+ exports.getInputGeo = getInputGeo;
24
+ exports.getInputMedia = getInputMedia;
25
+ exports.getAppropriatedPartSize = getAppropriatedPartSize;
26
+ exports.getPeer = getPeer;
27
+ exports.sanitizeParseMode = sanitizeParseMode;
28
+ exports.getPeerId = getPeerId;
29
+ exports.resolveId = resolveId;
30
+ exports.getMessageId = getMessageId;
31
+ exports.parsePhone = parsePhone;
32
+ exports.parseID = parseID;
33
+ exports.resolveInviteLink = resolveInviteLink;
34
+ exports.parseUsername = parseUsername;
35
+ exports.rtrim = rtrim;
36
+ exports.getDisplayName = getDisplayName;
37
+ const big_integer_1 = __importDefault(require("big-integer"));
38
+ const mime_1 = __importDefault(require("mime"));
39
+ const html_1 = require("./extensions/html");
40
+ const markdown_1 = require("./extensions/markdown");
41
+ const markdownv2_1 = require("./extensions/markdownv2");
42
+ const Helpers_1 = require("./Helpers");
43
+ const tl_1 = require("./tl");
44
+ function getFileInfo(fileLocation) {
45
+ if (!fileLocation || !fileLocation.SUBCLASS_OF_ID) {
46
+ _raiseCastFail(fileLocation, "InputFileLocation");
47
+ }
48
+ if (fileLocation.SUBCLASS_OF_ID == 354669666) {
49
+ return {
50
+ dcId: undefined,
51
+ location: fileLocation,
52
+ size: undefined,
53
+ };
54
+ }
55
+ let location;
56
+ if (fileLocation instanceof tl_1.Api.Message) {
57
+ location = fileLocation.media;
58
+ }
59
+ if (fileLocation instanceof tl_1.Api.MessageMediaDocument) {
60
+ location = fileLocation.document;
61
+ }
62
+ else if (fileLocation instanceof tl_1.Api.MessageMediaPhoto) {
63
+ location = fileLocation.photo;
64
+ }
65
+ if (location instanceof tl_1.Api.Document) {
66
+ return {
67
+ dcId: location.dcId,
68
+ location: new tl_1.Api.InputDocumentFileLocation({
69
+ id: location.id,
70
+ accessHash: location.accessHash,
71
+ fileReference: location.fileReference,
72
+ thumbSize: "",
73
+ }),
74
+ size: location.size,
75
+ };
76
+ }
77
+ else if (location instanceof tl_1.Api.Photo) {
78
+ return {
79
+ dcId: location.dcId,
80
+ location: new tl_1.Api.InputPhotoFileLocation({
81
+ id: location.id,
82
+ accessHash: location.accessHash,
83
+ fileReference: location.fileReference,
84
+ thumbSize: location.sizes[location.sizes.length - 1].type,
85
+ }),
86
+ size: (0, big_integer_1.default)(_photoSizeByteCount(location.sizes[location.sizes.length - 1]) || 0),
87
+ };
88
+ }
89
+ _raiseCastFail(fileLocation, "InputFileLocation");
90
+ }
91
+ /**
92
+ * Turns the given iterable into chunks of the specified size,
93
+ * which is 100 by default since that's what Telegram uses the most.
94
+ */
95
+ function* chunks(arr, size = 100) {
96
+ for (let i = 0; i < arr.length; i += size) {
97
+ yield arr.slice(i, i + size);
98
+ }
99
+ }
100
+ const USERNAME_RE = new RegExp("@|(?:https?:\\/\\/)?(?:www\\.)?" +
101
+ "(?:telegram\\.(?:me|dog)|t\\.me)\\/(@|joinchat\\/)?", "i");
102
+ const JPEG_HEADER = Buffer.from("ffd8ffe000104a46494600010100000100010000ffdb004300281c1e231e19282321232d2b28303c64413c37373c7b585d4964918099968f808c8aa0b4e6c3a0aadaad8a8cc8ffcbdaeef5ffffff9bc1fffffffaffe6fdfff8ffdb0043012b2d2d3c353c76414176f8a58ca5f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8ffc00011080000000003012200021101031101ffc4001f0000010501010101010100000000000000000102030405060708090a0bffc400b5100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9faffc4001f0100030101010101010101010000000000000102030405060708090a0bffc400b51100020102040403040705040400010277000102031104052131061241510761711322328108144291a1b1c109233352f0156272d10a162434e125f11718191a262728292a35363738393a434445464748494a535455565758595a636465666768696a737475767778797a82838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae2e3e4e5e6e7e8e9eaf2f3f4f5f6f7f8f9faffda000c03010002110311003f00", "hex");
103
+ const JPEG_FOOTER = Buffer.from("ffd9", "hex");
104
+ const TG_JOIN_RE = new RegExp("tg:\\/\\/(join)\\?invite=", "i");
105
+ const VALID_USERNAME_RE = new RegExp("^([a-z]((?!__)[\\w\\d]){2,30}[a-z\\d]|gif|vid|" +
106
+ "pic|bing|wiki|imdb|bold|vote|like|coub)$", "i");
107
+ function _raiseCastFail(entity, target) {
108
+ let toWrite = entity;
109
+ if (typeof entity === "object" && "className" in entity) {
110
+ toWrite = entity.className;
111
+ }
112
+ throw new Error(`Cannot cast ${toWrite} to any kind of ${target}`);
113
+ }
114
+ /**
115
+ Gets the input peer for the given "entity" (user, chat or channel).
116
+
117
+ A ``TypeError`` is raised if the given entity isn't a supported type
118
+ or if ``check_hash is True`` but the entity's ``accessHash is None``
119
+ *or* the entity contains ``min`` information. In this case, the hash
120
+ cannot be used for general purposes, and thus is not returned to avoid
121
+ any issues which can derive from invalid access hashes.
122
+
123
+ Note that ``checkHash`` **is ignored** if an input peer is already
124
+ passed since in that case we assume the user knows what they're doing.
125
+ This is key to getting entities by explicitly passing ``hash = 0``.
126
+
127
+ * @param entity
128
+ * @param allowSelf
129
+ * @param checkHash
130
+ */
131
+ function getInputPeer(entity, allowSelf = true, checkHash = true) {
132
+ if (entity.SUBCLASS_OF_ID === undefined) {
133
+ // e.g. custom.Dialog (can't cyclic import).
134
+ if (allowSelf && "inputEntity" in entity) {
135
+ return entity.inputEntity;
136
+ }
137
+ else if ("entity" in entity) {
138
+ return getInputPeer(entity.entity);
139
+ }
140
+ else {
141
+ _raiseCastFail(entity, "InputPeer");
142
+ }
143
+ }
144
+ if (entity.SUBCLASS_OF_ID === 0xc91c90b6) {
145
+ // crc32(b'InputPeer')
146
+ return entity;
147
+ }
148
+ if (entity instanceof tl_1.Api.User) {
149
+ if (entity.self && allowSelf) {
150
+ return new tl_1.Api.InputPeerSelf();
151
+ }
152
+ else if ((entity.accessHash !== undefined && !entity.min) ||
153
+ !checkHash) {
154
+ return new tl_1.Api.InputPeerUser({
155
+ userId: entity.id,
156
+ accessHash: entity.accessHash || (0, big_integer_1.default)(0),
157
+ });
158
+ }
159
+ else {
160
+ throw new Error("User without accessHash or min cannot be input");
161
+ }
162
+ }
163
+ if (entity instanceof tl_1.Api.Chat ||
164
+ entity instanceof tl_1.Api.ChatEmpty ||
165
+ entity instanceof tl_1.Api.ChatForbidden) {
166
+ return new tl_1.Api.InputPeerChat({ chatId: entity.id });
167
+ }
168
+ if (entity instanceof tl_1.Api.Channel) {
169
+ if ((entity.accessHash !== undefined && !entity.min) || !checkHash) {
170
+ return new tl_1.Api.InputPeerChannel({
171
+ channelId: entity.id,
172
+ accessHash: entity.accessHash || (0, big_integer_1.default)(0),
173
+ });
174
+ }
175
+ else {
176
+ throw new TypeError("Channel without accessHash or min info cannot be input");
177
+ }
178
+ }
179
+ if (entity instanceof tl_1.Api.ChannelForbidden) {
180
+ // "channelForbidden are never min", and since their hash is
181
+ // also not optional, we assume that this truly is the case.
182
+ return new tl_1.Api.InputPeerChannel({
183
+ channelId: entity.id,
184
+ accessHash: entity.accessHash,
185
+ });
186
+ }
187
+ if (entity instanceof tl_1.Api.InputUser) {
188
+ return new tl_1.Api.InputPeerUser({
189
+ userId: entity.userId,
190
+ accessHash: entity.accessHash,
191
+ });
192
+ }
193
+ if (entity instanceof tl_1.Api.InputChannel) {
194
+ return new tl_1.Api.InputPeerChannel({
195
+ channelId: entity.channelId,
196
+ accessHash: entity.accessHash,
197
+ });
198
+ }
199
+ if (entity instanceof tl_1.Api.UserEmpty) {
200
+ return new tl_1.Api.InputPeerEmpty();
201
+ }
202
+ if (entity instanceof tl_1.Api.UserFull) {
203
+ return getInputPeer(entity.id);
204
+ }
205
+ if (entity instanceof tl_1.Api.ChatFull) {
206
+ return new tl_1.Api.InputPeerChat({ chatId: entity.id });
207
+ }
208
+ if (entity instanceof tl_1.Api.PeerChat) {
209
+ return new tl_1.Api.InputPeerChat({
210
+ chatId: entity.chatId,
211
+ });
212
+ }
213
+ _raiseCastFail(entity, "InputPeer");
214
+ }
215
+ function _photoSizeByteCount(size) {
216
+ if (size instanceof tl_1.Api.PhotoSize) {
217
+ return size.size;
218
+ }
219
+ else if (size instanceof tl_1.Api.PhotoStrippedSize) {
220
+ if (size.bytes.length < 3 || size.bytes[0] != 1) {
221
+ return size.bytes.length;
222
+ }
223
+ return size.bytes.length + 622;
224
+ }
225
+ else if (size instanceof tl_1.Api.PhotoCachedSize) {
226
+ return size.bytes.length;
227
+ }
228
+ else if (size instanceof tl_1.Api.PhotoSizeEmpty) {
229
+ return 0;
230
+ }
231
+ else if (size instanceof tl_1.Api.PhotoSizeProgressive) {
232
+ return size.sizes[size.sizes.length - 1];
233
+ }
234
+ else {
235
+ return undefined;
236
+ }
237
+ }
238
+ function _getEntityPair(entityId, entities, cache, getInputPeerFunction = getInputPeer) {
239
+ const entity = entities.get(entityId);
240
+ let inputEntity;
241
+ try {
242
+ inputEntity = cache.get(entityId);
243
+ }
244
+ catch (e) {
245
+ try {
246
+ inputEntity = getInputPeerFunction(inputEntity);
247
+ }
248
+ catch (e) { }
249
+ }
250
+ return [entity, inputEntity];
251
+ }
252
+ function getInnerText(text, entities) {
253
+ const result = [];
254
+ entities.forEach(function (value, key) {
255
+ const start = value.offset;
256
+ const end = value.offset + value.length;
257
+ result.push(text.slice(start, end));
258
+ });
259
+ return result;
260
+ }
261
+ /**
262
+ Similar to :meth:`get_input_peer`, but for :tl:`InputChannel`'s alone.
263
+
264
+ .. important::
265
+
266
+ This method does not validate for invalid general-purpose access
267
+ hashes, unlike `get_input_peer`. Consider using instead:
268
+ ``get_input_channel(get_input_peer(channel))``.
269
+
270
+ * @param entity
271
+ * @returns {InputChannel|*}
272
+ */
273
+ function getInputChannel(entity) {
274
+ if (typeof entity === "string" ||
275
+ typeof entity == "number" ||
276
+ typeof entity == "bigint" ||
277
+ big_integer_1.default.isInstance(entity)) {
278
+ _raiseCastFail(entity, "InputChannel");
279
+ }
280
+ if (entity.SUBCLASS_OF_ID === undefined) {
281
+ _raiseCastFail(entity, "InputChannel");
282
+ }
283
+ if (entity.SUBCLASS_OF_ID === 0x40f202fd) {
284
+ // crc32(b'InputChannel')
285
+ return entity;
286
+ }
287
+ if (entity instanceof tl_1.Api.Channel ||
288
+ entity instanceof tl_1.Api.ChannelForbidden) {
289
+ return new tl_1.Api.InputChannel({
290
+ channelId: entity.id,
291
+ accessHash: entity.accessHash || big_integer_1.default.zero,
292
+ });
293
+ }
294
+ if (entity instanceof tl_1.Api.InputPeerChannel) {
295
+ return new tl_1.Api.InputChannel({
296
+ channelId: entity.channelId,
297
+ accessHash: entity.accessHash,
298
+ });
299
+ }
300
+ _raiseCastFail(entity, "InputChannel");
301
+ }
302
+ /**
303
+ Similar to :meth:`getInputPeer`, but for :tl:`InputUser`'s alone.
304
+
305
+ .. important::
306
+
307
+ This method does not validate for invalid general-purpose access
308
+ hashes, unlike `get_input_peer`. Consider using instead:
309
+ ``get_input_channel(get_input_peer(channel))``.
310
+
311
+ * @param entity
312
+ */
313
+ function getInputUser(entity) {
314
+ if (typeof entity === "string" ||
315
+ typeof entity == "number" ||
316
+ typeof entity == "bigint" ||
317
+ big_integer_1.default.isInstance(entity)) {
318
+ _raiseCastFail(entity, "InputUser");
319
+ }
320
+ if (entity.SUBCLASS_OF_ID === undefined) {
321
+ _raiseCastFail(entity, "InputUser");
322
+ }
323
+ if (entity.SUBCLASS_OF_ID === 0xe669bf46) {
324
+ // crc32(b'InputUser')
325
+ return entity;
326
+ }
327
+ if (entity instanceof tl_1.Api.User) {
328
+ if (entity.self) {
329
+ return new tl_1.Api.InputUserSelf();
330
+ }
331
+ else {
332
+ return new tl_1.Api.InputUser({
333
+ userId: entity.id,
334
+ accessHash: entity.accessHash || big_integer_1.default.zero,
335
+ });
336
+ }
337
+ }
338
+ if (entity instanceof tl_1.Api.InputPeerSelf) {
339
+ return new tl_1.Api.InputUserSelf();
340
+ }
341
+ if (entity instanceof tl_1.Api.UserEmpty ||
342
+ entity instanceof tl_1.Api.InputPeerEmpty) {
343
+ return new tl_1.Api.InputUserEmpty();
344
+ }
345
+ if (entity instanceof tl_1.Api.UserFull) {
346
+ return getInputUser(entity);
347
+ }
348
+ if (entity instanceof tl_1.Api.InputPeerUser) {
349
+ return new tl_1.Api.InputUser({
350
+ userId: entity.userId,
351
+ accessHash: entity.accessHash,
352
+ });
353
+ }
354
+ if (entity instanceof tl_1.Api.InputPeerUserFromMessage) {
355
+ return new tl_1.Api.InputUserFromMessage({
356
+ userId: entity.userId,
357
+ peer: entity.peer,
358
+ msgId: entity.msgId,
359
+ });
360
+ }
361
+ _raiseCastFail(entity, "InputUser");
362
+ }
363
+ /**
364
+ Similar to :meth:`get_input_peer`, but for dialogs
365
+ * @param dialog
366
+ */
367
+ /*CONTEST
368
+ function getInputDialog(dialog) {
369
+ try {
370
+ if (dialog.SUBCLASS_OF_ID === 0xa21c9795) { // crc32(b'InputDialogPeer')
371
+ return dialog
372
+ }
373
+ if (dialog.SUBCLASS_OF_ID === 0xc91c90b6) { // crc32(b'InputPeer')
374
+ return new Api.InputDialogPeer({ peer: dialog })
375
+ }
376
+ } catch (e) {
377
+ _raiseCastFail(dialog, 'InputDialogPeer')
378
+ }
379
+
380
+ try {
381
+ return new Api.InputDialogPeer(getInputPeer(dialog))
382
+ // eslint-disable-next-line no-empty
383
+ } catch (e) {
384
+
385
+ }
386
+ _raiseCastFail(dialog, 'InputDialogPeer')
387
+ }
388
+ */
389
+ /**
390
+ * Similar to :meth:`get_input_peer`, but for input messages.
391
+ */
392
+ function getInputMessage(message) {
393
+ if (typeof message === "number") {
394
+ return new tl_1.Api.InputMessageID({ id: message });
395
+ }
396
+ if (message === undefined || message.SUBCLASS_OF_ID === undefined) {
397
+ _raiseCastFail(message, "InputMessage");
398
+ }
399
+ if (message.SUBCLASS_OF_ID === 0x54b6bcc5) {
400
+ // crc32(b'InputMessage')
401
+ return message;
402
+ }
403
+ else if (message.SUBCLASS_OF_ID === 0x790009e3) {
404
+ // crc32(b'Message'):
405
+ return new tl_1.Api.InputMessageID({ id: message.id });
406
+ }
407
+ _raiseCastFail(message, "InputMessage");
408
+ }
409
+ /**
410
+ * Similar to :meth:`get_input_peer`, but for input messages.
411
+ */
412
+ function getInputChatPhoto(photo) {
413
+ if (photo === undefined || photo.SUBCLASS_OF_ID === undefined) {
414
+ _raiseCastFail(photo, "InputChatPhoto");
415
+ }
416
+ if (photo.SUBCLASS_OF_ID === 0xd4eb2d74) {
417
+ //crc32(b'InputChatPhoto')
418
+ return photo;
419
+ }
420
+ else if (photo.SUBCLASS_OF_ID === 0xe7655f1f) {
421
+ // crc32(b'InputFile'):
422
+ return new tl_1.Api.InputChatUploadedPhoto({
423
+ file: photo,
424
+ });
425
+ }
426
+ photo = getInputPhoto(photo);
427
+ if (photo instanceof tl_1.Api.InputPhoto) {
428
+ return new tl_1.Api.InputChatPhoto({
429
+ id: photo,
430
+ });
431
+ }
432
+ else if (photo instanceof tl_1.Api.InputPhotoEmpty) {
433
+ return new tl_1.Api.InputChatPhotoEmpty();
434
+ }
435
+ _raiseCastFail(photo, "InputChatPhoto");
436
+ }
437
+ /**
438
+ * Adds the JPG header and footer to a stripped image.
439
+ * Ported from https://github.com/telegramdesktop/tdesktop/blob/bec39d89e19670eb436dc794a8f20b657cb87c71/Telegram/SourceFiles/ui/image/image.cpp#L225
440
+
441
+ * @param stripped{Buffer}
442
+ * @returns {Buffer}
443
+ */
444
+ function strippedPhotoToJpg(stripped) {
445
+ // Note: Changes here should update _stripped_real_length
446
+ if (stripped.length < 3 || stripped[0] !== 1) {
447
+ return stripped;
448
+ }
449
+ const header = Buffer.from(JPEG_HEADER);
450
+ header[164] = stripped[1];
451
+ header[166] = stripped[2];
452
+ return Buffer.concat([header, stripped.slice(3), JPEG_FOOTER]);
453
+ }
454
+ /*CONTEST
455
+ function getInputLocation(location) {
456
+ try {
457
+ if (!location.SUBCLASS_OF_ID) {
458
+ throw new Error()
459
+ }
460
+ if (location.SUBCLASS_OF_ID === 0x1523d462) {
461
+ return {
462
+ dcId: null,
463
+ inputLocation: location
464
+ }
465
+ }
466
+ } catch (e) {
467
+ _raiseCastFail(location, 'InputFileLocation')
468
+ }
469
+ if (location instanceof Api.Message) {
470
+ location = location.media
471
+ }
472
+
473
+ if (location instanceof Api.MessageMediaDocument) {
474
+ location = location.document
475
+ } else if (location instanceof Api.MessageMediaPhoto) {
476
+ location = location.photo
477
+ }
478
+
479
+ if (location instanceof Api.Document) {
480
+ return {
481
+ dcId: location.dcId,
482
+ inputLocation: new Api.InputDocumentFileLocation({
483
+ id: location.id,
484
+ accessHash: location.accessHash,
485
+ fileReference: location.fileReference,
486
+ thumbSize: '', // Presumably to download one of its thumbnails
487
+ }),
488
+ }
489
+ } else if (location instanceof Api.Photo) {
490
+ return {
491
+ dcId: location.dcId,
492
+ inputLocation: new Api.InputPhotoFileLocation({
493
+ id: location.id,
494
+ accessHash: location.accessHash,
495
+ fileReference: location.fileReference,
496
+ thumbSize: location.sizes[location.sizes.length - 1].type,
497
+ }),
498
+ }
499
+ }
500
+
501
+ if (location instanceof Api.FileLocationToBeDeprecated) {
502
+ throw new Error('Unavailable location cannot be used as input')
503
+ }
504
+ _raiseCastFail(location, 'InputFileLocation')
505
+ }
506
+ */
507
+ /**
508
+ * Similar to :meth:`get_input_peer`, but for photos
509
+ */
510
+ function getInputPhoto(photo) {
511
+ if (photo.SUBCLASS_OF_ID === undefined) {
512
+ _raiseCastFail(photo, "InputPhoto");
513
+ }
514
+ if (photo.SUBCLASS_OF_ID === 2221106144) {
515
+ return photo;
516
+ }
517
+ if (photo instanceof tl_1.Api.Message) {
518
+ photo = photo.media;
519
+ }
520
+ if (photo instanceof tl_1.Api.photos.Photo ||
521
+ photo instanceof tl_1.Api.MessageMediaPhoto) {
522
+ photo = photo.photo;
523
+ }
524
+ if (photo instanceof tl_1.Api.Photo) {
525
+ return new tl_1.Api.InputPhoto({
526
+ id: photo.id,
527
+ accessHash: photo.accessHash,
528
+ fileReference: photo.fileReference,
529
+ });
530
+ }
531
+ if (photo instanceof tl_1.Api.PhotoEmpty) {
532
+ return new tl_1.Api.InputPhotoEmpty();
533
+ }
534
+ if (photo instanceof tl_1.Api.messages.ChatFull) {
535
+ photo = photo.fullChat;
536
+ }
537
+ if (photo instanceof tl_1.Api.ChannelFull) {
538
+ return getInputPhoto(photo.chatPhoto);
539
+ }
540
+ else {
541
+ if (photo instanceof tl_1.Api.UserFull) {
542
+ return getInputPhoto(photo.profilePhoto);
543
+ }
544
+ else {
545
+ if (photo instanceof tl_1.Api.Channel ||
546
+ photo instanceof tl_1.Api.Chat ||
547
+ photo instanceof tl_1.Api.User) {
548
+ return getInputPhoto(photo.photo);
549
+ }
550
+ }
551
+ }
552
+ if (photo instanceof tl_1.Api.UserEmpty ||
553
+ photo instanceof tl_1.Api.ChatEmpty ||
554
+ photo instanceof tl_1.Api.ChatForbidden ||
555
+ photo instanceof tl_1.Api.ChannelForbidden) {
556
+ return new tl_1.Api.InputPhotoEmpty();
557
+ }
558
+ _raiseCastFail(photo, "InputPhoto");
559
+ }
560
+ /**
561
+ * Similar to :meth:`get_input_peer`, but for documents
562
+ */
563
+ function getInputDocument(document) {
564
+ if (document.SUBCLASS_OF_ID === undefined) {
565
+ _raiseCastFail(document, "InputDocument");
566
+ }
567
+ if (document.SUBCLASS_OF_ID === 0xf33fdb68) {
568
+ return document;
569
+ }
570
+ if (document instanceof tl_1.Api.Document) {
571
+ return new tl_1.Api.InputDocument({
572
+ id: document.id,
573
+ accessHash: document.accessHash,
574
+ fileReference: document.fileReference,
575
+ });
576
+ }
577
+ if (document instanceof tl_1.Api.DocumentEmpty) {
578
+ return new tl_1.Api.InputDocumentEmpty();
579
+ }
580
+ if (document instanceof tl_1.Api.MessageMediaDocument) {
581
+ return getInputDocument(document.document);
582
+ }
583
+ if (document instanceof tl_1.Api.Message) {
584
+ return getInputDocument(document.media);
585
+ }
586
+ _raiseCastFail(document, "InputDocument");
587
+ }
588
+ /**
589
+ * Returns `True` if the file has an audio mime type.
590
+ */
591
+ function isAudio(file) {
592
+ const ext = _getExtension(file);
593
+ if (!ext) {
594
+ const metadata = _getMetadata(file);
595
+ return (metadata.get("mimeType") || "").startsWith("audio/");
596
+ }
597
+ else {
598
+ file = "a" + ext;
599
+ return (mime_1.default.getType(file) || "").startsWith("audio/");
600
+ }
601
+ }
602
+ /**
603
+ * Returns `True` if the file has an image mime type.
604
+ */
605
+ function isImage(file) {
606
+ const ext = _getExtension(file).toLowerCase();
607
+ return (ext.endsWith(".png") || ext.endsWith(".jpg") || ext.endsWith(".jpeg"));
608
+ }
609
+ function getExtension(media) {
610
+ // Photos are always compressed as .jpg by Telegram
611
+ try {
612
+ getInputPhoto(media);
613
+ return ".jpg";
614
+ }
615
+ catch (e) { }
616
+ if (media instanceof tl_1.Api.UserProfilePhoto ||
617
+ media instanceof tl_1.Api.ChatPhoto) {
618
+ return ".jpg";
619
+ }
620
+ if (media instanceof tl_1.Api.MessageMediaDocument) {
621
+ media = media.document;
622
+ }
623
+ if (media instanceof tl_1.Api.Document ||
624
+ media instanceof tl_1.Api.WebDocument ||
625
+ media instanceof tl_1.Api.WebDocumentNoProxy) {
626
+ if (media.mimeType === "application/octet-stream") {
627
+ // Octet stream are just bytes, which have no default extension
628
+ return "";
629
+ }
630
+ else {
631
+ return mime_1.default.getExtension(media.mimeType) || "";
632
+ }
633
+ }
634
+ return "";
635
+ }
636
+ /**
637
+ * Gets the extension for the given file, which can be either a
638
+ * str or an ``open()``'ed file (which has a ``.name`` attribute).
639
+ */
640
+ function _getExtension(file) {
641
+ if (typeof file === "string") {
642
+ return "." + file.split(".").pop();
643
+ }
644
+ else if ("name" in file) {
645
+ return _getExtension(file.name);
646
+ }
647
+ else {
648
+ return getExtension(file);
649
+ }
650
+ }
651
+ function _getMetadata(file) {
652
+ //TODO Return nothing for now until we find a better way
653
+ return new Map();
654
+ }
655
+ function isVideo(file) {
656
+ var _a;
657
+ const ext = _getExtension(file);
658
+ if (!ext) {
659
+ const metadata = _getMetadata(file);
660
+ if (metadata.has("mimeType")) {
661
+ return ((_a = metadata.get("mimeType")) === null || _a === void 0 ? void 0 : _a.startsWith("video/")) || false;
662
+ }
663
+ else {
664
+ return false;
665
+ }
666
+ }
667
+ else {
668
+ file = "a" + ext;
669
+ return (mime_1.default.getType(file) || "").startsWith("video/");
670
+ }
671
+ }
672
+ /**
673
+ Get a list of attributes for the given file and
674
+ the mime type as a tuple ([attribute], mime_type).
675
+ */
676
+ function getAttributes(file, { attributes = null, mimeType = undefined, forceDocument = false, voiceNote = false, videoNote = false, supportsStreaming = false, thumb = null, }) {
677
+ var _a, _b, _c, _d;
678
+ const name = typeof file == "string"
679
+ ? file
680
+ : "name" in file
681
+ ? file.name || "unnamed"
682
+ : "unnamed";
683
+ if (mimeType === undefined) {
684
+ mimeType = mime_1.default.getType(name) || "application/octet-stream";
685
+ }
686
+ const attrObj = new Map();
687
+ attrObj.set(tl_1.Api.DocumentAttributeFilename, new tl_1.Api.DocumentAttributeFilename({
688
+ fileName: name.split(/[\\/]/).pop() || "",
689
+ }));
690
+ if (isAudio(file)) {
691
+ const m = _getMetadata(file);
692
+ attrObj.set(tl_1.Api.DocumentAttributeAudio, new tl_1.Api.DocumentAttributeAudio({
693
+ voice: voiceNote,
694
+ title: m.has("title") ? m.get("title") : undefined,
695
+ performer: m.has("author") ? m.get("author") : undefined,
696
+ duration: Number.parseInt((_a = m.get("duration")) !== null && _a !== void 0 ? _a : "0"),
697
+ }));
698
+ }
699
+ if (!forceDocument && isVideo(file)) {
700
+ let doc;
701
+ if (thumb) {
702
+ const t_m = _getMetadata(thumb);
703
+ const width = Number.parseInt((t_m === null || t_m === void 0 ? void 0 : t_m.get("width")) || "1");
704
+ const height = Number.parseInt((t_m === null || t_m === void 0 ? void 0 : t_m.get("height")) || "1");
705
+ doc = new tl_1.Api.DocumentAttributeVideo({
706
+ duration: 0,
707
+ h: height,
708
+ w: width,
709
+ roundMessage: videoNote,
710
+ supportsStreaming: supportsStreaming,
711
+ });
712
+ }
713
+ else {
714
+ const m = _getMetadata(file);
715
+ doc = new tl_1.Api.DocumentAttributeVideo({
716
+ roundMessage: videoNote,
717
+ w: Number.parseInt((_b = m.get("width")) !== null && _b !== void 0 ? _b : "1"),
718
+ h: Number.parseInt((_c = m.get("height")) !== null && _c !== void 0 ? _c : "1"),
719
+ duration: Number.parseInt((_d = m.get("duration")) !== null && _d !== void 0 ? _d : "0"),
720
+ supportsStreaming: supportsStreaming,
721
+ });
722
+ }
723
+ attrObj.set(tl_1.Api.DocumentAttributeVideo, doc);
724
+ }
725
+ if (videoNote) {
726
+ if (attrObj.has(tl_1.Api.DocumentAttributeAudio)) {
727
+ attrObj.get(tl_1.Api.DocumentAttributeAudio).voice = true;
728
+ }
729
+ else {
730
+ attrObj.set(tl_1.Api.DocumentAttributeAudio, new tl_1.Api.DocumentAttributeAudio({
731
+ duration: 0,
732
+ voice: true,
733
+ }));
734
+ }
735
+ }
736
+ /* Now override the attributes if any. As we have a dict of
737
+ {cls: instance}, we can override any class with the list
738
+ of attributes provided by the user easily.
739
+ */
740
+ if (attributes) {
741
+ for (const a of attributes) {
742
+ attrObj.set(a.constructor, a);
743
+ }
744
+ }
745
+ return {
746
+ attrs: Array.from(attrObj.values()),
747
+ mimeType: mimeType,
748
+ };
749
+ }
750
+ /**
751
+ * Similar to :meth:`get_input_peer`, but for geo points
752
+ */
753
+ function getInputGeo(geo) {
754
+ if (geo === undefined || geo.SUBCLASS_OF_ID === undefined) {
755
+ _raiseCastFail(geo, "InputGeoPoint");
756
+ }
757
+ if (geo.SUBCLASS_OF_ID === 0x430d225) {
758
+ // crc32(b'InputGeoPoint'):
759
+ return geo;
760
+ }
761
+ if (geo instanceof tl_1.Api.GeoPoint) {
762
+ return new tl_1.Api.InputGeoPoint({ lat: geo.lat, long: geo.long });
763
+ }
764
+ if (geo instanceof tl_1.Api.GeoPointEmpty) {
765
+ return new tl_1.Api.InputGeoPointEmpty();
766
+ }
767
+ if (geo instanceof tl_1.Api.MessageMediaGeo) {
768
+ return getInputGeo(geo.geo);
769
+ }
770
+ if (geo instanceof tl_1.Api.Message) {
771
+ return getInputGeo(geo.media);
772
+ }
773
+ _raiseCastFail(geo, "InputGeoPoint");
774
+ }
775
+ /**
776
+ *
777
+ Similar to :meth:`get_input_peer`, but for media.
778
+
779
+ If the media is :tl:`InputFile` and ``is_photo`` is known to be `True`,
780
+ it will be treated as an :tl:`InputMediaUploadedPhoto`. Else, the rest
781
+ of parameters will indicate how to treat it.
782
+ * @param media
783
+ * @param isPhoto - whether it's a photo or not
784
+ * @param attributes
785
+ * @param forceDocument
786
+ * @param voiceNote
787
+ * @param videoNote
788
+ * @param supportsStreaming
789
+ */
790
+ function getInputMedia(media, { isPhoto = false, attributes = undefined, forceDocument = false, voiceNote = false, videoNote = false, supportsStreaming = false, } = {}) {
791
+ if (media.SUBCLASS_OF_ID === undefined) {
792
+ _raiseCastFail(media, "InputMedia");
793
+ }
794
+ if (media.SUBCLASS_OF_ID === 0xfaf846f4) {
795
+ // crc32(b'InputMedia')
796
+ return media;
797
+ }
798
+ else {
799
+ if (media.SUBCLASS_OF_ID === 2221106144) {
800
+ // crc32(b'InputPhoto')
801
+ return new tl_1.Api.InputMediaPhoto({ id: media });
802
+ }
803
+ else {
804
+ if (media.SUBCLASS_OF_ID === 4081048424) {
805
+ // crc32(b'InputDocument')
806
+ return new tl_1.Api.InputMediaDocument({ id: media });
807
+ }
808
+ }
809
+ }
810
+ if (media instanceof tl_1.Api.MessageMediaPhoto) {
811
+ return new tl_1.Api.InputMediaPhoto({
812
+ id: getInputPhoto(media.photo),
813
+ ttlSeconds: media.ttlSeconds,
814
+ });
815
+ }
816
+ if (media instanceof tl_1.Api.Photo ||
817
+ media instanceof tl_1.Api.photos.Photo ||
818
+ media instanceof tl_1.Api.PhotoEmpty) {
819
+ return new tl_1.Api.InputMediaPhoto({ id: getInputPhoto(media) });
820
+ }
821
+ if (media instanceof tl_1.Api.MessageMediaDocument) {
822
+ return new tl_1.Api.InputMediaDocument({
823
+ id: getInputDocument(media.document),
824
+ ttlSeconds: media.ttlSeconds,
825
+ });
826
+ }
827
+ if (media instanceof tl_1.Api.Document || media instanceof tl_1.Api.DocumentEmpty) {
828
+ return new tl_1.Api.InputMediaDocument({ id: getInputDocument(media) });
829
+ }
830
+ if (media instanceof tl_1.Api.InputFile || media instanceof tl_1.Api.InputFileBig) {
831
+ if (isPhoto) {
832
+ return new tl_1.Api.InputMediaUploadedPhoto({ file: media });
833
+ }
834
+ else {
835
+ const { attrs, mimeType } = getAttributes(media, {
836
+ attributes: attributes,
837
+ forceDocument: forceDocument,
838
+ voiceNote: voiceNote,
839
+ videoNote: videoNote,
840
+ supportsStreaming: supportsStreaming,
841
+ });
842
+ return new tl_1.Api.InputMediaUploadedDocument({
843
+ file: media,
844
+ mimeType: mimeType,
845
+ attributes: attrs,
846
+ forceFile: forceDocument,
847
+ });
848
+ }
849
+ }
850
+ if (media instanceof tl_1.Api.MessageMediaGame) {
851
+ return new tl_1.Api.InputMediaGame({
852
+ id: new tl_1.Api.InputGameID({
853
+ id: media.game.id,
854
+ accessHash: media.game.accessHash,
855
+ }),
856
+ });
857
+ }
858
+ if (media instanceof tl_1.Api.MessageMediaContact) {
859
+ return new tl_1.Api.InputMediaContact({
860
+ phoneNumber: media.phoneNumber,
861
+ firstName: media.firstName,
862
+ lastName: media.lastName,
863
+ vcard: "",
864
+ });
865
+ }
866
+ if (media instanceof tl_1.Api.MessageMediaGeo) {
867
+ return new tl_1.Api.InputMediaGeoPoint({ geoPoint: getInputGeo(media.geo) });
868
+ }
869
+ if (media instanceof tl_1.Api.MessageMediaVenue) {
870
+ return new tl_1.Api.InputMediaVenue({
871
+ geoPoint: getInputGeo(media.geo),
872
+ title: media.title,
873
+ address: media.address,
874
+ provider: media.provider,
875
+ venueId: media.venueId,
876
+ venueType: "",
877
+ });
878
+ }
879
+ if (media instanceof tl_1.Api.MessageMediaDice) {
880
+ return new tl_1.Api.InputMediaDice({
881
+ emoticon: media.emoticon,
882
+ });
883
+ }
884
+ if (media instanceof tl_1.Api.MessageMediaEmpty ||
885
+ media instanceof tl_1.Api.MessageMediaUnsupported ||
886
+ media instanceof tl_1.Api.ChatPhotoEmpty ||
887
+ media instanceof tl_1.Api.UserProfilePhotoEmpty ||
888
+ media instanceof tl_1.Api.ChatPhoto ||
889
+ media instanceof tl_1.Api.UserProfilePhoto) {
890
+ return new tl_1.Api.InputMediaEmpty();
891
+ }
892
+ if (media instanceof tl_1.Api.Message) {
893
+ return getInputMedia(media.media, { isPhoto: isPhoto });
894
+ }
895
+ if (media instanceof tl_1.Api.MessageMediaPoll) {
896
+ let correctAnswers;
897
+ if (media.poll.quiz) {
898
+ if (!media.results.results) {
899
+ throw new Error("Cannot cast unanswered quiz to any kind of InputMedia.");
900
+ }
901
+ correctAnswers = [];
902
+ for (const r of media.results.results) {
903
+ if (r.correct) {
904
+ correctAnswers.push(r.option);
905
+ }
906
+ }
907
+ }
908
+ else {
909
+ correctAnswers = undefined;
910
+ }
911
+ return new tl_1.Api.InputMediaPoll({
912
+ poll: media.poll,
913
+ correctAnswers: correctAnswers,
914
+ solution: media.results.solution,
915
+ solutionEntities: media.results.solutionEntities,
916
+ });
917
+ }
918
+ if (media instanceof tl_1.Api.Poll) {
919
+ return new tl_1.Api.InputMediaPoll({
920
+ poll: media,
921
+ });
922
+ }
923
+ _raiseCastFail(media, "InputMedia");
924
+ }
925
+ /**
926
+ * Gets the appropriated part size when uploading or downloading files,
927
+ * given an initial file size.
928
+ * @param fileSize
929
+ * @returns {Number}
930
+ */
931
+ function getAppropriatedPartSize(fileSize) {
932
+ if (fileSize.lesser(104857600)) {
933
+ // 100MB
934
+ return 128;
935
+ }
936
+ if (fileSize.lesser(786432000)) {
937
+ // 750MB
938
+ return 256;
939
+ }
940
+ return 512;
941
+ }
942
+ function getPeer(peer) {
943
+ if (!peer) {
944
+ _raiseCastFail(peer, "undefined");
945
+ }
946
+ if (typeof peer === "string") {
947
+ _raiseCastFail(peer, "peer");
948
+ }
949
+ if (typeof peer == "number" || typeof peer == "bigint") {
950
+ peer = (0, Helpers_1.returnBigInt)(peer);
951
+ }
952
+ try {
953
+ if (big_integer_1.default.isInstance(peer)) {
954
+ const res = resolveId(peer);
955
+ if (res[1] === tl_1.Api.PeerChannel) {
956
+ return new tl_1.Api.PeerChannel({ channelId: res[0] });
957
+ }
958
+ else if (res[1] === tl_1.Api.PeerChat) {
959
+ return new tl_1.Api.PeerChat({ chatId: res[0] });
960
+ }
961
+ else {
962
+ return new tl_1.Api.PeerUser({ userId: res[0] });
963
+ }
964
+ }
965
+ if (peer.SUBCLASS_OF_ID === undefined) {
966
+ throw new Error();
967
+ }
968
+ if (peer.SUBCLASS_OF_ID === 0x2d45687) {
969
+ // crc32('Peer')
970
+ return peer;
971
+ }
972
+ else if (peer instanceof tl_1.Api.contacts.ResolvedPeer ||
973
+ peer instanceof tl_1.Api.InputNotifyPeer ||
974
+ peer instanceof tl_1.Api.TopPeer ||
975
+ peer instanceof tl_1.Api.Dialog ||
976
+ peer instanceof tl_1.Api.DialogPeer) {
977
+ return peer.peer;
978
+ }
979
+ else if (peer instanceof tl_1.Api.ChannelFull) {
980
+ return new tl_1.Api.PeerChannel({ channelId: peer.id });
981
+ }
982
+ if (peer.SUBCLASS_OF_ID === 0x7d7c6f86 ||
983
+ peer.SUBCLASS_OF_ID === 0xd9c7fc18) {
984
+ // ChatParticipant, ChannelParticipant
985
+ if ("userId" in peer) {
986
+ return new tl_1.Api.PeerUser({ userId: peer.userId });
987
+ }
988
+ }
989
+ peer = getInputPeer(peer, false, false);
990
+ if (peer instanceof tl_1.Api.InputPeerUser) {
991
+ return new tl_1.Api.PeerUser({ userId: peer.userId });
992
+ }
993
+ else if (peer instanceof tl_1.Api.InputPeerChat) {
994
+ return new tl_1.Api.PeerChat({ chatId: peer.chatId });
995
+ }
996
+ else if (peer instanceof tl_1.Api.InputPeerChannel) {
997
+ return new tl_1.Api.PeerChannel({ channelId: peer.channelId });
998
+ }
999
+ }
1000
+ catch (e) { }
1001
+ _raiseCastFail(peer, "peer");
1002
+ }
1003
+ function sanitizeParseMode(mode) {
1004
+ if (mode === "md" || mode === "markdown") {
1005
+ return markdown_1.MarkdownParser;
1006
+ }
1007
+ if (mode === "md2" || mode === "markdownv2") {
1008
+ return markdownv2_1.MarkdownV2Parser;
1009
+ }
1010
+ if (mode == "html") {
1011
+ return html_1.HTMLParser;
1012
+ }
1013
+ if (typeof mode == "object") {
1014
+ if ("parse" in mode && "unparse" in mode) {
1015
+ return mode;
1016
+ }
1017
+ }
1018
+ throw new Error(`Invalid parse mode type ${mode}`);
1019
+ }
1020
+ /**
1021
+ Convert the given peer into its marked ID by default.
1022
+
1023
+ This "mark" comes from the "bot api" format, and with it the peer type
1024
+ can be identified back. User ID is left unmodified, chat ID is negated,
1025
+ and channel ID is prefixed with -100:
1026
+
1027
+ * ``userId``
1028
+ * ``-chatId``
1029
+ * ``-100channel_id``
1030
+
1031
+ The original ID and the peer type class can be returned with
1032
+ a call to :meth:`resolve_id(marked_id)`.
1033
+ * @param peer
1034
+ * @param addMark
1035
+ */
1036
+ function getPeerId(peer, addMark = true) {
1037
+ if (typeof peer == "string" && parseID(peer)) {
1038
+ peer = (0, Helpers_1.returnBigInt)(peer);
1039
+ }
1040
+ // First we assert it's a Peer TLObject, or early return for integers
1041
+ if (big_integer_1.default.isInstance(peer)) {
1042
+ return addMark ? peer.toString() : resolveId(peer)[0].toString();
1043
+ }
1044
+ // Tell the user to use their client to resolve InputPeerSelf if we got one
1045
+ if (peer instanceof tl_1.Api.InputPeerSelf) {
1046
+ _raiseCastFail(peer, "int (you might want to use client.get_peer_id)");
1047
+ }
1048
+ try {
1049
+ peer = getPeer(peer);
1050
+ }
1051
+ catch (e) {
1052
+ _raiseCastFail(peer, "int");
1053
+ }
1054
+ if (peer instanceof tl_1.Api.PeerUser) {
1055
+ return peer.userId.toString();
1056
+ }
1057
+ else if (peer instanceof tl_1.Api.PeerChat) {
1058
+ // Check in case the user mixed things up to avoid blowing up
1059
+ peer.chatId = resolveId((0, Helpers_1.returnBigInt)(peer.chatId))[0];
1060
+ return addMark
1061
+ ? peer.chatId.negate().toString()
1062
+ : peer.chatId.toString();
1063
+ }
1064
+ else if (typeof peer == "object" && "channelId" in peer) {
1065
+ // if (peer instanceof Api.PeerChannel)
1066
+ // Check in case the user mixed things up to avoid blowing up
1067
+ peer.channelId = resolveId((0, Helpers_1.returnBigInt)(peer.channelId))[0];
1068
+ if (!addMark) {
1069
+ return peer.channelId.toString();
1070
+ }
1071
+ // Concat -100 through math tricks, .to_supergroup() on
1072
+ // Madeline IDs will be strictly positive -> log works.
1073
+ return "-100" + peer.channelId.toString();
1074
+ }
1075
+ _raiseCastFail(peer, "int");
1076
+ }
1077
+ /**
1078
+ * Given a marked ID, returns the original ID and its :tl:`Peer` type.
1079
+ * @param markedId
1080
+ */
1081
+ function resolveId(markedId) {
1082
+ if (markedId.greaterOrEquals(big_integer_1.default.zero)) {
1083
+ return [markedId, tl_1.Api.PeerUser];
1084
+ }
1085
+ // There have been report of chat IDs being 10000xyz, which means their
1086
+ // marked version is -10000xyz, which in turn looks like a channel but
1087
+ // it becomes 00xyz (= xyz). Hence, we must assert that there are only
1088
+ // two zeroes.
1089
+ const m = markedId.toString().match(/-100([^0]\d*)/);
1090
+ if (m) {
1091
+ return [(0, big_integer_1.default)(m[1]), tl_1.Api.PeerChannel];
1092
+ }
1093
+ return [markedId.negate(), tl_1.Api.PeerChat];
1094
+ }
1095
+ /**
1096
+ * returns an entity pair
1097
+ * @param entityId
1098
+ * @param entities
1099
+ * @param cache
1100
+ * @param getInputPeer
1101
+ * @returns {{inputEntity: *, entity: *}}
1102
+ * @private
1103
+ */
1104
+ /*CONTEST
1105
+
1106
+ export function _getEntityPair(entityId, entities, cache, getInputPeer = getInputPeer) {
1107
+ const entity = entities.get(entityId)
1108
+ let inputEntity = cache[entityId]
1109
+ if (inputEntity === undefined) {
1110
+ try {
1111
+ inputEntity = getInputPeer(inputEntity)
1112
+ } catch (e) {
1113
+ inputEntity = null
1114
+ }
1115
+ }
1116
+ return {
1117
+ entity,
1118
+ inputEntity
1119
+ }
1120
+ }
1121
+ */
1122
+ function getMessageId(message) {
1123
+ if (!message) {
1124
+ return undefined;
1125
+ }
1126
+ else if (typeof message === "number") {
1127
+ return message;
1128
+ }
1129
+ else if (message.SUBCLASS_OF_ID === 0x790009e3 || "id" in message) {
1130
+ // crc32(b'Message')
1131
+ return message.id;
1132
+ }
1133
+ else {
1134
+ throw new Error(`Invalid message type: ${message.constructor.name}`);
1135
+ }
1136
+ }
1137
+ /**
1138
+ * Parses the given phone, or returns `undefined` if it's invalid.
1139
+ * @param phone
1140
+ */
1141
+ function parsePhone(phone) {
1142
+ phone = phone.toString().replace(/[()\s-]/gm, "");
1143
+ if (phone.startsWith("+") && phone.split("+").length - 1 == 1) {
1144
+ return !isNaN(Number(phone)) ? phone.replace("+", "") : undefined;
1145
+ }
1146
+ }
1147
+ /**
1148
+ * Parses a string ID into a big int
1149
+ * @param id
1150
+ */
1151
+ function parseID(id) {
1152
+ const isValid = /^(-?[0-9][0-9]*)$/.test(id);
1153
+ return isValid ? (0, big_integer_1.default)(id) : undefined;
1154
+ }
1155
+ function resolveInviteLink(link) {
1156
+ throw new Error("not implemented");
1157
+ }
1158
+ /**
1159
+ Parses the given username or channel access hash, given
1160
+ a string, username or URL. Returns a tuple consisting of
1161
+ both the stripped, lowercase username and whether it is
1162
+ a joinchat/ hash (in which case is not lowercase'd).
1163
+
1164
+ Returns ``(undefined, false)`` if the ``username`` or link is not valid.
1165
+
1166
+ * @param username {string}
1167
+ */
1168
+ function parseUsername(username) {
1169
+ username = username.trim();
1170
+ const m = username.match(USERNAME_RE) || username.match(TG_JOIN_RE);
1171
+ if (m) {
1172
+ username = username.replace(m[0], "");
1173
+ if (m[1]) {
1174
+ return {
1175
+ username: username,
1176
+ isInvite: true,
1177
+ };
1178
+ }
1179
+ else {
1180
+ username = rtrim(username, "/");
1181
+ }
1182
+ }
1183
+ if (username.match(VALID_USERNAME_RE)) {
1184
+ return {
1185
+ username: username.toLowerCase(),
1186
+ isInvite: false,
1187
+ };
1188
+ }
1189
+ else {
1190
+ return {
1191
+ username: undefined,
1192
+ isInvite: false,
1193
+ };
1194
+ }
1195
+ }
1196
+ function rtrim(s, mask) {
1197
+ while (~mask.indexOf(s[s.length - 1])) {
1198
+ s = s.slice(0, -1);
1199
+ }
1200
+ return s;
1201
+ }
1202
+ /**
1203
+ * Gets the display name for the given :tl:`User`,
1204
+ :tl:`Chat` or :tl:`Channel`. Returns an empty string otherwise
1205
+ * @param entity
1206
+ */
1207
+ function getDisplayName(entity) {
1208
+ if (entity instanceof tl_1.Api.User) {
1209
+ if (entity.lastName && entity.firstName) {
1210
+ return `${entity.firstName} ${entity.lastName}`;
1211
+ }
1212
+ else if (entity.firstName) {
1213
+ return entity.firstName;
1214
+ }
1215
+ else if (entity.lastName) {
1216
+ return entity.lastName;
1217
+ }
1218
+ else {
1219
+ return "";
1220
+ }
1221
+ }
1222
+ else if (entity instanceof tl_1.Api.Chat || entity instanceof tl_1.Api.Channel) {
1223
+ return entity.title;
1224
+ }
1225
+ return "";
1226
+ }
1227
+ /**
1228
+ * check if a given item is an array like or not
1229
+ * @param item
1230
+ * @returns {boolean}
1231
+ */
1232
+ /*CONTEST
1233
+ Duplicate ?
1234
+ export function isListLike(item) {
1235
+ return (
1236
+ Array.isArray(item) ||
1237
+ (!!item &&
1238
+ typeof item === 'object' &&
1239
+ typeof (item.length) === 'number' &&
1240
+ (item.length === 0 ||
1241
+ (item.length > 0 &&
1242
+ (item.length - 1) in item)
1243
+ )
1244
+ )
1245
+ )
1246
+ }
1247
+ */