fix: resolve 500 error by removing Astro.locals.runtime.env and switch to robust HTML parsing for telegram dispatch
This commit is contained in:
1 parent
21058cc9b2
commit
7d270a63e3
1 file changed
+38
-17
+38
-17
@@ -66,26 +66,47 @@ export const POST: APIRoute = async ({ request, locals }) => {
|
|||||||
if (history.length > 30) history.shift();
|
if (history.length > 30) history.shift();
|
||||||
sessions.set(sessionId, history);
|
sessions.set(sessionId, history);
|
||||||
|
|
||||||
// Dispatch to Telegram Bot (support Cloudflare Pages runtime bindings, process.env, and import.meta.env)
|
// Safely resolve bot credentials across Astro / Cloudflare Workers
|
||||||
const runtimeEnv = (locals as any)?.runtime?.env || {};
|
let botToken: string | undefined = undefined;
|
||||||
const botToken = runtimeEnv.TELEGRAM_BOT_TOKEN ||
|
let chatId: string | undefined = undefined;
|
||||||
(typeof process !== 'undefined' ? process.env?.TELEGRAM_BOT_TOKEN : undefined) ||
|
|
||||||
(import.meta.env as any)?.TELEGRAM_BOT_TOKEN;
|
try {
|
||||||
const chatId = runtimeEnv.TELEGRAM_CHAT_ID ||
|
if (typeof process !== 'undefined' && process.env) {
|
||||||
(typeof process !== 'undefined' ? process.env?.TELEGRAM_CHAT_ID : undefined) ||
|
botToken = process.env.TELEGRAM_BOT_TOKEN;
|
||||||
(import.meta.env as any)?.TELEGRAM_CHAT_ID;
|
chatId = process.env.TELEGRAM_CHAT_ID;
|
||||||
|
}
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
if (!botToken && typeof import.meta !== 'undefined' && import.meta.env) {
|
||||||
|
botToken = (import.meta.env as any).TELEGRAM_BOT_TOKEN;
|
||||||
|
chatId = (import.meta.env as any).TELEGRAM_CHAT_ID;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try cloudflare worker env if available
|
||||||
|
if (!botToken && (locals as any)?.env) {
|
||||||
|
botToken = (locals as any).env.TELEGRAM_BOT_TOKEN;
|
||||||
|
chatId = (locals as any).env.TELEGRAM_CHAT_ID;
|
||||||
|
}
|
||||||
|
|
||||||
let tgSuccess = false;
|
let tgSuccess = false;
|
||||||
let tgErrorDetails = null;
|
let tgErrorDetails = null;
|
||||||
|
|
||||||
if (botToken && chatId) {
|
if (botToken && chatId) {
|
||||||
const telegramText =
|
const safeSession = String(sessionId).replace(/[&<>"']/g, '');
|
||||||
`💬 *New Portfolio Live Message*\n\n` +
|
const safeUser = String(userName || 'Visitor').replace(/[&<>"']/g, '');
|
||||||
`👤 *From:* ${userName || 'Visitor'}\n` +
|
const safeLoc = String(locationStr).replace(/[&<>"']/g, '');
|
||||||
`🔑 *Session:* #${sessionId}\n` +
|
const safeMsg = String(message.trim())
|
||||||
`📍 *Origin:* ${locationStr}\n\n` +
|
.replace(/&/g, '&')
|
||||||
`📝 *Message:*\n"${message.trim()}"\n\n` +
|
.replace(/</g, '<')
|
||||||
`_👉 To reply to this visitor, swipe right and reply to this message!_`;
|
.replace(/>/g, '>');
|
||||||
|
|
||||||
|
const telegramHtml =
|
||||||
|
`💬 <b>New Portfolio Live Message</b>\n\n` +
|
||||||
|
`👤 <b>From:</b> ${safeUser}\n` +
|
||||||
|
`🔑 <b>Session:</b> #${safeSession}\n` +
|
||||||
|
`📍 <b>Origin:</b> ${safeLoc}\n\n` +
|
||||||
|
`📝 <b>Message:</b>\n<i>"${safeMsg}"</i>\n\n` +
|
||||||
|
`👉 <i>To reply, swipe right and reply to this message!</i>`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const tgRes = await fetch(`https://api.telegram.org/bot${botToken}/sendMessage`, {
|
const tgRes = await fetch(`https://api.telegram.org/bot${botToken}/sendMessage`, {
|
||||||
@@ -93,8 +114,8 @@ export const POST: APIRoute = async ({ request, locals }) => {
|
|||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
chat_id: String(chatId).trim(),
|
chat_id: String(chatId).trim(),
|
||||||
text: telegramText,
|
text: telegramHtml,
|
||||||
parse_mode: 'Markdown',
|
parse_mode: 'HTML',
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user