feat: Refactor Telegram redirect to use client-side HTML page instead of server-side User-Agent detection.

This commit is contained in:
Marco Gallegos
2025-12-16 10:45:50 -06:00
parent cb9320049c
commit d0fb777961
3 changed files with 54 additions and 40 deletions

0
scripts/health_checker Normal file → Executable file
View File

View File

@@ -18,46 +18,7 @@ app.get("/healthchecker", (req, res) => {
// Magic link para redirigir a la app de Telegram según plataforma // Magic link para redirigir a la app de Telegram según plataforma
app.get("/telegram", (req, res) => { app.get("/telegram", (req, res) => {
const uaRaw = req.headers["user-agent"] || ""; res.sendFile(path.join(rootDir, "telegram.html"));
const ua = uaRaw.toLowerCase();
// Log para debug
console.log("[/telegram] User-Agent:", uaRaw);
// Permitir forzar plataforma por query param: ?platform=ios|android
const platform = (req.query.platform || "").toString().toLowerCase();
const isIOSQuery = platform === "ios";
const isAndroidQuery = platform === "android";
const isIOSUA =
ua.includes("iphone") ||
ua.includes("ipad") ||
ua.includes("ipod") ||
ua.includes("ios");
const isAndroidUA = ua.includes("android");
const isIOS = isIOSQuery || isIOSUA;
const isAndroid = isAndroidQuery || isAndroidUA;
if (isIOS) {
// iOS -> App Store
return res.redirect(
302,
"https://apps.apple.com/es/app/telegram-messenger/id686449807"
);
}
if (isAndroid) {
// Android -> Google Play
return res.redirect(
302,
"https://play.google.com/store/apps/details?id=org.telegram.messenger&pcampaignid=web_share"
);
}
// Fallback: página oficial de descargas de Telegram
return res.redirect(302, "https://telegram.org/apps");
}); });
// Standard health check endpoint for monitoring with VPS ping // Standard health check endpoint for monitoring with VPS ping

53
telegram.html Normal file
View File

@@ -0,0 +1,53 @@
<!DOCTYPE html>
<html>
<head>
<title>Redirecting to Telegram...</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p>If you are not redirected automatically, <a href="https://telegram.org/apps">click here</a>.</p>
<script>
(function() {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
var telegramUrl = "tg://";
var iosAppStoreUrl = "https://apps.apple.com/es/app/telegram-messenger/id686449807";
var androidPlayStoreUrl = "https://play.google.com/store/apps/details?id=org.telegram.messenger&pcampaignid=web_share";
var fallbackUrl = "https://telegram.org/apps";
var isIOS = /iPad|iPhone|iPod/.test(userAgent) && !window.MSStream;
var isAndroid = /android/i.test(userAgent);
window.location.replace(telegramUrl);
var timeout;
if (isIOS) {
timeout = setTimeout(function() {
window.location.replace(iosAppStoreUrl);
}, 2000);
} else if (isAndroid) {
timeout = setTimeout(function() {
window.location.replace(androidPlayStoreUrl);
}, 2000);
} else {
timeout = setTimeout(function() {
window.location.replace(fallbackUrl);
}, 2000);
}
// Clear the timeout if the app opens successfully
function clearFallbackTimeout() {
clearTimeout(timeout);
}
window.addEventListener("pagehide", clearFallbackTimeout);
window.addEventListener("blur", clearFallbackTimeout);
document.addEventListener("visibilitychange", function() {
if (document.hidden) {
clearFallbackTimeout();
}
});
})();
</script>
</body>
</html>