|
| 1 | +import { Client } from "@mtkruto/mtkruto"; |
| 2 | +import { findApiCredentials } from "./find_api_credentials.ts"; |
| 3 | +import { isApiHashValid } from "./is_api_hash_valid.ts"; |
| 4 | + |
| 5 | +let apiId: number | null = null; |
| 6 | +let apiHash: string | null = null; |
| 7 | + |
| 8 | +const apiCredentials = findApiCredentials(); |
| 9 | +if (apiCredentials !== null) { |
| 10 | + console.log(`${apiCredentials.apiId.envVar}=${apiCredentials.apiId.value}`); |
| 11 | + console.log( |
| 12 | + `${apiCredentials.apiHash.envVar}=${apiCredentials.apiHash.value}`, |
| 13 | + ); |
| 14 | + console.log(); |
| 15 | + if ( |
| 16 | + !prompt( |
| 17 | + "The above credentials were found in your environment.\nDo you want to use them? [Y/n]", |
| 18 | + )?.toLowerCase().startsWith("n") |
| 19 | + ) { |
| 20 | + apiId = apiCredentials.apiId.value; |
| 21 | + apiHash = apiCredentials.apiHash.value; |
| 22 | + } else { |
| 23 | + console.log(); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +if (apiId === null || apiHash === null) { |
| 28 | + console.log( |
| 29 | + "API credentials can be obtained from <https://my.telegram.org/apps/>.", |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +while (apiId === null || apiId < 0) { |
| 34 | + const result = prompt("Enter API ID:"); |
| 35 | + if (result !== null) { |
| 36 | + apiId = parseInt(result); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +while (!apiHash || !isApiHashValid(apiHash)) { |
| 41 | + const result = prompt("Enter API hash:"); |
| 42 | + if (result !== null) { |
| 43 | + apiHash = result; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +const client = new Client({ |
| 48 | + apiId, |
| 49 | + apiHash, |
| 50 | +}); |
| 51 | + |
| 52 | +console.log(); |
| 53 | +try { |
| 54 | + await client.connect(); |
| 55 | + console.log("Connected to Telegram."); |
| 56 | +} catch (err) { |
| 57 | + console.log("Failed to connect to Telegram:", err); |
| 58 | +} |
| 59 | + |
| 60 | +try { |
| 61 | + await client.signIn(); |
| 62 | +} catch (err) { |
| 63 | + console.log(); |
| 64 | + console.log("Failed to sign in:", err); |
| 65 | +} |
| 66 | + |
| 67 | +const authString = await client.exportAuthString(); |
| 68 | + |
| 69 | +console.log(); |
| 70 | +console.log("The auth string for the current session is:"); |
| 71 | +console.log(authString); |
| 72 | +await client.disconnect(); |
0 commit comments