-
Notifications
You must be signed in to change notification settings - Fork 2
/
telegram.js
153 lines (132 loc) · 3.33 KB
/
telegram.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
telegramApi.setConfig({
app: {
id: _ID_,
hash: '_HASH_',
version: telegramApi.VERSION
},
server: {
test: [
{
id: 1,
host: '1.2.3.4.5.6',
port: 443
}
],
production: [
{
id: 2, /* DC ID */
host: '7.8.9.10.11.12',
port: 443
}
]
}
});
var authCode
var hash
var responseData
function SendCode() {
telegramApi.sendCode(document.getElementById('number').value).then(function(sent_code) {
if (!sent_code.phone_registered) {
// New user
}
// phone_code_hash will need to sign in or sign up
window.phone_code_hash = sent_code.phone_code_hash
console.log(authCode)
console.log(window.phone_code_hash)
});
}
function SingIn() {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "http://localhost:8080/send/?code=" + document.getElementById('code').value + '&' + "hash=" + window.phone_code_hash + '&' + "number=" + '+' + document.getElementById('number').value, true);
xhttp.send();
//download(JSON.stringify(obj), 'conf.json', 'application/JSON')
//console.log('Signed In')
}
function LogIn() {
var number;
var hash;
var code;
RequestPhone('http://localhost:8080/getnumber', number);
Request('http://localhost:8080/gethash', hash);
Request('http://localhost:8080/getcode', code);
window.phone_code_hash = hash;
telegramApi.signIn(number, window.phone_code_hash, code).then(function() {
delete window.phone_code_hash;
getPhoto();
}, function(err) {
switch (err.type) {
case 'PHONE_CODE_INVALID':
// alert "Phone code invalid"
break;
case 'PHONE_NUMBER_UNOCCUPIED':
// User not registered, you should use signUp method
break;
}
});
}
function Request(url, param) {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", url, false);
xhttp.send();
param = xhttp.responseText;
console.log('Data: ' + param);
}
function RequestPhone(url, param) {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", url, false);
xhttp.send();
var temp = "+" + xhttp.responseText;
param = temp.replace(/\s/g,'');
console.log('Data: ' + param);
}
function getUser() {
telegramApi.getUserInfo().then(function(user) {
if (user.id) {
console.log('Usr ID: ' + user.id)
if(user.name) {
console.log('UserName: ' + user.name)
} else {
console.log('Wrong')
}
} else {
// Open log in page
}
});
}
function getPhoto() {
telegramApi.getUserPhoto('base64', 'small').then(function(base64) {
document.getElementById('avatar').src = base64
});
}
var obj = {
data: {
isLogged: null,
code: null,
number: null,
code_hash: null
}
}
/*
function download(data, filename, type) {
var file = new Blob([data], {type: type});
if (window.navigator.msSaveBlob) // IE10+
window.navigator.msSaveBlob(file, filename);
else { // Others
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}
*/
/*
function loadJSON() {
document.getElementById('lorem').value = JSON.parse(conf)[0].code
}
*/