Skip to content

Commit 72dcea8

Browse files
committed
Initial commit
0 parents  commit 72dcea8

7 files changed

Lines changed: 249 additions & 0 deletions

File tree

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Roj
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# @mtkruto/auth-string
2+
3+
Generate an [MTKruto auth string](https://mtkru.to/auth-strings/) just by running:
4+
5+
```shell
6+
deno run -A jsr:@mtkruto/auth-string
7+
```

deno.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "@mtkruto/auth-string",
3+
"version": "0.0.0",
4+
"tasks": {
5+
"dev": "deno run --watch main.ts"
6+
},
7+
"exports": {
8+
".": "./main.ts"
9+
},
10+
"imports": {
11+
"@mtkruto/mtkruto": "jsr:@mtkruto/mtkruto@^0.75.1"
12+
}
13+
}

deno.lock

Lines changed: 92 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

find_api_credentials.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { isApiHashValid } from "./is_api_hash_valid.ts";
2+
3+
export function findApiCredentials() {
4+
let apiIdKey = TELEGRAM_API_ID;
5+
let apiId = Deno.env.get(apiIdKey);
6+
if (apiId === undefined) {
7+
apiIdKey = API_ID;
8+
apiId = Deno.env.get(API_ID);
9+
}
10+
11+
let apiHashKey = TELEGRAM_API_HASH;
12+
let apiHash = Deno.env.get(apiHashKey);
13+
if (apiHash === undefined) {
14+
apiHashKey = API_HASH;
15+
apiHash = Deno.env.get(API_HASH);
16+
}
17+
18+
const apiIdNumber = apiId === undefined ? 0 : parseInt(apiId);
19+
if (
20+
!isNaN(apiIdNumber) && apiIdNumber > 0 && apiHash && isApiHashValid(apiHash)
21+
) {
22+
return {
23+
apiId: {
24+
envVar: apiIdKey,
25+
value: apiIdNumber,
26+
},
27+
apiHash: {
28+
envVar: apiHashKey,
29+
value: apiHash,
30+
},
31+
};
32+
} else {
33+
return null;
34+
}
35+
}
36+
37+
const API_ID = "API_ID";
38+
const TELEGRAM_API_ID = "TELEGRAM_API_ID";
39+
40+
const API_HASH = "API_HASH";
41+
const TELEGRAM_API_HASH = "TELEGRAM_API_HASH";

is_api_hash_valid.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function isApiHashValid(apiHash: string) {
2+
return /^[a-z0-f]{32,32}$/.test(apiHash);
3+
}

main.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)