TelegramClient.ts 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  1. import { TelegramBaseClient, TelegramClientParams } from "./telegramBaseClient";
  2. import * as authMethods from "./auth";
  3. import * as botMethods from "./bots";
  4. import * as buttonsMethods from "./buttons";
  5. import * as downloadMethods from "./downloads";
  6. import * as parseMethods from "./messageParse";
  7. import * as messageMethods from "./messages";
  8. import * as updateMethods from "./updates";
  9. import * as uploadMethods from "./uploads";
  10. import * as userMethods from "./users";
  11. import * as chatMethods from "./chats";
  12. import * as dialogMethods from "./dialogs";
  13. import * as twoFA from "./2fa";
  14. import type { ButtonLike, Entity, EntityLike, MessageIDLike } from "../define";
  15. import { Api } from "../tl";
  16. import { sanitizeParseMode } from "../Utils";
  17. import type { EventBuilder } from "../events/common";
  18. import { MTProtoSender } from "../network";
  19. import { LAYER } from "../tl/AllTLObjects";
  20. import { betterConsoleLog } from "../Helpers";
  21. import { DownloadMediaInterface } from "./downloads";
  22. import { NewMessage, NewMessageEvent } from "../events";
  23. import { _handleUpdate, _updateLoop } from "./updates";
  24. import { Session } from "../sessions";
  25. import { Album, AlbumEvent } from "../events/Album";
  26. import { CallbackQuery, CallbackQueryEvent } from "../events/CallbackQuery";
  27. import { EditedMessage, EditedMessageEvent } from "../events/EditedMessage";
  28. import { DeletedMessage, DeletedMessageEvent } from "../events/DeletedMessage";
  29. import { LogLevel } from "../extensions/Logger";
  30. import { inspect } from "../inspect";
  31. import { isNode } from "../platform";
  32. /**
  33. * The TelegramClient uses several methods in different files to provide all the common functionality in a nice interface.</br>
  34. * **In short, to create a client you must do:**
  35. *
  36. * ```ts
  37. * import {TelegramClient} from "telegram";
  38. *
  39. * const client = new TelegramClient(new StringSession(''),apiId,apiHash,{});
  40. * ```
  41. *
  42. * You don't need to import any methods that are inside the TelegramClient class as they binding in it.
  43. */
  44. export class TelegramClient extends TelegramBaseClient {
  45. /**
  46. * @param session - a session to be used to save the connection and auth key to. This can be a custom session that inherits MemorySession.
  47. * @param apiId - The API ID you obtained from https://my.telegram.org.
  48. * @param apiHash - The API hash you obtained from https://my.telegram.org.
  49. * @param clientParams - see {@link TelegramClientParams}
  50. */
  51. constructor(
  52. session: string | Session,
  53. apiId: number,
  54. apiHash: string,
  55. clientParams: TelegramClientParams
  56. ) {
  57. super(session, apiId, apiHash, clientParams);
  58. }
  59. // region auth
  60. /**
  61. * Used to handle all aspects of connecting to telegram.<br/>
  62. * This method will connect to the telegram servers and check if the user is already logged in.<br/>
  63. * in the case of a new connection this will sign in if the phone already exists or sign up otherwise<br/>
  64. * By using this method you are **agreeing to Telegram's Terms of Service** https://core.telegram.org/api/terms.<br/>
  65. * this method also calls {@link getMe} to tell telegram that we want to receive updates.<br/>
  66. * @param authParams - see UserAuthParams and BotAuthParams
  67. * @return nothing
  68. * @example
  69. * ```ts
  70. * // this example assumes you've installed and imported the input package. npm i input.
  71. * // This package uses CLI to receive input from the user. you can use your own callback function.
  72. * import { TelegramClient } from "telegram";
  73. * import { StringSession } from "telegram/sessions";
  74. *
  75. * const client = new TelegramClient(new StringSession(''), apiId, apiHash, {});
  76. * // logging in as a bot account
  77. * await client.start(botToken="123456:abcdfgh123456789);
  78. * // logging in as a user account
  79. * await client.start({
  80. * phoneNumber: async () => await input.text("number ?"),
  81. * password: async () => await input.text("password?"),
  82. * phoneCode: async () => await input.text("Code ?"),
  83. * onError: (err) => console.log(err),
  84. * });
  85. * >Number ? +1234567897
  86. * >Code ? 12345
  87. * >password ? 111111
  88. * Logged in as user...
  89. *
  90. * You can now use the client instance to call other api requests.
  91. */
  92. start(authParams: authMethods.UserAuthParams | authMethods.BotAuthParams) {
  93. return authMethods.start(this, authParams);
  94. }
  95. /**
  96. * Checks whether the current client is authorized or not. (logged in as a user)
  97. * @example
  98. * ```ts
  99. * await client.connect();
  100. * if (await client.checkAuthorization()){
  101. * console.log("I am logged in!");
  102. * }else{
  103. * console.log("I am connected to telegram servers but not logged in with any account/bot");
  104. * }
  105. * ```
  106. * @return boolean (true of authorized else false)
  107. */
  108. checkAuthorization() {
  109. return authMethods.checkAuthorization(this);
  110. }
  111. /**
  112. * Logs in as a user. Should only be used when not already logged in.<br/>
  113. * This method will send a code when needed.<br/>
  114. * This will also sign up if needed.
  115. * @example
  116. * ```ts
  117. * await client.connect();
  118. * // we should only use this when we are not already authorized.
  119. * // This function is very similar to `client.start`
  120. * // The minor difference that start checks if already authorized and supports bots as well.
  121. * if (!await client.checkAuthorization()){
  122. * const phoneNumber = "+123456789";
  123. * await client.signIn({
  124. * apiId:132456,
  125. * apiHash:"132456",
  126. * },{
  127. * phoneNumber: async () => await input.text("number ?"),
  128. * password: async () => await input.text("password?"),
  129. * phoneCode: async () => await input.text("Code ?"),
  130. * onError: (err) => console.log(err),
  131. * })
  132. * }
  133. * ```
  134. * @param apiCredentials - credentials to be used.
  135. * @param authParams - user auth params.
  136. */
  137. signInUser(
  138. apiCredentials: authMethods.ApiCredentials,
  139. authParams: authMethods.UserAuthParams
  140. ) {
  141. return authMethods.signInUser(this, apiCredentials, authParams);
  142. }
  143. /**
  144. * logs the user using a QR code to be scanned.<br/>
  145. * this function generates the QR code that needs to be scanned by mobile.
  146. * @example
  147. * '''ts
  148. * await client.connect();
  149. * const user = await client.signInUserWithQrCode({ apiId, apiHash },
  150. * {
  151. * onError: async function(p1: Error) {
  152. * console.log("error", p1);
  153. * // true = stop the authentication processes
  154. * return true;
  155. * },
  156. * qrCode: async (code) => {
  157. * console.log("Convert the next string to a QR code and scan it");
  158. * console.log(
  159. * `tg://login?token=${code.token.toString("base64url")}`
  160. * );
  161. * },
  162. * password: async (hint) => {
  163. * // password if needed
  164. * return "1111";
  165. * }
  166. * }
  167. * );
  168. * console.log("user is", user);
  169. *
  170. * '''
  171. * @param apiCredentials - credentials to be used.
  172. * @param authParams - user auth params.
  173. */
  174. signInUserWithQrCode(
  175. apiCredentials: authMethods.ApiCredentials,
  176. authParams: authMethods.QrCodeAuthParams
  177. ) {
  178. return authMethods.signInUserWithQrCode(
  179. this,
  180. apiCredentials,
  181. authParams
  182. );
  183. }
  184. /**
  185. * Sends a telegram authentication code to the phone number.
  186. * @example
  187. * ```ts
  188. * await client.connect();
  189. * const {phoneCodeHash,isCodeViaApp} = await client.sendCode({
  190. * apiId:1234,
  191. * apiHash:"123456789abcfghj",
  192. * },"+123456798"});
  193. * ```
  194. * @param apiCredentials - credentials to be used.
  195. * @param phoneNumber - the phone number to send the code to
  196. * @param forceSMS - whether to send it as an SMS or a normal in app message
  197. * @return the phone code hash and whether it was sent via app
  198. */
  199. sendCode(
  200. apiCredentials: authMethods.ApiCredentials,
  201. phoneNumber: string,
  202. forceSMS = false
  203. ) {
  204. return authMethods.sendCode(
  205. this,
  206. apiCredentials,
  207. phoneNumber,
  208. forceSMS
  209. );
  210. }
  211. /**
  212. * Uses the 2FA password to sign in the account.<br/>
  213. * This function should be used after the user has signed in with the code they received.
  214. * @param apiCredentials - credentials to be used.
  215. * @param authParams - user auth params.
  216. * @returns the logged in user.
  217. */
  218. signInWithPassword(
  219. apiCredentials: authMethods.ApiCredentials,
  220. authParams: authMethods.UserPasswordAuthParams
  221. ) {
  222. return authMethods.signInWithPassword(this, apiCredentials, authParams);
  223. }
  224. /**
  225. * Used to sign in as a bot.
  226. * @example
  227. * ```ts
  228. * await client.connect();
  229. * const bot = await client.signInBot({
  230. * apiId:1234,
  231. * apiHash:"12345",
  232. * },{
  233. * botToken:"123456:abcdfghae4fg654",
  234. * });
  235. * // we are now logged in as a bot
  236. * console.log("Logged in",bot);
  237. * ```
  238. * @param apiCredentials - credentials to be used.
  239. * @param authParams - user auth params.
  240. * @return instance User of the logged in bot.
  241. */
  242. signInBot(
  243. apiCredentials: authMethods.ApiCredentials,
  244. authParams: authMethods.BotAuthParams
  245. ) {
  246. return authMethods.signInBot(this, apiCredentials, authParams);
  247. }
  248. /**
  249. * Changes the 2FA settings of the logged in user.
  250. Note that this method may be *incredibly* slow depending on the
  251. prime numbers that must be used during the process to make sure
  252. that everything is safe.
  253. Has no effect if both current and new password are omitted.
  254. * @param client: The telegram client instance
  255. * @param isCheckPassword: Must be ``true`` if you want to check the current password
  256. * @param currentPassword: The current password, to authorize changing to ``new_password``.
  257. Must be set if changing existing 2FA settings.
  258. Must **not** be set if 2FA is currently disabled.
  259. Passing this by itself will remove 2FA (if correct).
  260. * @param newPassword: The password to set as 2FA.
  261. If 2FA was already enabled, ``currentPassword`` **must** be set.
  262. Leaving this blank or `undefined` will remove the password.
  263. * @param hint: Hint to be displayed by Telegram when it asks for 2FA.
  264. Must be set when changing or creating a new password.
  265. Has no effect if ``newPassword`` is not set.
  266. * @param email: Recovery and verification email. If present, you must also
  267. set `emailCodeCallback`, else it raises an Error.
  268. * @param emailCodeCallback: If an email is provided, a callback that returns the code sent
  269. to it must also be set. This callback may be asynchronous.
  270. It should return a string with the code. The length of the
  271. code will be passed to the callback as an input parameter.
  272. * @param onEmailCodeError: Called when an error happens while sending an email.
  273. If the callback returns an invalid code, it will raise an rpc error with the message
  274. ``CODE_INVALID``
  275. * @returns Promise<void>
  276. * @throws this method can throw:
  277. "PASSWORD_HASH_INVALID" if you entered a wrong password (or set it to undefined).
  278. "EMAIL_INVALID" if the entered email is wrong
  279. "EMAIL_HASH_EXPIRED" if the user took too long to verify their email
  280. */
  281. async updateTwoFaSettings({
  282. isCheckPassword,
  283. currentPassword,
  284. newPassword,
  285. hint = "",
  286. email,
  287. emailCodeCallback,
  288. onEmailCodeError,
  289. }: twoFA.TwoFaParams) {
  290. return twoFA.updateTwoFaSettings(this, {
  291. isCheckPassword,
  292. currentPassword,
  293. newPassword,
  294. hint,
  295. email,
  296. emailCodeCallback,
  297. onEmailCodeError,
  298. });
  299. }
  300. //endregion auth
  301. //region bot
  302. /**
  303. * Makes an inline query to the specified bot and gets the result list.<br/>
  304. * This is equivalent to writing `@pic something` in clients
  305. * @param bot - the bot entity to which the inline query should be made
  306. * @param query - the query string that should be made for that bot (up to 512 characters). can be empty
  307. * @param entity - The entity where the inline query is being made from.<br/>
  308. * Certain bots use this to display different results depending on where it's used, such as private chats, groups or channels.<br/>
  309. * If specified, it will also be the default entity where the message will be sent after clicked.<br/>
  310. * Otherwise, the “empty peer” will be used, which some bots may not handle correctly.
  311. * @param offset - String offset of the results to be returned. can be empty
  312. * @param geoPoint - The geo point location information to send to the bot for localised results. Available under some bots.
  313. * @return a list of InlineResults
  314. * @example
  315. * ```ts
  316. * // Makes the query to @pic
  317. * const results = await client.inlineQuery("pic", "something");
  318. * // clicks on the first result
  319. * await results[0].click();
  320. * ```
  321. */
  322. inlineQuery(
  323. bot: EntityLike,
  324. query: string,
  325. entity?: Api.InputPeerSelf,
  326. offset?: string,
  327. geoPoint?: Api.TypeInputGeoPoint
  328. ) {
  329. return botMethods.inlineQuery(
  330. this,
  331. bot,
  332. query,
  333. entity,
  334. offset,
  335. geoPoint
  336. );
  337. }
  338. //endregion
  339. //region buttons
  340. /**
  341. * Builds a ReplyInlineMarkup or ReplyKeyboardMarkup for the given buttons.<br/><br/>
  342. * Does nothing if either no buttons are provided or the provided argument is already a reply markup.<br/><br/>
  343. * this function is called internally when passing an array of buttons.
  344. * @param buttons - The button, array of buttons, array of array of buttons or markup to convert into a markup.
  345. * @param inlineOnly - Whether the buttons **must** be inline buttons only or not.
  346. * @example
  347. * ```ts
  348. * import {Button} from "telegram";
  349. * // PS this function is not async
  350. * const markup = client.buildReplyMarkup(Button.inline("Hello!"));
  351. *
  352. * await client.sendMessage(chat, {
  353. * message: "click me!",
  354. * buttons: markup,
  355. * }
  356. *
  357. * // The following example can also be used in a simpler way like so
  358. *
  359. * await client.sendMessage(chat, {
  360. * message: "click me!",
  361. * buttons: [Button.inline("Hello!")],
  362. * }
  363. * ```
  364. */
  365. buildReplyMarkup(
  366. buttons:
  367. | Api.TypeReplyMarkup
  368. | undefined
  369. | ButtonLike
  370. | ButtonLike[]
  371. | ButtonLike[][],
  372. inlineOnly: boolean = false
  373. ) {
  374. return buttonsMethods.buildReplyMarkup(buttons, inlineOnly);
  375. }
  376. //endregion
  377. //region download
  378. /**
  379. * Low-level method to download files from their input location.
  380. * downloadMedia should generally be used over this.
  381. * @param inputLocation - The file location from which the file will be downloaded. See getInputLocation source for a complete list of supported types.
  382. * @param fileParams - {@link DownloadFileParams}
  383. * @return a Buffer downloaded from the inputFile.
  384. * @example
  385. * ```ts
  386. * const photo = message.photo;
  387. * const buffer = await client.downloadFile(
  388. * new Api.InputPhotoFileLocation({
  389. * id: photo.id,
  390. * accessHash: photo.accessHash,
  391. * fileReference: photo.fileReference,
  392. * thumbSize: size.type
  393. * }),
  394. * {
  395. * dcId: photo.dcId,
  396. * fileSize: "m",
  397. * }
  398. * );
  399. * ```
  400. */
  401. downloadFile(
  402. inputLocation: Api.TypeInputFileLocation,
  403. fileParams: downloadMethods.DownloadFileParamsV2 = {}
  404. ) {
  405. return downloadMethods.downloadFileV2(this, inputLocation, fileParams);
  406. }
  407. //region download
  408. /**
  409. * Downloads the profile photo from the given user,chat or channel.<br/>
  410. * This method will return an empty buffer in case of no profile photo.
  411. * @param entity - where to download the photo from.
  412. * @param downloadProfilePhotoParams - {@link DownloadProfilePhotoParams}
  413. * @return buffer containing the profile photo. can be empty in case of no profile photo.
  414. * @example
  415. * ```ts
  416. * // Download your own profile photo
  417. * const buffer = await client.downloadProfilePhoto('me')
  418. * console.log("Downloaded image is",buffer);
  419. * // if you want to save it as a file you can use the fs module on node for that.
  420. * import { promises as fs } from 'fs';
  421. * await fs.writeFile("picture.jpg",buffer);
  422. * ```
  423. */
  424. downloadProfilePhoto(
  425. entity: EntityLike,
  426. downloadProfilePhotoParams: downloadMethods.DownloadProfilePhotoParams = {
  427. isBig: false,
  428. }
  429. ) {
  430. return downloadMethods.downloadProfilePhoto(
  431. this,
  432. entity,
  433. downloadProfilePhotoParams
  434. );
  435. }
  436. /**
  437. * Downloads the given media from a message or a media object.<br/>
  438. * this will return an empty Buffer in case of wrong or empty media.
  439. * @param messageOrMedia - instance of a message or a media.
  440. * @param downloadParams {@link DownloadMediaInterface}
  441. * @return a buffer containing the downloaded data if outputFile is undefined else nothing.
  442. * @example ```ts
  443. * const buffer = await client.downloadMedia(message, {})
  444. * // to save it to a file later on using fs.
  445. * import { promises as fs } from 'fs';
  446. * await fs.writeFile("file",buffer);
  447. * // to use a progress callback you can pass it like so.
  448. * const buffer = await client.downloadMedia(message, {
  449. * progressCallback : console.log
  450. * })
  451. * ```
  452. */
  453. downloadMedia(
  454. messageOrMedia: Api.Message | Api.TypeMessageMedia,
  455. downloadParams?: DownloadMediaInterface
  456. ) {
  457. return downloadMethods.downloadMedia(
  458. this,
  459. messageOrMedia,
  460. downloadParams?.outputFile,
  461. downloadParams?.thumb,
  462. downloadParams?.progressCallback
  463. );
  464. }
  465. //endregion
  466. //region message parse
  467. /**
  468. * This property is the default parse mode used when sending messages. Defaults to {@link MarkdownParser}.<br/>
  469. * It will always be either undefined or an object with parse and unparse methods.<br/>
  470. * When setting a different value it should be one of:<br/>
  471. *<br/>
  472. * - Object with parse and unparse methods.<br/>
  473. * - A str indicating the parse_mode. For Markdown 'md' or 'markdown' may be used. For HTML, 'html' may be used.<br/>
  474. * The parse method should be a function accepting a single parameter, the text to parse, and returning a tuple consisting of (parsed message str, [MessageEntity instances]).<br/>
  475. * <br/>
  476. * The unparse method should be the inverse of parse such that text == unparse(parse(text)).<br/>
  477. * <br/>
  478. * See {@link Api.TypeMessageEntity} for allowed message entities.
  479. * @example
  480. * ```ts
  481. * // gets the current parse mode.
  482. * console.log("parse mode is :", client.parseMode)
  483. * ```
  484. */
  485. get parseMode() {
  486. return this._parseMode;
  487. }
  488. /** Setter for parseMode.
  489. * {@link parseMode}
  490. * @param mode can be md,markdown for Markdown or html for html. can also pass a custom mode.
  491. * pass undefined for no parsing.
  492. * @example
  493. * // sets the mode to HTML
  494. * client.setParseMode("html");
  495. * await client.sendMessage("me",{message:"<u>This is an underline text</u>"});
  496. * // disable formatting
  497. * client.setParseMode(undefined);
  498. * await client.sendMessage("me",{message:"<u> this will be sent as it is</u> ** with no formatting **});
  499. */
  500. setParseMode(
  501. mode:
  502. | "md"
  503. | "markdown"
  504. | "html"
  505. | parseMethods.ParseInterface
  506. | undefined
  507. ) {
  508. if (mode) {
  509. this._parseMode = sanitizeParseMode(mode);
  510. } else {
  511. this._parseMode = undefined;
  512. }
  513. }
  514. //endregion
  515. // region messages
  516. /**
  517. * Iterates over the messages for a given chat.
  518. * <br/>
  519. * The default order is from newest to oldest but can be changed with the reverse param.<br/>
  520. * If either `search`, `filter` or `fromUser` are provided this will use {@link Api.messages.Search} instead of {@link Api.messages.GetHistory}.
  521. * @remarks
  522. * Telegram limits GetHistory requests every 10 requests (1 000 messages) therefore a sleep of 1 seconds will be the default for this limit.
  523. * @param entity - The entity from whom to retrieve the message history.<br/>
  524. * It may be undefined to perform a global search, or to get messages by their ID from no particular chat<br/>
  525. * **Note** that some of the offsets will not work if this is the case.<br/>
  526. * **Note** that if you want to perform a global search, you must set a non-empty search string, a filter. or fromUser.
  527. * @param iterParams - {@link IterMessagesParams}
  528. * @yield Instances of custom {@link Message}
  529. * @example
  530. * ```ts
  531. * // From most-recent to oldest
  532. * for await (const message of client.iterMessages(chat,{}){
  533. * console.log(message.id, message.text)
  534. * }
  535. *
  536. * // From oldest to most-recent
  537. * for await (const message of client.iterMessages(chat,{reverse:true}){
  538. * console.log(message.id, message.text)
  539. * }
  540. *
  541. * // Filter by sender
  542. * for await (const message of client.iterMessages(chat,{fromUser:"me"}){
  543. * console.log(message.id, message.text)
  544. * }
  545. *
  546. * // Server-side search with fuzzy text
  547. * for await (const message of client.iterMessages(chat,{search:"hello"}){
  548. * console.log(message.id, message.text)
  549. * }
  550. *
  551. * // Filter by message type:
  552. * import { Api } from "telegram";
  553. * for await (const message of client.iterMessages(chat,{filter: Api.InputMessagesFilterPhotos}){
  554. * console.log(message.id, message.photo)
  555. * }
  556. *
  557. * // Getting comments from a post in a channel:
  558. * * for await (const message of client.iterMessages(chat,{replyTo: 123}){
  559. * console.log(message.chat.title,, message.text)
  560. * }
  561. * ```
  562. */
  563. iterMessages(
  564. entity: EntityLike | undefined,
  565. iterParams: Partial<messageMethods.IterMessagesParams> = {}
  566. ) {
  567. return messageMethods.iterMessages(this, entity, iterParams);
  568. }
  569. /**
  570. * Same as iterMessages() but returns a TotalList instead.<br/>
  571. * if the `limit` is not set, it will be 1 by default unless both `minId` **and** `maxId` are set. in which case the entire range will be returned.<br/>
  572. * @param entity - The entity from whom to retrieve the message history. see {@link iterMessages}.<br/>
  573. * @param getMessagesParams - see {@link IterMessagesParams}.
  574. * @return {@link TotalList} of messages.
  575. * @example
  576. * ```ts
  577. * // The totalList has a .total attribute which will show the complete number of messages even if none are fetched.
  578. * // Get 0 photos and print the total to show how many photos there are
  579. * import { Api } from "telegram";
  580. * const photos = await client.getMessages(chat, {limit: 0, filter:Api.InputMessagesFilterPhotos})
  581. * console.log(photos.total)
  582. *
  583. * // Get all the photos
  584. * const photos = await client.getMessages(chat, {limit: undefined, filter:Api.InputMessagesFilterPhotos})
  585. *
  586. // Get messages by ID:
  587. * const messages = await client.getMessages(chat, {ids:1337})
  588. * const message_1337 = messages[0];
  589. * ```
  590. */
  591. getMessages(
  592. entity: EntityLike | undefined,
  593. getMessagesParams: Partial<messageMethods.IterMessagesParams> = {}
  594. ) {
  595. return messageMethods.getMessages(this, entity, getMessagesParams);
  596. }
  597. /**
  598. * Sends a message to the specified user, chat or channel.<br/>
  599. * The default parse mode is the same as the official applications (a custom flavour of markdown). **bold**, `code` or __italic__ are available.<br/>
  600. * In addition you can send [links](https://example.com) and [mentions](@username) (or using IDs like in the Bot API: [mention](tg://user?id=123456789)) and pre blocks with three backticks.<br/>
  601. * <br/>
  602. * Sending a /start command with a parameter (like ?start=data) is also done through this method. Simply send '/start data' to the bot.<br/>
  603. * <br/>
  604. * See also Message.respond() and Message.reply().
  605. *
  606. * @param entity - Who to sent the message to.
  607. * @param sendMessageParams - see {@link SendMessageParams}
  608. * @return
  609. * The sent custom Message.
  610. * @example
  611. * ```ts
  612. * // Markdown is the default.
  613. * await client.sendMessage("me",{message:"Hello **world!**});
  614. *
  615. * // Defaults to another parse mode.
  616. * client.setParseMode("HTML");
  617. *
  618. * await client.sendMessage('me', {message:'Some <b>bold</b> and <i>italic</i> text'})
  619. * await client.sendMessage('me', {message:'An <a href="https://example.com">URL</a>'})
  620. * await client.sendMessage('me', {message:'<a href="tg://user?id=me">Mentions</a>'})
  621. *
  622. * // Explicit parse mode.
  623. * // No parse mode by default
  624. * client.setParseMode(undefined);
  625. * //...but here I want markdown
  626. * await client.sendMessage('me', {message:'Hello, **world**!', {parseMode:"md"}})
  627. *
  628. * // ...and here I need HTML
  629. * await client.sendMessage('me', {message:'Hello, <i>world</i>!', {parseMode='html'}})
  630. *
  631. *
  632. * // Scheduling a message to be sent after 5 minutes
  633. *
  634. * await client.sendMessage(chat, {message:'Hi, future!', schedule:(60 * 5) + (Date.now() / 1000)})
  635. *
  636. * ```
  637. */
  638. sendMessage(
  639. entity: EntityLike,
  640. sendMessageParams: messageMethods.SendMessageParams = {}
  641. ) {
  642. return messageMethods.sendMessage(this, entity, sendMessageParams);
  643. }
  644. /**
  645. * Forwards the given messages to the specified entity.<br/>
  646. *<br/>
  647. * If you want to "forward" a message without the forward header
  648. * (the "forwarded from" text), you should use `sendMessage` with
  649. * the original message instead. This will send a copy of it.
  650. *<br/>
  651. * See also {@link Message.forwardTo}`.
  652. * @param entity - To which entity the message(s) will be forwarded.
  653. * @param forwardMessagesParams - see {@link ForwardMessagesParams}
  654. * @return The list of forwarded Message, Note.<br/>
  655. * if some messages failed to be forwarded the returned list will have them as undefined.
  656. * @example ```ts
  657. * // a single one
  658. * await client.forwardMessages(chat, {messages: message});
  659. * // or
  660. * await client.forwardMessages(chat, {messages:messageId,fromPeer:fromChat});
  661. * // or
  662. * await message.forwardTo(chat)
  663. *
  664. * // multiple
  665. * await client.forwardMessages(chat, {messages:messages});
  666. * // or
  667. * await client.forwardMessages(chat, {messages:messageIds,fromPeer:fromChat});
  668. *
  669. * // Forwarding as a copy
  670. * await client.sendMessage(chat, {message:message});
  671. * ```
  672. */
  673. forwardMessages(
  674. entity: EntityLike,
  675. forwardMessagesParams: messageMethods.ForwardMessagesParams
  676. ) {
  677. return messageMethods.forwardMessages(
  678. this,
  679. entity,
  680. forwardMessagesParams
  681. );
  682. }
  683. /**
  684. * Used to edit a message by changing it's text or media
  685. * message refers to the message to be edited not what to edit
  686. * text refers to the new text
  687. * See also Message.edit()<br/>
  688. * Notes: It is not possible to edit the media of a message that doesn't contain media.
  689. * @param entity - From which chat to edit the message.<br/>
  690. * This can also be the message to be edited, and the entity will be inferred from it, so the next parameter will be assumed to be the message text.<br/>
  691. * You may also pass a InputBotInlineMessageID, which is the only way to edit messages that were sent after the user selects an inline query result. Not supported yet!
  692. * @param editMessageParams - see {@link EditMessageParams}.
  693. * @return The edited Message.
  694. * @throws
  695. * `MESSAGE_AUTHOR_REQUIRED` if you're not the author of the message but tried editing it anyway.
  696. * `MESSAGE_NOT_MODIFIED` if the contents of the message were not modified at all.
  697. * `MESSAGE_ID_INVALID` if the ID of the message is invalid (the ID itself may be correct, but the message with that ID cannot be edited).<br/>
  698. * For example, when trying to edit messages with a reply markup (or clear markup) this error will be raised.
  699. * @example
  700. * ```ts
  701. * const message = await client.sendMessage(chat,{message:"Hi!"});
  702. *
  703. * await client.editMessage(chat,{message:message,text:"Hello!"}
  704. * // or
  705. * await client.editMessage(chat,{message:message.id,text:"Hello!"}
  706. * ```
  707. */
  708. editMessage(
  709. entity: EntityLike,
  710. editMessageParams: messageMethods.EditMessageParams
  711. ) {
  712. return messageMethods.editMessage(this, entity, editMessageParams);
  713. }
  714. /**
  715. * Deletes the given messages, optionally "for everyone".
  716. *
  717. * See also {@link Message.delete}`.
  718. *
  719. * @remarks This method does **not** validate that the message IDs belong to the chat that you passed! It's possible for the method to delete messages from different private chats and small group chats at once, so make sure to pass the right IDs.
  720. * @param entity - From who the message will be deleted. This can actually be `undefined` for normal chats, but **must** be present for channels and megagroups.
  721. * @param messageIds - The IDs (or ID) or messages to be deleted.
  722. * @param revoke - Whether the message should be deleted for everyone or not.
  723. * By default it has the opposite behaviour of official clients,
  724. * and it will delete the message for everyone.
  725. * Disabling this has no effect on channels or megagroups,
  726. * since it will unconditionally delete the message for everyone.
  727. * @return
  728. * A list of {@link AffectedMessages}, each item being the result for the delete calls of the messages in chunks of 100 each.
  729. * @example
  730. * ```ts
  731. * await client.deleteMessages(chat, messages);
  732. *
  733. * await client.deleteMessages(chat, messages, {revoke:false});
  734. * ```
  735. */
  736. deleteMessages(
  737. entity: EntityLike | undefined,
  738. messageIds: MessageIDLike[],
  739. { revoke = true }
  740. ) {
  741. return messageMethods.deleteMessages(this, entity, messageIds, {
  742. revoke: revoke,
  743. });
  744. }
  745. /**
  746. * Pins a message in a chat.
  747. *
  748. * See also {@link Message.pin}`.
  749. *
  750. * @remarks The default behavior is to **not** notify members, unlike the official applications.
  751. * @param entity - The chat where the message should be pinned.
  752. * @param message - The message or the message ID to pin. If it's `undefined`, all messages will be unpinned instead.
  753. * @param pinMessageParams - see {@link UpdatePinMessageParams}.
  754. * @return
  755. * The pinned message. if message is undefined the return will be {@link AffectedHistory}
  756. * @example
  757. * ```ts
  758. * const message = await client.sendMessage(chat, 'GramJS is awesome!');
  759. *
  760. * await client.pinMessage(chat, message);
  761. * ```
  762. */
  763. pinMessage(
  764. entity: EntityLike,
  765. message?: undefined,
  766. pinMessageParams?: messageMethods.UpdatePinMessageParams
  767. ): Promise<Api.messages.AffectedHistory>;
  768. pinMessage(
  769. entity: EntityLike,
  770. message: MessageIDLike,
  771. pinMessageParams?: messageMethods.UpdatePinMessageParams
  772. ): Promise<Api.Message>;
  773. pinMessage(
  774. entity: EntityLike,
  775. message?: any,
  776. pinMessageParams?: messageMethods.UpdatePinMessageParams
  777. ) {
  778. return messageMethods.pinMessage(
  779. this,
  780. entity,
  781. message,
  782. pinMessageParams
  783. );
  784. }
  785. /**
  786. * Unpins a message in a chat.
  787. *
  788. * See also {@link Message.unpin}`.
  789. *
  790. * @remarks The default behavior is to **not** notify members, unlike the official applications.
  791. * @param entity - The chat where the message should be unpinned.
  792. * @param message - The message or the message ID to unpin. If it's `undefined`, all messages will be unpinned instead.
  793. * @param pinMessageParams - see {@link UpdatePinMessageParams}.
  794. * @return
  795. * The pinned message. if message is undefined the return will be {@link AffectedHistory}
  796. * @example
  797. * ```ts
  798. * const message = await client.sendMessage(chat, 'GramJS is awesome!');
  799. *
  800. * // unpin one message
  801. * await client.unpinMessage(chat, message);
  802. *
  803. * // unpin all messages
  804. * await client.unpinMessage(chat);
  805. * ```
  806. */
  807. unpinMessage(
  808. entity: EntityLike,
  809. message?: undefined,
  810. pinMessageParams?: messageMethods.UpdatePinMessageParams
  811. ): Promise<Api.messages.AffectedHistory>;
  812. unpinMessage(
  813. entity: EntityLike,
  814. message: MessageIDLike,
  815. pinMessageParams?: messageMethods.UpdatePinMessageParams
  816. ): Promise<undefined>;
  817. unpinMessage(
  818. entity: EntityLike,
  819. message?: any,
  820. unpinMessageParams?: messageMethods.UpdatePinMessageParams
  821. ) {
  822. return messageMethods.unpinMessage(
  823. this,
  824. entity,
  825. message,
  826. unpinMessageParams
  827. ) as Promise<Api.messages.AffectedHistory | undefined>;
  828. }
  829. /**
  830. * Marks messages as read and optionally clears mentions. <br/>
  831. * This effectively marks a message as read (or more than one) in the given conversation. <br />
  832. * If a message or maximum ID is provided, all the messages up to and
  833. * including such ID will be marked as read (for all messages whose ID ≤ max_id).
  834. *
  835. * See also {@link Message.markRead}`.
  836. *
  837. * @remarks If neither message nor maximum ID are provided, all messages will be marked as read by assuming that `max_id = 0`.
  838. * @param entity - The chat where the message should be pinned.
  839. * @param message - The message or the message ID to pin. If it's `undefined`, all messages will be unpinned instead.
  840. * @param markAsReadParams - see {@link MarkAsReadParams}.
  841. * @return boolean
  842. * @example
  843. * ```ts
  844. * // using a Message object
  845. * const message = await client.sendMessage(chat, 'GramJS is awesome!');
  846. * await client.markAsRead(chat, message)
  847. * // ...or using the int ID of a Message
  848. * await client.markAsRead(chat, message.id);
  849. *
  850. * // ...or passing a list of messages to mark as read
  851. * await client.markAsRead(chat, messages)
  852. * ```
  853. */
  854. markAsRead(
  855. entity: EntityLike,
  856. message?: MessageIDLike | MessageIDLike[],
  857. markAsReadParams?: messageMethods.MarkAsReadParams
  858. ) {
  859. return messageMethods.markAsRead(
  860. this,
  861. entity,
  862. message,
  863. markAsReadParams
  864. );
  865. }
  866. //endregion
  867. //region dialogs
  868. /**
  869. * Iterator over the dialogs (open conversations/subscribed channels) sequentially.<br/>
  870. * The order is the same as the one seen in official applications. (dialogs that had recent messages come first)
  871. * @param iterDialogsParams - see {@link IterDialogsParams}
  872. * @yield instances of custom {@link Dialog}.
  873. * @example
  874. * ```ts
  875. * // logs all dialog IDs and their title.
  876. * for await (const dialog of client.iterDialogs({})){
  877. * console.log(`${dialog.id}: ${dialog.title}`);
  878. * }
  879. * ```
  880. */
  881. iterDialogs(iterDialogsParams: dialogMethods.IterDialogsParams = {}) {
  882. return dialogMethods.iterDialogs(this, iterDialogsParams);
  883. }
  884. /**
  885. * Same as iterDialogs but returns a TotalList instead of an iterator.
  886. * @param params - {@link IterDialogsParams}
  887. * @example
  888. * ```ts
  889. * // Get all open conversation, print the title of the first
  890. * const dialogs = await client.getDialogs({});
  891. * const first = dialogs[0];
  892. * console.log(first.title);
  893. * <br/>
  894. * // Use the dialog somewhere else
  895. * await client.sendMessage(first, {message: "hi"});
  896. * <br/>
  897. * // Getting only non-archived dialogs (both equivalent)
  898. * non_archived = await client.get_dialogs({folder:0})
  899. * non_archived = await client.get_dialogs({archived:false})
  900. * <br/>
  901. * // Getting only archived dialogs (both equivalent)
  902. * archived = await client.get_dialogs({folder:1})
  903. * archived = await client.get_dialogs({archived:true})
  904. * ```
  905. */
  906. getDialogs(params: dialogMethods.IterDialogsParams = {}) {
  907. return dialogMethods.getDialogs(this, params);
  908. }
  909. //endregion
  910. //region chats
  911. /**
  912. * Iterates over the participants belonging to a specified chat , channel or supergroup.<br/>
  913. * <br/>
  914. * Channels can return a maximum of 200 users while supergroups can return up to 10 000.<br/>
  915. * You must be an admin to retrieve users from a channel.<br/>
  916. * @param entity - The entity from which to retrieve the participants list.
  917. * @param params - {@link IterParticipantsParams}
  918. * @remarks
  919. * The filter ChannelParticipantsBanned will return restricted users. If you want banned users you should use ChannelParticipantsKicked instead.
  920. * @yield The User objects returned by GetParticipants with an additional .participant attribute<br/>
  921. * which is the matched ChannelParticipant type for channels/supergroup or ChatParticipants for normal chats.
  922. * @example
  923. * ```ts
  924. * // logs all user IDs in a chat.
  925. * for await (const user of client.iterParticipants(chat)){
  926. * console.log("User id",user.id);
  927. * }
  928. *
  929. * // Searches by name.
  930. * for await (const user of client.iterParticipants(chat, {search: "name"})){
  931. * console.log("Username is ",user.username); // Some users don't have a username so this can be undefined.
  932. * }
  933. *
  934. * // Filter by admins.
  935. * import { Api } from "telegram";
  936. *
  937. * for await (const user of client.iterParticipants(chat, {filter: Api.ChannelParticipantsAdmins})){
  938. * console.log("admin first name is ",user.firstName);
  939. * }
  940. * ```
  941. */
  942. iterParticipants(
  943. entity: EntityLike,
  944. params: chatMethods.IterParticipantsParams = {}
  945. ) {
  946. return chatMethods.iterParticipants(this, entity, params);
  947. }
  948. /**
  949. * Exact same as iterParticipants but returns a TotalList instead.<br/>
  950. * This can be used if you want to retrieve a list instead of iterating over the users.
  951. * @param entity - entity to get users from.
  952. * @param params - {@link IterParticipantsParams}.
  953. * @return
  954. */
  955. getParticipants(
  956. entity: EntityLike,
  957. params: chatMethods.IterParticipantsParams = {}
  958. ) {
  959. return chatMethods.getParticipants(this, entity, params);
  960. }
  961. //endregion
  962. //region updates
  963. /** TODO */
  964. on(event: any) {
  965. return updateMethods.on(this, event);
  966. }
  967. /**
  968. * Registers a new event handler callback.<br/>
  969. * <br/>
  970. * The callback will be called when the specified event occurs.
  971. * @param callback - The callable function accepting one parameter to be used.<br/>
  972. * **Note** the event type passed in the callback will change depending on the eventBuilder.
  973. * @param event -The event builder class or instance to be used,
  974. * for example ``new events.NewMessage({});``.
  975. * If left unspecified, {@link Raw} (the {@link Api.TypeUpdate} objects with no further processing) will be passed instead.<br>
  976. *
  977. * @example
  978. *```ts
  979. * import {TelegramClient} from "telegram";
  980. * const client = new TelegramClient(new StringSession(''), apiId, apiHash, {});
  981. *
  982. * async function handler(event: NewMessageEvent) {
  983. * ...
  984. * }
  985. * client.addEventHandler(handler, new NewMessage({}));
  986. ```
  987. */
  988. addEventHandler(
  989. callback: { (event: NewMessageEvent): void },
  990. event: NewMessage
  991. ): void;
  992. addEventHandler(
  993. callback: { (event: CallbackQueryEvent): void },
  994. event: CallbackQuery
  995. ): void;
  996. addEventHandler(
  997. callback: { (event: AlbumEvent): void },
  998. event: Album
  999. ): void;
  1000. addEventHandler(
  1001. callback: { (event: EditedMessageEvent): void },
  1002. event: EditedMessage
  1003. ): void;
  1004. addEventHandler(
  1005. callback: { (event: DeletedMessageEvent): void },
  1006. event: DeletedMessage
  1007. ): void;
  1008. addEventHandler(
  1009. callback: { (event: any): void },
  1010. event?: EventBuilder
  1011. ): void;
  1012. addEventHandler(callback: { (event: any): void }, event?: EventBuilder) {
  1013. return updateMethods.addEventHandler(this, callback, event);
  1014. }
  1015. /**
  1016. * Inverse operation of addEventHandler().<br>
  1017. *
  1018. * @param callback - the callback function to be removed.
  1019. * @param event - the type of the event.
  1020. */
  1021. removeEventHandler(callback: CallableFunction, event: EventBuilder) {
  1022. return updateMethods.removeEventHandler(this, callback, event);
  1023. }
  1024. /**
  1025. * Lists all registered event handlers.
  1026. * @return pair of [eventBuilder,CallableFunction]
  1027. */
  1028. listEventHandlers() {
  1029. return updateMethods.listEventHandlers(this);
  1030. }
  1031. // region uploads
  1032. /**
  1033. * Uploads a file to Telegram's servers, without sending it.
  1034. * @remarks generally it's better to use {@link sendFile} instead.
  1035. * This method returns a handle (an instance of InputFile or InputFileBig, as required) which can be later used before it expires (they are usable during less than a day).<br/>
  1036. * Uploading a file will simply return a "handle" to the file stored remotely in the Telegram servers,
  1037. * which can be later used on. This will not upload the file to your own chat or any chat at all.
  1038. * This also can be used to update profile pictures
  1039. * @param fileParams see {@link UploadFileParams}
  1040. * @return {@link Api.InputFileBig} if the file size is larger than 10mb otherwise {@link Api.InputFile}
  1041. * @example
  1042. * ```ts
  1043. * const toUpload = new CustomFile("photo.jpg", fs.statSync("../photo.jpg").size, "../photo.jpg");
  1044. * const file = await client.uploadFile({
  1045. * file: toUpload,
  1046. * workers: 1,
  1047. * });
  1048. * await client.invoke(new Api.photos.UploadProfilePhoto({
  1049. * file: file,
  1050. * }));
  1051. * ```
  1052. */
  1053. uploadFile(fileParams: uploadMethods.UploadFileParams) {
  1054. return uploadMethods.uploadFile(this, fileParams);
  1055. }
  1056. /**
  1057. * Sends message with the given file to the specified entity.
  1058. * This uses {@link uploadFile} internally so if you want more control over uploads you can use that.
  1059. * @param entity - who will receive the file.
  1060. * @param sendFileParams - see {@link SendFileInterface}
  1061. * @example
  1062. * ```
  1063. * // Normal files like photos
  1064. * await client.sendFile(chat, {file:'/my/photos/me.jpg', caption:"It's me!"})
  1065. * // or
  1066. * await client.sendMessage(chat, {message:"It's me!", file:'/my/photos/me.jpg'})
  1067. *
  1068. * Voice notes or round videos
  1069. * await client.sendFile(chat, {file: '/my/songs/song.mp3', voiceNote:True})
  1070. * await client.sendFile(chat, {file: '/my/videos/video.mp4', videoNote:True})
  1071. *
  1072. * // Custom thumbnails
  1073. * await client.sendFile(chat, {file:'/my/documents/doc.txt', thumb:'photo.jpg'})
  1074. *
  1075. * // Only documents
  1076. * await client.sendFile(chat, {file:'/my/photos/photo.png', forceDocument:True})
  1077. *
  1078. * //logging progress
  1079. * await client.sendFile(chat, {file: file, progressCallback=console.log})
  1080. *
  1081. * // Dices, including dart and other future emoji
  1082. * await client.sendFile(chat, {file:new Api.InputMediaDice("")})
  1083. * await client.sendFile(chat, {file:new Api.InputMediaDice("🎯")})
  1084. *
  1085. * // Contacts
  1086. * await client.sendFile(chat, {file: new Api.InputMediaContact({
  1087. * phoneNumber:'+1 123 456 789',
  1088. * firstName:'Example',
  1089. * lastName:'',
  1090. * vcard:''
  1091. * }))
  1092. * ```
  1093. */
  1094. sendFile(
  1095. entity: EntityLike,
  1096. sendFileParams: uploadMethods.SendFileInterface
  1097. ) {
  1098. return uploadMethods.sendFile(this, entity, sendFileParams);
  1099. }
  1100. // endregion
  1101. //region user methods
  1102. /**
  1103. * invokes raw Telegram requests.<br/>
  1104. * This is a low level method that can be used to call manually any Telegram API method.<br/>
  1105. * Generally this should only be used when there isn't a friendly method that does what you need.<br/>
  1106. * All available requests and types are found under the `Api.` namespace.
  1107. * @param request - The request to send. this should be of type request.
  1108. * @param sender - Optional sender to use to send the requests. defaults to main sender.
  1109. * @return The response from Telegram.
  1110. * @example
  1111. * ```ts
  1112. * //
  1113. * const result = await client.invoke(new Api.account.CheckUsername({
  1114. * username: 'some string here'
  1115. * }));
  1116. * console.log("does this username exist?",result);
  1117. *
  1118. * ```
  1119. */
  1120. invoke<R extends Api.AnyRequest>(
  1121. request: R,
  1122. sender?: MTProtoSender
  1123. ): Promise<R["__response"]> {
  1124. return userMethods.invoke(this, request, sender);
  1125. }
  1126. /**
  1127. * Gets the current logged in {@link Api.User}.
  1128. * If the user has not logged in this will throw an error.
  1129. * @param inputPeer - Whether to return the input peer version {@link Api.InputPeerUser} or the whole user {@link Api.User}.
  1130. * @return Your own {@link Api.User}
  1131. * @example
  1132. * ```ts
  1133. * const me = await client.getMe();
  1134. * console.log("My username is",me.username);
  1135. * ```
  1136. */
  1137. getMe(inputPeer = false) {
  1138. return userMethods.getMe(this, inputPeer);
  1139. }
  1140. /**
  1141. * Return true if the signed-in user is a bot, false otherwise.
  1142. * @example
  1143. * ```ts
  1144. * if (await client.isBot()){
  1145. * console.log("I am a bot. PI is 3.14159265359);
  1146. * } else {
  1147. * console.log("I am a human. Pies are delicious);
  1148. * }
  1149. * ```
  1150. */
  1151. isBot() {
  1152. return userMethods.isBot(this);
  1153. }
  1154. /**
  1155. * Returns true if the user is authorized (logged in).
  1156. * @example
  1157. * if (await client.isUserAuthorized()){
  1158. * console.log("I am authorized. I can call functions and use requests");
  1159. * }else{
  1160. * console.log("I am not logged in. I need to sign in first before being able to call methods");
  1161. * }
  1162. */
  1163. isUserAuthorized() {
  1164. return userMethods.isUserAuthorized(this);
  1165. }
  1166. /**
  1167. * Turns the given entity into a valid Telegram {@link Api.User}, {@link Api.Chat} or {@link Api.Channel}.<br/>
  1168. * You can also pass a list or iterable of entities, and they will be efficiently fetched from the network.
  1169. * @remarks Telegram does not allow to get user profile by integer id if current client had never "saw" it.
  1170. * @param entity - If a username is given, the username will be resolved making an API call every time.<br/>
  1171. * Resolving usernames is an expensive operation and will start hitting flood waits around 50 usernames in a short period of time.<br/>
  1172. * <br/>
  1173. * Similar limits apply to invite links, and you should use their ID instead.<br/>
  1174. * Using phone numbers (from people in your contact list), exact names, integer IDs or Peer rely on a getInputEntity first,<br/>
  1175. * which in turn needs the entity to be in cache, unless a InputPeer was passed.<br/>
  1176. * <br/>
  1177. * If the entity can't be found, ValueError will be raised.
  1178. * @return
  1179. * {@link Api.Chat},{@link Api.Chat} or {@link Api.Channel} corresponding to the input entity. A list will be returned if more than one was given.
  1180. * @example
  1181. * ```ts
  1182. * const me = await client.getEntity("me");
  1183. * console.log("My name is",utils.getDisplayName(me));
  1184. *
  1185. * const chat = await client.getInputEntity("username");
  1186. * for await (const message of client.iterMessages(chat){
  1187. * console.log("Message text is",message.text);
  1188. * }
  1189. *
  1190. * // Note that you could have used the username directly, but it's
  1191. * // good to use getInputEntity if you will reuse it a lot.
  1192. * ```
  1193. */
  1194. getEntity(entity: EntityLike): Promise<Entity>;
  1195. getEntity(entity: EntityLike[]): Promise<Entity[]>;
  1196. getEntity(entity: any) {
  1197. return userMethods.getEntity(this, entity);
  1198. }
  1199. /**
  1200. * Turns the given entity into its input entity version.<br/>
  1201. * Almost all requests use this kind of InputPeer, so this is the most suitable call to make for those cases.<br/>
  1202. * **Generally you should let the library do its job** and don't worry about getting the input entity first, but if you're going to use an entity often, consider making the call.
  1203. * @param entity - If a username or invite link is given, the library will use the cache.<br/>
  1204. * This means that it's possible to be using a username that changed or an old invite link (this only happens if an invite link for a small group chat is used after it was upgraded to a mega-group).
  1205. *<br/>
  1206. * - If the username or ID from the invite link is not found in the cache, it will be fetched. The same rules apply to phone numbers ('+34 123456789') from people in your contact list.
  1207. *<br/>
  1208. * - If an exact name is given, it must be in the cache too. This is not reliable as different people can share the same name and which entity is returned is arbitrary,<br/>
  1209. * and should be used only for quick tests.
  1210. *<br/>
  1211. * - If a positive integer ID is given, the entity will be searched in cached users, chats or channels, without making any call.
  1212. *<br/>
  1213. * - If a negative integer ID is given, the entity will be searched exactly as either a chat (prefixed with -) or as a channel (prefixed with -100).
  1214. *<br/>
  1215. * - If a Peer is given, it will be searched exactly in the cache as either a user, chat or channel.
  1216. *<br/>
  1217. * - If the given object can be turned into an input entity directly, said operation will be done.
  1218. *<br/>
  1219. * -If the entity can't be found, this will throw an error.
  1220. * @return
  1221. * {@link Api.InputPeerUser} , {@link Api.InputPeerChat} , {@link Api.InputPeerChannel} or {@link Api.InputPeerSelf} if the parameter is "me" or "self"
  1222. * @example
  1223. * ```ts
  1224. * // If you're going to use "username" often in your code
  1225. * // (make a lot of calls), consider getting its input entity
  1226. * // once, and then using the "user" everywhere instead.
  1227. * user = await client.getInputEntity('username')
  1228. *
  1229. * // The same applies to IDs, chats or channels.
  1230. * chat = await client.getInputEntity(-123456789)
  1231. * ```
  1232. */
  1233. getInputEntity(entity: EntityLike) {
  1234. return userMethods.getInputEntity(this, entity);
  1235. }
  1236. /**
  1237. * Gets the ID for the given entity.<br/>
  1238. * This method needs to be async because peer supports usernames, invite-links, phone numbers (from people in your contact list), etc.<br/>
  1239. * <br/>
  1240. * If addMark is false, then a positive ID will be returned instead. By default, bot-API style IDs (signed) are returned.
  1241. * @param peer
  1242. * @param addMark - whether to return a bot api style id.
  1243. * @return the ID of the entity.
  1244. * @example
  1245. * ```ts
  1246. * console.log(await client.getPeerId("me"));
  1247. * ```
  1248. */
  1249. getPeerId(peer: EntityLike, addMark = true) {
  1250. return userMethods.getPeerId(this, peer, addMark);
  1251. }
  1252. /** @hidden */
  1253. _getInputDialog(peer: any) {
  1254. return userMethods._getInputDialog(this, peer);
  1255. }
  1256. /** @hidden */
  1257. _getInputNotify(notify: any) {
  1258. return userMethods._getInputNotify(this, notify);
  1259. }
  1260. //endregion
  1261. /** @hidden */
  1262. async _handleReconnect() {
  1263. try {
  1264. await this.getMe();
  1265. } catch (e) {
  1266. this._log.error(`Error while trying to reconnect`);
  1267. if (this._log.canSend(LogLevel.ERROR)) {
  1268. console.error(e);
  1269. }
  1270. }
  1271. }
  1272. //region base methods
  1273. async connect() {
  1274. await this._initSession();
  1275. if (this._sender === undefined) {
  1276. this._sender = new MTProtoSender(this.session.getAuthKey(), {
  1277. logger: this._log,
  1278. dcId: this.session.dcId || 4,
  1279. retries: this._connectionRetries,
  1280. delay: this._retryDelay,
  1281. autoReconnect: this._autoReconnect,
  1282. connectTimeout: this._timeout,
  1283. authKeyCallback: this._authKeyCallback.bind(this),
  1284. updateCallback: _handleUpdate.bind(this),
  1285. isMainSender: true,
  1286. client: this,
  1287. securityChecks: this._securityChecks,
  1288. autoReconnectCallback: this._handleReconnect.bind(this),
  1289. });
  1290. }
  1291. // set defaults vars
  1292. this._sender.userDisconnected = false;
  1293. this._sender._userConnected = false;
  1294. this._sender._reconnecting = false;
  1295. this._sender._disconnected = true;
  1296. const connection = new this._connection({
  1297. ip: this.session.serverAddress,
  1298. port: this.useWSS ? 443 : 80,
  1299. dcId: this.session.dcId,
  1300. loggers: this._log,
  1301. proxy: this._proxy,
  1302. socket: this.networkSocket,
  1303. testServers: this.testServers,
  1304. });
  1305. const newConnection = await this._sender.connect(connection);
  1306. if (!newConnection) {
  1307. // we're already connected so no need to reset auth key.
  1308. if (!this._loopStarted) {
  1309. _updateLoop(this);
  1310. this._loopStarted = true;
  1311. }
  1312. return;
  1313. }
  1314. this.session.setAuthKey(this._sender.authKey);
  1315. this._initRequest.query = new Api.help.GetConfig();
  1316. this._log.info(`Using LAYER ${LAYER} for initial connect`);
  1317. await this._sender.send(
  1318. new Api.InvokeWithLayer({
  1319. layer: LAYER,
  1320. query: this._initRequest,
  1321. })
  1322. );
  1323. if (!this._loopStarted) {
  1324. _updateLoop(this);
  1325. this._loopStarted = true;
  1326. }
  1327. this._reconnecting = false;
  1328. }
  1329. //endregion
  1330. // region Working with different connections/Data Centers
  1331. /** @hidden */
  1332. async _switchDC(newDc: number) {
  1333. this._log.info(`Reconnecting to new data center ${newDc}`);
  1334. const DC = await this.getDC(newDc);
  1335. this.session.setDC(newDc, DC.ipAddress, DC.port);
  1336. // authKey's are associated with a server, which has now changed
  1337. // so it's not valid anymore. Set to undefined to force recreating it.
  1338. await this._sender!.authKey.setKey(undefined);
  1339. this.session.setAuthKey(undefined);
  1340. this._reconnecting = true;
  1341. await this.disconnect();
  1342. return this.connect();
  1343. }
  1344. /**
  1345. * Returns the DC ip in case of node or the DC web address in case of browser.<br/>
  1346. * This will do an API request to fill the cache if it's the first time it's called.
  1347. * @param dcId The DC ID.
  1348. * @param downloadDC whether to use -1 DCs or not
  1349. * @param web if true this will get the web DCs.
  1350. * TODO, hardcode IPs.
  1351. * (These only support downloading/uploading and not creating a new AUTH key)
  1352. */
  1353. async getDC(
  1354. dcId: number,
  1355. downloadDC = false,
  1356. web = false
  1357. ): Promise<{ id: number; ipAddress: string; port: number }> {
  1358. this._log.debug(`Getting DC ${dcId}`);
  1359. if (!isNode || web) {
  1360. switch (dcId) {
  1361. case 1:
  1362. return {
  1363. id: 1,
  1364. ipAddress: `pluto${
  1365. downloadDC ? "-1" : ""
  1366. }.web.telegram.org`,
  1367. port: 443,
  1368. };
  1369. case 2:
  1370. return {
  1371. id: 2,
  1372. ipAddress: `venus${
  1373. downloadDC ? "-1" : ""
  1374. }.web.telegram.org`,
  1375. port: 443,
  1376. };
  1377. case 3:
  1378. return {
  1379. id: 3,
  1380. ipAddress: `aurora${
  1381. downloadDC ? "-1" : ""
  1382. }.web.telegram.org`,
  1383. port: 443,
  1384. };
  1385. case 4:
  1386. return {
  1387. id: 4,
  1388. ipAddress: `vesta${
  1389. downloadDC ? "-1" : ""
  1390. }.web.telegram.org`,
  1391. port: 443,
  1392. };
  1393. case 5:
  1394. return {
  1395. id: 5,
  1396. ipAddress: `flora${
  1397. downloadDC ? "-1" : ""
  1398. }.web.telegram.org`,
  1399. port: 443,
  1400. };
  1401. default:
  1402. throw new Error(
  1403. `Cannot find the DC with the ID of ${dcId}`
  1404. );
  1405. }
  1406. }
  1407. if (!this._config) {
  1408. this._config = await this.invoke(new Api.help.GetConfig());
  1409. }
  1410. for (const DC of this._config.dcOptions) {
  1411. if (DC.id === dcId && !!DC.ipv6 === this._useIPV6) {
  1412. return {
  1413. id: DC.id,
  1414. ipAddress: DC.ipAddress,
  1415. port: 443,
  1416. };
  1417. }
  1418. }
  1419. throw new Error(`Cannot find the DC with the ID of ${dcId}`);
  1420. }
  1421. /** @hidden */
  1422. _removeSender(dcId: number) {
  1423. delete this._borrowedSenderPromises[dcId];
  1424. }
  1425. /** @hidden */
  1426. _getResponseMessage(req: any, result: any, inputChat: any) {
  1427. return parseMethods._getResponseMessage(this, req, result, inputChat);
  1428. }
  1429. /** @hidden */
  1430. [inspect.custom]() {
  1431. return betterConsoleLog(this);
  1432. }
  1433. /**
  1434. * Small hack for using it in browsers
  1435. */
  1436. static get events() {
  1437. return require("../events");
  1438. }
  1439. // endregion
  1440. }