-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
310 lines (290 loc) · 12.2 KB
/
index.html
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<!DOCTYPE html>
<!-- saved from url=(0051)https://webappcontent.telegram.org/demo?mode=simple -->
<html class="light" style="--tg-viewport-height:100vh; --tg-viewport-stable-height:100vh;">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="MobileOptimized" content="176">
<meta name="HandheldFriendly" content="True">
<meta name="robots" content="noindex,nofollow">
<script src="https://telegram.org/js/telegram-web-app.js"></script>
<link rel="stylesheet" href="css/styles.css">
<script>
function setThemeClass() {
document.documentElement.className = Telegram.WebApp.colorScheme;
}
Telegram.WebApp.onEvent('themeChanged', setThemeClass);
setThemeClass();
</script>
</head>
<body style="">
<section>
<textarea id="text_field" rows="4"></textarea>
<button id="data_btn" onclick="sendText(true);">Send text to bot (x10)</button>
<button onclick="webviewExpand();">Expand Webview</button>
<button onclick="sendHello();">Send Hello</button>
<button onclick="sendDataToSalebot();">Get test msg</button>
<button id="data_btn" onclick="sendTime(true);">Send current time to bot (x10)</button>
<button onclick="toggleMainButton(this);">Hide Main Button</button>
<input type="file">
<div id="btn_status" class="hint" style="display: none;">
</div>
<p>Test links:</p>
<ul>
<!-- <li><a id="regular_link" href="https://webappcontent.telegram.org/demo?nextpage=1">Regular link #1</a> (opens inside webview)</li> -->
<li><a id="regular_link"
href="https://sadji0.github.io/test_tg_web_app_bot/DurgerKingBot.html?mode=menu">DurgerKingBot</a> (opens inside webview)</li>
<li><a href="https://telegram.org/" target="_blank">target="_blank" link</a> (opens outside webview)</li>
<li><a href="javascript:window.open('https://telegram.org/');">window.open() link</a> (opens outside
webview)</li>
<li><a href="https://t.me/like">LikeBot t.me link</a> (opens inside Telegram app)</li>
<li><a href="tg://resolve?domain=vote">VoteBot tg:// link</a> (does not open)</li>
</ul>
<p>Test permissions:</p>
<ul>
<li><a href="javascript:;" onclick="return requestLocation(this);">Request Location</a> <span></span></li>
<li><a href="javascript:;" onclick="return requestVideo(this);">Request Video</a> <span></span></li>
<li><a href="javascript:;" onclick="return requestAudio(this);">Request Audio</a> <span></span></li>
</ul>
<pre><code id="webview_data">{}</code></pre>
<div class="hint">
Data passed to webview.
<span id="webview_data_status" style="display: none;">Checking hash...</span>
</div>
<pre><code id="theme_data">{}</code></pre>
<div class="hint">
Theme params
</div>
</section>
<div class="viewport_border" text="1536 x 711"></div>
<div class="viewport_stable_border" text="1536 x 711 | is_expanded: true"></div>
<script src="js/jquery.min.js"></script>
<script>
Telegram.WebApp.ready();
var initData = Telegram.WebApp.initData || '';
var initDataUnsafe = Telegram.WebApp.initDataUnsafe || {};
/* Данная функция создаёт кроссбраузерный объект XMLHTTP */
function getXmlHttp() {
var xmlhttp;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
function sendDataToSalebot() {
var xmlhttp = getXmlHttp(); // Создаём объект XMLHTTP
xmlhttp.open('POST', 'https://chatter.salebot.pro/api/cab68744c159b80b214509ae5c79133b/tg_callback', true); // Открываем асинхронное соединение
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); // Отправляем кодировку
// xmlhttp.send("a=" + encodeURIComponent(a) + "&b=" + encodeURIComponent(b)); // Отправляем POST-запрос
xmlhttp.send("group_id=MihailRequestsInfoBot&user_id=663044964&message=test&my_callback_action=send_message");
xmlhttp.onreadystatechange = function() { // Ждём ответа от сервера
if (xmlhttp.readyState == 4) { // Ответ пришёл
if(xmlhttp.status == 200) { // Сервер вернул код 200 (что хорошо)
document.getElementById("summa").innerHTML = xmlhttp.responseText; // Выводим ответ сервера
}
}
};
}
function sendMessage(msg_id, with_webview) {
if (!initDataUnsafe.query_id) {
alert('WebViewQueryId not defined');
return;
}
$('button').prop('disabled', true);
$('#btn_status').text('Sending...').removeClass('ok err').show();
$.ajax('/demo/sendMessage', {
type: 'POST',
data: { _auth: initData, msg_id: msg_id || '', with_webview: !initDataUnsafe.receiver && with_webview ? 1 : 0 },
dataType: 'json',
success: function (result) {
$('button').prop('disabled', false);
if (result.response) {
if (result.response.ok) {
$('#btn_status').html('Message sent successfully!').addClass('ok').show();
} else {
$('#btn_status').text(result.response.description).addClass('err').show();
alert(result.response.description);
}
} else {
$('#btn_status').text('Unknown error').addClass('err').show();
alert('Unknown error');
}
},
error: function (xhr) {
$('button').prop('disabled', false);
$('#btn_status').text('Server error').addClass('err').show();
alert('Server error');
}
});
}
function changeButton() {
$('button').prop('disabled', true);
$('#btn_status').text('Changing button...').removeClass('ok err').show();
$.ajax('/demo/changeMenuButton', {
type: 'POST',
data: { _auth: initData },
dataType: 'json',
success: function (result) {
$('button').prop('disabled', false);
if (result.response) {
if (result.response.ok) {
$('#btn_status').html('Button changed!').addClass('ok').show();
Telegram.WebApp.close();
} else {
$('#btn_status').text(result.response.description).addClass('err').show();
alert(result.response.description);
}
} else {
$('#btn_status').text('Unknown error').addClass('err').show();
alert('Unknown error');
}
},
error: function (xhr) {
$('button').prop('disabled', false);
$('#btn_status').text('Server error').addClass('err').show();
alert('Server error');
}
});
setTimeout(function () {
Telegram.WebApp.close();
}, 50);
}
function byteLength(str) {
if (window.Blob) {
try { return new Blob([str]).size; } catch (e) { }
}
var s = str.length;
for (var i = str.length - 1; i >= 0; i--) {
var code = str.charCodeAt(i);
if (code > 0x7f && code <= 0x7ff) s++;
else if (code > 0x7ff && code <= 0xffff) s += 2;
if (code >= 0xDC00 && code <= 0xDFFF) i--;
}
return s;
}
function sendText(spam) {
var text = $('#text_field').val();
if (!text.length) {
return $('#text_field').focus();
}
if (byteLength(text) > 4096) {
return alert('Text is too long');
}
var repeat = spam ? 10 : 1;
for (var i = 0; i < repeat; i++) {
Telegram.WebApp.sendData(text);
}
}
function sendTime(spam) {
var repeat = spam ? 10 : 1;
for (var i = 0; i < repeat; i++) {
Telegram.WebApp.sendData(new Date().toString());
}
}
function sendHello() {
Telegram.WebApp.sendData("Hello_World");
}
function webviewExpand() {
Telegram.WebApp.expand();
}
function webviewClose() {
Telegram.WebApp.close();
}
function requestLocation(el) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
$(el).next('span').html('(' + position.coords.latitude + ', ' + position.coords.longitude + ')').attr('class', 'ok');
});
} else {
$(el).next('span').html('Geolocation is not supported in this browser.').attr('class', 'err');
}
return false;
}
function requestVideo(el) {
if (navigator.mediaDevices) {
navigator.mediaDevices.getUserMedia({ audio: false, video: true }).then(function (stream) {
$(el).next('span').html('(Access granted)').attr('class', 'ok');
});
} else {
$(el).next('span').html('Media devices is not supported in this browser.').attr('class', 'err');
}
return false;
}
function requestAudio(el) {
if (navigator.mediaDevices) {
navigator.mediaDevices.getUserMedia({ audio: true, video: false }).then(function (stream) {
$(el).next('span').html('(Access granted)').attr('class', 'ok');
});
} else {
$(el).next('span').html('Media devices is not supported in this browser.').attr('class', 'err');
}
return false;
}
Telegram.WebApp.onEvent('themeChanged', function () {
$('#theme_data').html(JSON.stringify(Telegram.WebApp.themeParams, null, 2));
});
$('#main_btn').toggle(!!initDataUnsafe.query_id);
$('#with_webview_btn').toggle(!!initDataUnsafe.query_id && !initDataUnsafe.receiver);
// $('#data_btn').toggle(!initDataUnsafe.query_id || !initDataUnsafe.receiver);
$('#webview_data').html(JSON.stringify(initDataUnsafe, null, 2));
$('#theme_data').html(JSON.stringify(Telegram.WebApp.themeParams, null, 2));
$('#regular_link').attr('href', $('#regular_link').attr('href') + location.hash);
$('#text_field').focus();
if (initDataUnsafe.query_id && initData) {
$('#webview_data_status').show();
$.ajax('/demo/checkData', {
type: 'POST',
data: { _auth: initData },
dataType: 'json',
success: function (result) {
if (result.ok) {
$('#webview_data_status').html('Hash is correct').addClass('ok');
} else {
$('#webview_data_status').html(result.error).addClass('err');
}
},
error: function (xhr) {
$('#webview_data_status').html('Server error').addClass('err');
}
});
}
$('body').css('visibility', '');
Telegram.WebApp.MainButton
.setText('CLOSE WEBVIEW')
.show()
.onClick(function () { webviewClose(); });
function toggleMainButton(el) {
var mainButton = Telegram.WebApp.MainButton;
if (mainButton.isVisible) {
mainButton.hide();
el.innerHTML = 'Show Main Button';
} else {
mainButton.show();
el.innerHTML = 'Hide Main Button';
}
}
function round(val, d) {
var k = Math.pow(10, d || 0);
return Math.round(val * k) / k;
}
function setViewportData() {
$('.viewport_border').attr('text', window.innerWidth + ' x ' + round(Telegram.WebApp.viewportHeight, 2));
$('.viewport_stable_border').attr('text', window.innerWidth + ' x ' + round(Telegram.WebApp.viewportStableHeight, 2) + ' | is_expanded: ' + (Telegram.WebApp.isExpanded ? 'true' : 'false'));
}
Telegram.WebApp.onEvent('viewportChanged', setViewportData);
setViewportData();
</script>
</body>
</html>