forked from abhishekjnvk/telegram-channel-downloader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
72 lines (62 loc) · 2.39 KB
/
index.js
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
const fs = require("fs");
const readline = require("readline-sync");
const { TelegramClient } = require("telegram");
const { StringSession } = require("telegram/sessions");
const { selectDialog, getDialogName } = require("./modules/dialoges");
const { getMessages } = require("./modules/messages");
const { logMessage } = require("./utils/helper");
const { updateCredentials, getLastSelection, getCredentials } = require("./utils/file_helper");
// let { apiHash, apiId, sessionId } = getCredentials()
let { apiHash, apiId, sessionId } = {
"apiId": xxx,
"apiHash": "xxx",
"sessionId": ""
}
const stringSession = new StringSession(sessionId || "");
let { channelId } = getLastSelection()
var client = null;
const init = async () => {
if (!fs.existsSync("./export")) {
fs.mkdirSync("./export");
}
client = new TelegramClient(stringSession, apiId, apiHash, {
connectionRetries: 5,
});
try {
await client.start({
phoneNumber: async () => await readline.question("Please enter your number: "),
password: async () => await readline.question("Please enter your password: "),
phoneCode: async () =>
await readline.question("Please enter the code you received: "),
onError: (err) => logMessage.error(err),
});
logMessage.success("You should now be connected.");
if (!sessionId) {
sessionId = client.session.save();
updateCredentials({sessionId});
logMessage.info(`To avoid login again and again session id has been saved to config.json, please don't share it with anyone`);
}
return client;
}
catch (err) {
logMessage.error(err)
}
}
(async () => {
await init();
if (!channelId) {
channelId = await selectDialog(client)
} else {
logMessage.success(`Selected channel is: ${getDialogName(channelId)}`)
if (readline.keyInYN('Do you want to change channel?')) {
channelId = await selectDialog(client)
}
}
let downloadMedia = false;
if (readline.keyInYN('Do you want to download media?')) {
downloadMedia = true
}
await getMessages(client, channelId, downloadMedia);
await client.disconnect();
process.exit(0);
})()