Skip to content

Instantly share code, notes, and snippets.

@thanksshu
Last active May 10, 2024 08:59
Show Gist options
  • Save thanksshu/a4b8506067a03f163174b9989a3057ab to your computer and use it in GitHub Desktop.
Save thanksshu/a4b8506067a03f163174b9989a3057ab to your computer and use it in GitHub Desktop.
Genshin Impact Daily Check-In Cloudflare Worker script
/// Cloudflare Worker script for Genshin Impact Daily Check-In
/// LTUID, LTOKEN must be set for check-in
/// TELEGRAM_CHAT_ID, TELEGRAM_BOT_TOKEN may be set for messaging
const CHECK_IN_URL = "https://hk4e-api-os.mihoyo.com/event/sol/sign?act_id=e202102251931481";
async function handleScheduled(request) {
const headers = new Headers();
headers.set("Cookie", `ltuid=${LTUID}; ltoken=${LTOKEN};`);
headers.set(
"User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/100.0"
);
const checkInResponse = await fetch(CHECK_IN_URL, {
method: "POST",
headers: headers,
});
const checkInResult = await checkInResponse.json();
const message = `(${checkInResult.retcode}) ${checkInResult.message}`;
if (checkInResult.retcode != -5003 && checkInResult.retcode != 200 && checkInResult.retcode != 0) {
try {
const TELEGRAM_MESSAGE_URL = `https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage`;
const messageTelegramResponse = await fetch(TELEGRAM_MESSAGE_URL, {
method: "POST",
body: JSON.stringify({
chat_id: TELEGRAM_CHAT_ID,
text: message,
}),
headers: {
"Content-Type": "application/json",
},
});
if (!messageTelegramResponse.ok) {
console.error("Telegram messaging failed");
}
} catch (error) {
if (error instanceof ReferenceError) {
console.log("Telegram messaging not activatied");
} else {
console.error(error);
}
}
}
console.log(message);
}
addEventListener("scheduled", (event) => {
event.waitUntil(handleScheduled(event));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment