var DemoApp = { initData: Telegram.WebApp.initData || '', initDataUnsafe: Telegram.WebApp.initDataUnsafe || {}, MainButton: Telegram.WebApp.MainButton, BackButton: Telegram.WebApp.BackButton, init: function(options) { $('body').css('visibility', ''); Telegram.WebApp.ready(); Telegram.WebApp.MainButton.setParams({ text: 'CLOSE WEBVIEW', is_visible: true }).onClick(DemoApp.close); Telegram.WebApp.BackButton.onClick(function() { DemoApp.showAlert('Back button pressed'); }); Telegram.WebApp.onEvent('settingsButtonClicked', function() { DemoApp.showAlert('Settings opened!'); }); }, expand: function() { Telegram.WebApp.expand(); }, close: function() { Telegram.WebApp.close(); }, sendMessage: function(msg_id, with_webview) { if (!DemoApp.initDataUnsafe.query_id) { alert('WebViewQueryId not defined'); return; } $('button').prop('disabled', true); $('#btn_status').text('Sending...').removeClass('ok err').show(); DemoApp.apiRequest('sendMessage', { msg_id: msg_id || '', with_webview: !DemoApp.initDataUnsafe.receiver && with_webview ? 1 : 0 }, 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 if (result.error) { $('#btn_status').text(result.error).addClass('err').show(); alert(result.error); } else { $('#btn_status').text('Unknown error').addClass('err').show(); alert('Unknown error'); } }); }, changeMenuButton: function(close) { $('button').prop('disabled', true); $('#btn_status').text('Changing button...').removeClass('ok err').show(); DemoApp.apiRequest('changeMenuButton', {}, 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 if (result.error) { $('#btn_status').text(result.error).addClass('err').show(); alert(result.error); } else { $('#btn_status').text('Unknown error').addClass('err').show(); alert('Unknown error'); } }); if (close) { setTimeout(function() { Telegram.WebApp.close(); }, 50); } }, checkInitData: function() { if (DemoApp.initDataUnsafe.query_id && DemoApp.initData && $('#webview_data_status').hasClass('status_need')) { $('#webview_data_status').removeClass('status_need'); DemoApp.apiRequest('checkInitData', {}, function(result) { if (result.ok) { $('#webview_data_status').html('Hash is correct (async)').addClass('ok'); } else { $('#webview_data_status').html(result.error + ' (async)').addClass('err'); } }); } }, sendText: function(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); } }, sendTime: function(spam) { var repeat = spam ? 10 : 1; for (var i = 0; i < repeat; i++) { Telegram.WebApp.sendData(new Date().toString()); } }, switchInlineQuery: function(query, choose_chat) { var choose_chat_types = false; if (choose_chat) { var choose_chat_types = []; var types = ['users', 'bots', 'groups', 'channels']; for (var i = 0; i < types.length; i++) { if ($('#select-' + types[i]).prop('checked')) { choose_chat_types.push(types[i]); } } if (!choose_chat_types.length) { return DemoApp.showAlert('Select chat types!'); } } Telegram.WebApp.switchInlineQuery(query, choose_chat_types); }, requestLocation: function(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; }, requestVideo: function(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; }, requestAudio: function(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; }, requestAudioVideo: function(el) { if (navigator.mediaDevices) { navigator.mediaDevices.getUserMedia({ audio: true, 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; }, testClipboard: function(el) { Telegram.WebApp.readTextFromClipboard(function(clipText) { if (clipText === null) { $(el).next('span').html('Clipboard text unavailable.').attr('class', 'err'); } else { $(el).next('span').text('(Read from clipboard: «' + clipText + '»)').attr('class', 'ok'); } }); return false; }, requestWriteAccess: function(el) { Telegram.WebApp.requestWriteAccess(function(allowed) { if (allowed) { $(el).next('span').text('(Access granted)').attr('class', 'ok'); } else { $(el).next('span').html('(User declined this request)').attr('class', 'err'); } }); }, requestPhoneNumber: function(el) { Telegram.WebApp.requestContact(function(sent) { if (sent) { $(el).next('span').text('(Phone number sent to the bot)').attr('class', 'ok'); } else { $(el).next('span').html('(User declined this request)').attr('class', 'err'); } }); }, requestServerTime: function(el) { Telegram.WebApp.invokeCustomMethod('getCurrentTime', {}, function(err, time) { if (err) { $(el).next('span').html('(' + cleanHTML(err) + ')').attr('class', 'err'); } else { $(el).next('span').text('(' + (new Date(time*1000)).toString() + ')').attr('class', 'ok'); } }); }, cloudStorageKeys: {}, cloudStorageItems: {}, editCloudRow: function(el, event) { event.preventDefault(); var values = DemoApp.cloudStorageItems; var key = $(el).parents('tr').attr('data-key'); el.form.reset(); el.form.key.value = key; el.form.value.value = values[key]; }, deleteCloudRow: function(el, event) { event.preventDefault(); var key = $(el).parents('tr').attr('data-key'); Telegram.WebApp.CloudStorage.removeItem(key, function(err, deleted) { if (err) { DemoApp.showAlert('Error: ' + err); } else { if (deleted) { var index = DemoApp.cloudStorageKeys.indexOf(key); if (index >= 0) { DemoApp.cloudStorageKeys.splice(index, 1); } delete DemoApp.cloudStorageItems[key]; } el.form.reset(); DemoApp.updateCloudRows(); } }); }, saveCloudForm: function(form, event) { event.preventDefault(); var key = form.key.value; var value = form.value.value; Telegram.WebApp.CloudStorage.setItem(key, value, function(err, saved) { if (err) { DemoApp.showAlert('Error: ' + err); } else { if (saved) { if (typeof DemoApp.cloudStorageItems[key] === 'undefined') { DemoApp.cloudStorageKeys.push(key); } DemoApp.cloudStorageItems[key] = value; } form.reset(); DemoApp.updateCloudRows(); } }); }, updateCloudRows: function() { var html = ''; var keys = DemoApp.cloudStorageKeys; var values = DemoApp.cloudStorageItems; for (var i = 0; i < keys.length; i++) { var key = keys[i]; html += '