@ebowwa/mcp-telegram 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/auth-signin.js +182 -0
- package/auth-signin.ts +122 -0
- package/auth-verify.js +234 -0
- package/auth-verify.ts +180 -0
- package/auth.js +154 -0
- package/auth.ts +98 -0
- package/bun.lock +302 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +807 -0
- package/dist/index.js.map +1 -0
- package/package.json +52 -0
- package/src/index.js +918 -0
- package/src/index.ts +907 -0
- package/tg.sh +44 -0
- package/tsconfig.json +20 -0
package/auth-signin.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
"use strict";
|
|
3
|
+
/**
|
|
4
|
+
* Telegram Authentication - One-shot sign in
|
|
5
|
+
*
|
|
6
|
+
* Run: doppler run --project seed --config prd -- bun run auth-signin.ts
|
|
7
|
+
*
|
|
8
|
+
* Enter phone ā receive code ā enter code ā done (all in one session)
|
|
9
|
+
*/
|
|
10
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
11
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
12
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
14
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
15
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
16
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
20
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
21
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
22
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
23
|
+
function step(op) {
|
|
24
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
25
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
26
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
27
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
28
|
+
switch (op[0]) {
|
|
29
|
+
case 0: case 1: t = op; break;
|
|
30
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
31
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
32
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
33
|
+
default:
|
|
34
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
35
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
36
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
37
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
38
|
+
if (t[2]) _.ops.pop();
|
|
39
|
+
_.trys.pop(); continue;
|
|
40
|
+
}
|
|
41
|
+
op = body.call(thisArg, _);
|
|
42
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
43
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
47
|
+
var telegram_1 = require("telegram");
|
|
48
|
+
var sessions_1 = require("telegram/sessions");
|
|
49
|
+
var os_1 = require("os");
|
|
50
|
+
var path_1 = require("path");
|
|
51
|
+
var fs_1 = require("fs");
|
|
52
|
+
var readline = require("readline");
|
|
53
|
+
var SESSION_DIR = (0, path_1.join)((0, os_1.homedir)(), ".telegram-mcp");
|
|
54
|
+
var SESSION_FILE = (0, path_1.join)(SESSION_DIR, "session.txt");
|
|
55
|
+
if (!(0, fs_1.existsSync)(SESSION_DIR)) {
|
|
56
|
+
(0, fs_1.mkdirSync)(SESSION_DIR, { recursive: true });
|
|
57
|
+
}
|
|
58
|
+
var rl = readline.createInterface({
|
|
59
|
+
input: process.stdin,
|
|
60
|
+
output: process.stdout,
|
|
61
|
+
});
|
|
62
|
+
function question(prompt) {
|
|
63
|
+
return new Promise(function (resolve) { return rl.question(prompt, resolve); });
|
|
64
|
+
}
|
|
65
|
+
function main() {
|
|
66
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
67
|
+
var apiId, apiHash, existing, session_1, client_1, me, phoneNumber, session, client, sendResult, phoneCodeHash, code, signInResult, sessionString, me, e_1, password, pwdInfo, _a, _b, _c, _d, sessionString, me;
|
|
68
|
+
var _e;
|
|
69
|
+
var _f;
|
|
70
|
+
return __generator(this, function (_g) {
|
|
71
|
+
switch (_g.label) {
|
|
72
|
+
case 0:
|
|
73
|
+
apiId = parseInt(process.env.TELEGRAM_API_ID || "0");
|
|
74
|
+
apiHash = process.env.TELEGRAM_API_HASH;
|
|
75
|
+
if (!apiId || !apiHash) {
|
|
76
|
+
console.error("ERROR: TELEGRAM_API_ID and TELEGRAM_API_HASH required");
|
|
77
|
+
process.exit(1);
|
|
78
|
+
}
|
|
79
|
+
console.log("\nš± Telegram Authentication\n");
|
|
80
|
+
if (!(0, fs_1.existsSync)(SESSION_FILE)) return [3 /*break*/, 7];
|
|
81
|
+
existing = (0, fs_1.readFileSync)(SESSION_FILE, "utf-8").trim();
|
|
82
|
+
if (!existing) return [3 /*break*/, 7];
|
|
83
|
+
console.log("Testing existing session...");
|
|
84
|
+
session_1 = new sessions_1.StringSession(existing);
|
|
85
|
+
client_1 = new telegram_1.TelegramClient(session_1, apiId, apiHash, { connectionRetries: 5 });
|
|
86
|
+
return [4 /*yield*/, client_1.connect()];
|
|
87
|
+
case 1:
|
|
88
|
+
_g.sent();
|
|
89
|
+
return [4 /*yield*/, client_1.checkAuthorization()];
|
|
90
|
+
case 2:
|
|
91
|
+
if (!_g.sent()) return [3 /*break*/, 5];
|
|
92
|
+
return [4 /*yield*/, client_1.getMe()];
|
|
93
|
+
case 3:
|
|
94
|
+
me = _g.sent();
|
|
95
|
+
console.log("\n\u2705 Already logged in as: ".concat(me.firstName, " (@").concat(me.username || "N/A", ")"));
|
|
96
|
+
return [4 /*yield*/, client_1.disconnect()];
|
|
97
|
+
case 4:
|
|
98
|
+
_g.sent();
|
|
99
|
+
rl.close();
|
|
100
|
+
process.exit(0);
|
|
101
|
+
_g.label = 5;
|
|
102
|
+
case 5: return [4 /*yield*/, client_1.disconnect()];
|
|
103
|
+
case 6:
|
|
104
|
+
_g.sent();
|
|
105
|
+
_g.label = 7;
|
|
106
|
+
case 7: return [4 /*yield*/, question("Phone number (with country code): ")];
|
|
107
|
+
case 8:
|
|
108
|
+
phoneNumber = _g.sent();
|
|
109
|
+
session = new sessions_1.StringSession("");
|
|
110
|
+
client = new telegram_1.TelegramClient(session, apiId, apiHash, { connectionRetries: 5 });
|
|
111
|
+
return [4 /*yield*/, client.connect()];
|
|
112
|
+
case 9:
|
|
113
|
+
_g.sent();
|
|
114
|
+
console.log("\nSending verification code...");
|
|
115
|
+
return [4 /*yield*/, client.invoke(new telegram_1.Api.auth.SendCode({
|
|
116
|
+
phoneNumber: phoneNumber,
|
|
117
|
+
apiId: apiId,
|
|
118
|
+
apiHash: apiHash,
|
|
119
|
+
settings: new telegram_1.Api.CodeSettings({}),
|
|
120
|
+
}))];
|
|
121
|
+
case 10:
|
|
122
|
+
sendResult = _g.sent();
|
|
123
|
+
phoneCodeHash = sendResult.phoneCodeHash;
|
|
124
|
+
return [4 /*yield*/, question("Enter verification code: ")];
|
|
125
|
+
case 11:
|
|
126
|
+
code = _g.sent();
|
|
127
|
+
_g.label = 12;
|
|
128
|
+
case 12:
|
|
129
|
+
_g.trys.push([12, 15, , 23]);
|
|
130
|
+
return [4 /*yield*/, client.invoke(new telegram_1.Api.auth.SignIn({
|
|
131
|
+
phoneNumber: phoneNumber,
|
|
132
|
+
phoneCodeHash: phoneCodeHash,
|
|
133
|
+
phoneCode: code,
|
|
134
|
+
}))];
|
|
135
|
+
case 13:
|
|
136
|
+
signInResult = _g.sent();
|
|
137
|
+
sessionString = client.session.save();
|
|
138
|
+
(0, fs_1.writeFileSync)(SESSION_FILE, sessionString, "utf-8");
|
|
139
|
+
return [4 /*yield*/, client.getMe()];
|
|
140
|
+
case 14:
|
|
141
|
+
me = _g.sent();
|
|
142
|
+
console.log("\n\u2705 Logged in as: ".concat(me.firstName, " (@").concat(me.username || "N/A", ")"));
|
|
143
|
+
console.log("\uD83D\uDCC1 Session saved to: ".concat(SESSION_FILE));
|
|
144
|
+
return [3 /*break*/, 23];
|
|
145
|
+
case 15:
|
|
146
|
+
e_1 = _g.sent();
|
|
147
|
+
if (!((_f = e_1.message) === null || _f === void 0 ? void 0 : _f.includes("SESSION_PASSWORD_NEEDED"))) return [3 /*break*/, 21];
|
|
148
|
+
return [4 /*yield*/, question("2FA password: ")];
|
|
149
|
+
case 16:
|
|
150
|
+
password = _g.sent();
|
|
151
|
+
return [4 /*yield*/, client.invoke(new telegram_1.Api.account.GetPassword({}))];
|
|
152
|
+
case 17:
|
|
153
|
+
pwdInfo = _g.sent();
|
|
154
|
+
_b = (_a = client).invoke;
|
|
155
|
+
_d = (_c = telegram_1.Api.auth.CheckPassword).bind;
|
|
156
|
+
_e = {};
|
|
157
|
+
return [4 /*yield*/, client.computeCheck(pwdInfo, password)];
|
|
158
|
+
case 18: return [4 /*yield*/, _b.apply(_a, [new (_d.apply(_c, [void 0, (_e.password = _g.sent(),
|
|
159
|
+
_e)]))()])];
|
|
160
|
+
case 19:
|
|
161
|
+
_g.sent();
|
|
162
|
+
sessionString = client.session.save();
|
|
163
|
+
(0, fs_1.writeFileSync)(SESSION_FILE, sessionString, "utf-8");
|
|
164
|
+
return [4 /*yield*/, client.getMe()];
|
|
165
|
+
case 20:
|
|
166
|
+
me = _g.sent();
|
|
167
|
+
console.log("\n\u2705 Logged in as: ".concat(me.firstName, " (@").concat(me.username || "N/A", ")"));
|
|
168
|
+
return [3 /*break*/, 22];
|
|
169
|
+
case 21:
|
|
170
|
+
console.error("\nā Error:", e_1.message);
|
|
171
|
+
_g.label = 22;
|
|
172
|
+
case 22: return [3 /*break*/, 23];
|
|
173
|
+
case 23: return [4 /*yield*/, client.disconnect()];
|
|
174
|
+
case 24:
|
|
175
|
+
_g.sent();
|
|
176
|
+
rl.close();
|
|
177
|
+
return [2 /*return*/];
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
main().catch(console.error);
|
package/auth-signin.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
/**
|
|
3
|
+
* Telegram Authentication - One-shot sign in
|
|
4
|
+
*
|
|
5
|
+
* Run: doppler run --project seed --config prd -- bun run auth-signin.ts
|
|
6
|
+
*
|
|
7
|
+
* Enter phone ā receive code ā enter code ā done (all in one session)
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { TelegramClient, Api } from "telegram";
|
|
11
|
+
import { StringSession } from "telegram/sessions";
|
|
12
|
+
import { homedir } from "os";
|
|
13
|
+
import { join } from "path";
|
|
14
|
+
import { mkdirSync, existsSync, writeFileSync, readFileSync } from "fs";
|
|
15
|
+
import * as readline from "readline";
|
|
16
|
+
|
|
17
|
+
const SESSION_DIR = join(homedir(), ".telegram-mcp");
|
|
18
|
+
const SESSION_FILE = join(SESSION_DIR, "session.txt");
|
|
19
|
+
|
|
20
|
+
if (!existsSync(SESSION_DIR)) {
|
|
21
|
+
mkdirSync(SESSION_DIR, { recursive: true });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const rl = readline.createInterface({
|
|
25
|
+
input: process.stdin,
|
|
26
|
+
output: process.stdout,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
function question(prompt: string): Promise<string> {
|
|
30
|
+
return new Promise((resolve) => rl.question(prompt, resolve));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function main() {
|
|
34
|
+
const apiId = parseInt(process.env.TELEGRAM_API_ID || "0");
|
|
35
|
+
const apiHash = process.env.TELEGRAM_API_HASH;
|
|
36
|
+
|
|
37
|
+
if (!apiId || !apiHash) {
|
|
38
|
+
console.error("ERROR: TELEGRAM_API_ID and TELEGRAM_API_HASH required");
|
|
39
|
+
process.exit(1);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
console.log("\nš± Telegram Authentication\n");
|
|
43
|
+
|
|
44
|
+
// Check existing session
|
|
45
|
+
if (existsSync(SESSION_FILE)) {
|
|
46
|
+
const existing = readFileSync(SESSION_FILE, "utf-8").trim();
|
|
47
|
+
if (existing) {
|
|
48
|
+
console.log("Testing existing session...");
|
|
49
|
+
const session = new StringSession(existing);
|
|
50
|
+
const client = new TelegramClient(session, apiId, apiHash, { connectionRetries: 5 });
|
|
51
|
+
await client.connect();
|
|
52
|
+
if (await client.checkAuthorization()) {
|
|
53
|
+
const me = await client.getMe();
|
|
54
|
+
console.log(`\nā
Already logged in as: ${me.firstName} (@${me.username || "N/A"})`);
|
|
55
|
+
await client.disconnect();
|
|
56
|
+
rl.close();
|
|
57
|
+
process.exit(0);
|
|
58
|
+
}
|
|
59
|
+
await client.disconnect();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const phoneNumber = await question("Phone number (with country code): ");
|
|
64
|
+
|
|
65
|
+
// Connect and send code
|
|
66
|
+
const session = new StringSession("");
|
|
67
|
+
const client = new TelegramClient(session, apiId, apiHash, { connectionRetries: 5 });
|
|
68
|
+
await client.connect();
|
|
69
|
+
|
|
70
|
+
console.log("\nSending verification code...");
|
|
71
|
+
const sendResult = await client.invoke(
|
|
72
|
+
new Api.auth.SendCode({
|
|
73
|
+
phoneNumber,
|
|
74
|
+
apiId,
|
|
75
|
+
apiHash,
|
|
76
|
+
settings: new Api.CodeSettings({}),
|
|
77
|
+
})
|
|
78
|
+
);
|
|
79
|
+
const phoneCodeHash = (sendResult as any).phoneCodeHash;
|
|
80
|
+
|
|
81
|
+
const code = await question("Enter verification code: ");
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
const signInResult = await client.invoke(
|
|
85
|
+
new Api.auth.SignIn({
|
|
86
|
+
phoneNumber,
|
|
87
|
+
phoneCodeHash,
|
|
88
|
+
phoneCode: code,
|
|
89
|
+
})
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
// Success!
|
|
93
|
+
const sessionString = client.session.save() as unknown as string;
|
|
94
|
+
writeFileSync(SESSION_FILE, sessionString, "utf-8");
|
|
95
|
+
|
|
96
|
+
const me = await client.getMe();
|
|
97
|
+
console.log(`\nā
Logged in as: ${me.firstName} (@${me.username || "N/A"})`);
|
|
98
|
+
console.log(`š Session saved to: ${SESSION_FILE}`);
|
|
99
|
+
|
|
100
|
+
} catch (e: any) {
|
|
101
|
+
if (e.message?.includes("SESSION_PASSWORD_NEEDED")) {
|
|
102
|
+
const password = await question("2FA password: ");
|
|
103
|
+
const pwdInfo = await client.invoke(new Api.account.GetPassword({}));
|
|
104
|
+
await client.invoke(
|
|
105
|
+
new Api.auth.CheckPassword({
|
|
106
|
+
password: await client.computeCheck(pwdInfo as any, password),
|
|
107
|
+
})
|
|
108
|
+
);
|
|
109
|
+
const sessionString = client.session.save() as unknown as string;
|
|
110
|
+
writeFileSync(SESSION_FILE, sessionString, "utf-8");
|
|
111
|
+
const me = await client.getMe();
|
|
112
|
+
console.log(`\nā
Logged in as: ${me.firstName} (@${me.username || "N/A"})`);
|
|
113
|
+
} else {
|
|
114
|
+
console.error("\nā Error:", e.message);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
await client.disconnect();
|
|
119
|
+
rl.close();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
main().catch(console.error);
|
package/auth-verify.js
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
"use strict";
|
|
3
|
+
/**
|
|
4
|
+
* Telegram Authentication Script (Non-interactive)
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
* Step 1 - Request code:
|
|
8
|
+
* doppler run --project seed --config prd -- bun run auth-verify.ts +1234567890
|
|
9
|
+
*
|
|
10
|
+
* Step 2 - Verify with code:
|
|
11
|
+
* doppler run --project seed --config prd -- bun run auth-verify.ts +1234567890 12345
|
|
12
|
+
*
|
|
13
|
+
* Step 3 - With 2FA password:
|
|
14
|
+
* doppler run --project seed --config prd -- bun run auth-verify.ts +1234567890 12345 "mypassword"
|
|
15
|
+
*/
|
|
16
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
17
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
18
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
19
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
20
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
21
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
22
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
26
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
27
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
28
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
29
|
+
function step(op) {
|
|
30
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
31
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
32
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
33
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
34
|
+
switch (op[0]) {
|
|
35
|
+
case 0: case 1: t = op; break;
|
|
36
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
37
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
38
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
39
|
+
default:
|
|
40
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
41
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
42
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
43
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
44
|
+
if (t[2]) _.ops.pop();
|
|
45
|
+
_.trys.pop(); continue;
|
|
46
|
+
}
|
|
47
|
+
op = body.call(thisArg, _);
|
|
48
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
49
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
+
var telegram_1 = require("telegram");
|
|
54
|
+
var sessions_1 = require("telegram/sessions");
|
|
55
|
+
var os_1 = require("os");
|
|
56
|
+
var path_1 = require("path");
|
|
57
|
+
var fs_1 = require("fs");
|
|
58
|
+
var SESSION_DIR = (0, path_1.join)((0, os_1.homedir)(), ".telegram-mcp");
|
|
59
|
+
var SESSION_FILE = (0, path_1.join)(SESSION_DIR, "session.txt");
|
|
60
|
+
var PHONE_CODE_HASH_FILE = (0, path_1.join)(SESSION_DIR, "phone_code_hash.txt");
|
|
61
|
+
// Ensure session directory exists
|
|
62
|
+
if (!(0, fs_1.existsSync)(SESSION_DIR)) {
|
|
63
|
+
(0, fs_1.mkdirSync)(SESSION_DIR, { recursive: true });
|
|
64
|
+
}
|
|
65
|
+
function main() {
|
|
66
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
67
|
+
var apiId, apiHash, phoneNumber, code, password, existingSession, session_1, client_1, me, e_1, session, client, result, e_2, phoneCodeHash, result, e_3, passwordInfo, _a, _b, _c, _d, sessionString, me, e_4;
|
|
68
|
+
var _e;
|
|
69
|
+
var _f;
|
|
70
|
+
return __generator(this, function (_g) {
|
|
71
|
+
switch (_g.label) {
|
|
72
|
+
case 0:
|
|
73
|
+
apiId = parseInt(process.env.TELEGRAM_API_ID || "0");
|
|
74
|
+
apiHash = process.env.TELEGRAM_API_HASH;
|
|
75
|
+
if (!apiId || !apiHash) {
|
|
76
|
+
console.error("ERROR: TELEGRAM_API_ID and TELEGRAM_API_HASH must be set");
|
|
77
|
+
console.error("Run with: doppler run --project seed --config prd -- bun run auth-verify.ts ...");
|
|
78
|
+
process.exit(1);
|
|
79
|
+
}
|
|
80
|
+
phoneNumber = process.argv[2];
|
|
81
|
+
code = process.argv[3];
|
|
82
|
+
password = process.argv[4] || "";
|
|
83
|
+
if (!phoneNumber) {
|
|
84
|
+
console.error("Usage: bun run auth-verify.ts <phone> [code] [password]");
|
|
85
|
+
console.error(" phone: Phone number with country code (e.g., +1234567890)");
|
|
86
|
+
console.error(" code: Verification code (optional, for step 2)");
|
|
87
|
+
console.error(" password: 2FA password (optional)");
|
|
88
|
+
process.exit(1);
|
|
89
|
+
}
|
|
90
|
+
if (!(0, fs_1.existsSync)(SESSION_FILE)) return [3 /*break*/, 8];
|
|
91
|
+
existingSession = (0, fs_1.readFileSync)(SESSION_FILE, "utf-8").trim();
|
|
92
|
+
if (!(existingSession && !code)) return [3 /*break*/, 8];
|
|
93
|
+
console.log("Found existing session, testing connection...");
|
|
94
|
+
session_1 = new sessions_1.StringSession(existingSession);
|
|
95
|
+
client_1 = new telegram_1.TelegramClient(session_1, apiId, apiHash, {
|
|
96
|
+
connectionRetries: 5,
|
|
97
|
+
});
|
|
98
|
+
_g.label = 1;
|
|
99
|
+
case 1:
|
|
100
|
+
_g.trys.push([1, 7, , 8]);
|
|
101
|
+
return [4 /*yield*/, client_1.connect()];
|
|
102
|
+
case 2:
|
|
103
|
+
_g.sent();
|
|
104
|
+
return [4 /*yield*/, client_1.checkAuthorization()];
|
|
105
|
+
case 3:
|
|
106
|
+
if (!_g.sent()) return [3 /*break*/, 6];
|
|
107
|
+
return [4 /*yield*/, client_1.getMe()];
|
|
108
|
+
case 4:
|
|
109
|
+
me = _g.sent();
|
|
110
|
+
console.log("\nā
Already authenticated as:", me.firstName, me.lastName || "");
|
|
111
|
+
console.log("š Phone:", me.phone);
|
|
112
|
+
return [4 /*yield*/, client_1.disconnect()];
|
|
113
|
+
case 5:
|
|
114
|
+
_g.sent();
|
|
115
|
+
process.exit(0);
|
|
116
|
+
_g.label = 6;
|
|
117
|
+
case 6: return [3 /*break*/, 8];
|
|
118
|
+
case 7:
|
|
119
|
+
e_1 = _g.sent();
|
|
120
|
+
console.log("Existing session invalid, re-authenticating...");
|
|
121
|
+
return [3 /*break*/, 8];
|
|
122
|
+
case 8:
|
|
123
|
+
session = new sessions_1.StringSession("");
|
|
124
|
+
client = new telegram_1.TelegramClient(session, apiId, apiHash, {
|
|
125
|
+
connectionRetries: 5,
|
|
126
|
+
});
|
|
127
|
+
return [4 /*yield*/, client.connect()];
|
|
128
|
+
case 9:
|
|
129
|
+
_g.sent();
|
|
130
|
+
if (!!code) return [3 /*break*/, 14];
|
|
131
|
+
// Step 1: Send verification code
|
|
132
|
+
console.log("\n\uD83D\uDCF1 Sending verification code to ".concat(phoneNumber, "..."));
|
|
133
|
+
_g.label = 10;
|
|
134
|
+
case 10:
|
|
135
|
+
_g.trys.push([10, 12, , 13]);
|
|
136
|
+
return [4 /*yield*/, client.invoke(new telegram_1.Api.auth.SendCode({
|
|
137
|
+
phoneNumber: phoneNumber,
|
|
138
|
+
apiId: apiId,
|
|
139
|
+
apiHash: apiHash,
|
|
140
|
+
settings: new telegram_1.Api.CodeSettings({}),
|
|
141
|
+
}))];
|
|
142
|
+
case 11:
|
|
143
|
+
result = _g.sent();
|
|
144
|
+
// Save phone_code_hash for step 2
|
|
145
|
+
(0, fs_1.writeFileSync)(PHONE_CODE_HASH_FILE, result.phoneCodeHash, "utf-8");
|
|
146
|
+
console.log("\nā
Verification code sent!");
|
|
147
|
+
console.log("Check your Telegram app for the code.");
|
|
148
|
+
console.log("\nNext step: Run this command with the code:");
|
|
149
|
+
console.log(" doppler run --project seed --config prd -- bun run auth-verify.ts ".concat(phoneNumber, " <CODE>"));
|
|
150
|
+
return [3 /*break*/, 13];
|
|
151
|
+
case 12:
|
|
152
|
+
e_2 = _g.sent();
|
|
153
|
+
console.error("\nā Error sending code:", e_2.message);
|
|
154
|
+
return [3 /*break*/, 13];
|
|
155
|
+
case 13: return [3 /*break*/, 27];
|
|
156
|
+
case 14:
|
|
157
|
+
// Step 2: Sign in with code
|
|
158
|
+
console.log("\n\uD83D\uDD10 Signing in with code: ".concat(code, "..."));
|
|
159
|
+
phoneCodeHash = "";
|
|
160
|
+
if ((0, fs_1.existsSync)(PHONE_CODE_HASH_FILE)) {
|
|
161
|
+
phoneCodeHash = (0, fs_1.readFileSync)(PHONE_CODE_HASH_FILE, "utf-8").trim();
|
|
162
|
+
}
|
|
163
|
+
if (!phoneCodeHash) {
|
|
164
|
+
console.error("ā No phone_code_hash found. Run step 1 first (without code).");
|
|
165
|
+
process.exit(1);
|
|
166
|
+
}
|
|
167
|
+
_g.label = 15;
|
|
168
|
+
case 15:
|
|
169
|
+
_g.trys.push([15, 26, , 27]);
|
|
170
|
+
result = void 0;
|
|
171
|
+
_g.label = 16;
|
|
172
|
+
case 16:
|
|
173
|
+
_g.trys.push([16, 18, , 24]);
|
|
174
|
+
return [4 /*yield*/, client.invoke(new telegram_1.Api.auth.SignIn({
|
|
175
|
+
phoneNumber: phoneNumber,
|
|
176
|
+
phoneCodeHash: phoneCodeHash,
|
|
177
|
+
phoneCode: code,
|
|
178
|
+
}))];
|
|
179
|
+
case 17:
|
|
180
|
+
result = _g.sent();
|
|
181
|
+
return [3 /*break*/, 24];
|
|
182
|
+
case 18:
|
|
183
|
+
e_3 = _g.sent();
|
|
184
|
+
if (!(((_f = e_3.message) === null || _f === void 0 ? void 0 : _f.includes("SESSION_PASSWORD_NEEDED")) || password)) return [3 /*break*/, 22];
|
|
185
|
+
// 2FA required
|
|
186
|
+
if (!password) {
|
|
187
|
+
console.log("\nā ļø 2FA is enabled on this account.");
|
|
188
|
+
console.log("Please provide your 2FA password:");
|
|
189
|
+
console.log(" doppler run --project seed --config prd -- bun run auth-verify.ts ".concat(phoneNumber, " ").concat(code, " \"YOUR_PASSWORD\""));
|
|
190
|
+
process.exit(1);
|
|
191
|
+
}
|
|
192
|
+
return [4 /*yield*/, client.invoke(new telegram_1.Api.account.GetPassword({}))];
|
|
193
|
+
case 19:
|
|
194
|
+
passwordInfo = _g.sent();
|
|
195
|
+
_b = (_a = client).invoke;
|
|
196
|
+
_d = (_c = telegram_1.Api.auth.CheckPassword).bind;
|
|
197
|
+
_e = {};
|
|
198
|
+
return [4 /*yield*/, client.computeCheck(passwordInfo, password)];
|
|
199
|
+
case 20: return [4 /*yield*/, _b.apply(_a, [new (_d.apply(_c, [void 0, (_e.password = _g.sent(),
|
|
200
|
+
_e)]))()])];
|
|
201
|
+
case 21:
|
|
202
|
+
result = _g.sent();
|
|
203
|
+
return [3 /*break*/, 23];
|
|
204
|
+
case 22: throw e_3;
|
|
205
|
+
case 23: return [3 /*break*/, 24];
|
|
206
|
+
case 24:
|
|
207
|
+
sessionString = client.session.save();
|
|
208
|
+
(0, fs_1.writeFileSync)(SESSION_FILE, sessionString, "utf-8");
|
|
209
|
+
console.log("\nā
Successfully authenticated!");
|
|
210
|
+
console.log("\uD83D\uDCC1 Session saved to: ".concat(SESSION_FILE));
|
|
211
|
+
return [4 /*yield*/, client.getMe()];
|
|
212
|
+
case 25:
|
|
213
|
+
me = _g.sent();
|
|
214
|
+
console.log("\uD83D\uDC64 Logged in as: ".concat(me.firstName, " ").concat(me.lastName || ""));
|
|
215
|
+
console.log("\uD83D\uDCDE Phone: ".concat(me.phone));
|
|
216
|
+
console.log("\uD83D\uDC64 Username: @".concat(me.username || "N/A"));
|
|
217
|
+
return [3 /*break*/, 27];
|
|
218
|
+
case 26:
|
|
219
|
+
e_4 = _g.sent();
|
|
220
|
+
console.error("\nā Sign in error:", e_4.message);
|
|
221
|
+
return [3 /*break*/, 27];
|
|
222
|
+
case 27: return [4 /*yield*/, client.disconnect()];
|
|
223
|
+
case 28:
|
|
224
|
+
_g.sent();
|
|
225
|
+
process.exit(0);
|
|
226
|
+
return [2 /*return*/];
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
main().catch(function (err) {
|
|
232
|
+
console.error("Fatal error:", err);
|
|
233
|
+
process.exit(1);
|
|
234
|
+
});
|