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