-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.ts
More file actions
120 lines (93 loc) · 3.43 KB
/
Copy pathexample.ts
File metadata and controls
120 lines (93 loc) · 3.43 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import Debug from "debug"
import { equal, ok } from "node:assert/strict"
import { createInterface } from "node:readline/promises"
import { stdin as input, stdout as output } from 'node:process';
import { MTProto } from "./mtproto.js";
import { createTempStorage } from "./temp-storage.js";
import { getSRPParams } from "./get-srp-params.js";
const debug = Debug("example")
/**
* Can be taken on https://my.telegram.org/apps
*/
const api_id = Number(process.env.API_ID || 0)
const api_hash = String(process.env.API_HASH || "")
const api_test = Boolean(process.env.API_TEST)
ok(api_id, "`process.env.API_ID` is missing")
ok(api_hash, "`process.env.API_HASH` is missing")
/**
* The default phone number for sign in
*/
const default_phone_number = String(process.env.PHONE_NUMBER || "")
const storage = createTempStorage()
const readline = createInterface({ input, output })
const mtproto = new MTProto({
api_id,
api_hash,
storage,
test: api_test,
initConnectionParams: {
device_model: "Test Device",
}
})
const config = await mtproto.call("help.getConfig")
equal(config._, "config")
const countries = await mtproto.call("help.getCountriesList")
equal(countries._, "help.countriesList")
const enter_phone_number = await readline.question(`Enter phone number (default: ${default_phone_number}): `)
const phone_number = enter_phone_number || default_phone_number
debug("phone_number = %s", phone_number)
const sentCode = await mtproto.call("auth.sendCode", {
phone_number,
api_id,
api_hash,
settings: {
_: "codeSettings"
}
})
equal(sentCode._, "auth.sentCode")
const phone_code = await readline.question("Enter phone code: ")
const phone_code_hash = sentCode.phone_code_hash
const auth = await mtproto.call("auth.signIn", { phone_number, phone_code_hash, phone_code }).catch(async function (err) {
if (err.message === "SESSION_PASSWORD_NEEDED") {
const accountPassword = await mtproto.call("account.getPassword")
equal(accountPassword._, "account.password")
equal(accountPassword.current_algo?._, "passwordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow")
ok(accountPassword.current_algo.g, "`accountPassword.current_algo.g` is required")
ok(accountPassword.current_algo.p, "`accountPassword.current_algo.p` is required")
ok(accountPassword.current_algo.salt1, "`accountPassword.current_algo.salt1` is required")
ok(accountPassword.current_algo.salt2, "`accountPassword.current_algo.salt2` is required")
ok(accountPassword.srp_B, "`password.srp_B` is required")
ok(accountPassword.srp_id, "`password.srp_id` is required")
const password = await readline.question(`Enter password (hint ${accountPassword.hint}): `)
const srp = getSRPParams({
g: accountPassword.current_algo.g,
p: accountPassword.current_algo.p,
salt1: accountPassword.current_algo.salt1,
salt2: accountPassword.current_algo.salt2,
gB: accountPassword.srp_B,
password,
})
return mtproto.call("auth.checkPassword", {
password: {
_: "inputCheckPasswordSRP",
srp_id: accountPassword.srp_id,
A: srp.A,
M1: srp.M1,
}
})
}
throw err
})
debug("auth %j", auth)
const me = await mtproto.call("users.getFullUser", {
id: {
_: "inputUserSelf"
}
})
debug("me %j", me)
const state = await mtproto.call("updates.getState", {})
debug("state %j", state)
await mtproto.call("auth.logOut")
await mtproto.destroy()
await readline.close()
debug("finished")