-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateSessionString.ts
45 lines (37 loc) · 1.36 KB
/
generateSessionString.ts
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
import { TelegramClient } from "telegram";
import { StringSession } from "telegram/sessions";
import * as readline from "readline";
import * as dotenv from "dotenv";
dotenv.config();
const API_ID = parseInt(process.env.API_ID || "0");
const API_HASH = process.env.API_HASH || "";
const prompt = (question: string): Promise<string> =>
new Promise((resolve) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(question, (answer) => {
rl.close();
resolve(answer.trim());
});
});
async function generateSessionString() {
console.log("Generating a new session string...");
const client = new TelegramClient(new StringSession(""), API_ID, API_HASH, {
connectionRetries: 5,
});
await client.start({
phoneNumber: async () => await prompt("Enter your phone number: "),
password: async () => await prompt("Enter your 2FA password (if enabled): "),
phoneCode: async () => await prompt("Enter the code sent to your Telegram: "),
onError: (err) => console.error(err),
});
console.log("Logged in successfully!");
const sessionString = client.session.save();
console.log("Your session string:");
console.log(sessionString);
await client.disconnect();
console.log("Save this session string in your .env file as SESSION_STRING.");
}
generateSessionString();