Skip to content

Instantly share code, notes, and snippets.

@toolmanxyz
Created Apr 18, 2022
Embed
What would you like to do?
const token = ''
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
if(request.method == 'POST'){
let data = await request.json()
if(data.message !== undefined){
handlemessage(data.message)
}
}
return new Response('ok', {status: 200})
}
async function handlemessage(d){
let message_text = d.web_app_data.data.split(',')[1] || ''
let web_app_query_id = d.web_app_data.data.split(',')[0]
// await tg(token,'sendmessage',{
// chat_id: chat_id,
// text: d.web_app_data.data
// })
let res_data = []
res_data.push({
type: "article",
id: "123123-123123",
title: "HELLO WORLD",
input_message_content: {
message_text: message_text
}
})
await tg(token,'answerWebAppQuery',{
web_app_query_id: web_app_query_id,
result: JSON.stringify(res_data[0])
})
}
async function tg(token,type,data,n = true){
try {
let t = await fetch('https://api.telegram.org/bot' + token + '/' + type,{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
let d = await t.json()
if(!d.ok && n)
throw d
else
return d
}catch(e){
return e
}
}
const html = `<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://telegram.org/js/telegram-web-app.js"></script>
<style type="text/css">
div {
font-size: 3em;
color: var(--tg-theme-text-color);
}
button {
font-size: 2em;
color: var(--tg-theme-button-text-color);
background-color: var(--tg-theme-button-color);
}
</style>
</head>
<body>
<div id="telegram-app">Hello, World!</div>
<input type="text" id="input" /> <br />
<button onClick="click_event()">Click me</button>
</body>
<script>
let telegram = window.Telegram.WebApp;
telegram.onEvent('mainButtonClicked', () => {
document.getElementById("telegram-app").innerText = document.getElementById("input").value;
telegram.sendData(telegram.initData.split('&')[0].split('=')[1] + "," + document.getElementById("telegram-app").innerText);
});
function click_event() {
telegram.MainButton.show();
}
</script>
</html>`;
async function handleRequest(request) {
return new Response(html, {
headers: {
'content-type': 'text/html;charset=UTF-8',
},
status: 200,
});
}
addEventListener('fetch', event => {
return event.respondWith(handleRequest(event.request));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment