Skip to content

Instantly share code, notes, and snippets.

@mahdyar
Created March 3, 2021 14:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahdyar/47348a67eb83d336d4bb0556a40bcc32 to your computer and use it in GitHub Desktop.
Save mahdyar/47348a67eb83d336d4bb0556a40bcc32 to your computer and use it in GitHub Desktop.
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event));
});
let data = {
ok: false,
};
async function handleRequest(event) {
if (event.request.method == "POST") {
let body = await event.request.json();
if (body.url && body.title && body.telemark_code) {
const url = body.url;
const title = body.title;
const telemark_code = body.telemark_code;
const api =
"https://api.telegram.org/bot" +
API_KEY +
"/sendMessage?chat_id=" +
telemark_code +
"&text=[" +
title +
"](" +
url +
")&parse_mode=markdown";
const init = {
method: "GET",
headers: {
"content-type": "application/json;charset=UTF-8",
},
};
const response = await fetch(api, init);
const responseBody = await response.json();
data = {
ok: responseBody.ok,
};
}
}
const json = JSON.stringify(data, null, 2);
return new Response(json, {
headers: {
"content-type": "application/json;charset=UTF-8",
},
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment