mirror of
https://github.com/tiennm99/miti-telegram.git
synced 2026-10-11 03:13:41 +00:00
112 lines
3.2 KiB
JavaScript
112 lines
3.2 KiB
JavaScript
export default {
|
|
async fetch(request, env, ctx) {
|
|
if (request.method !== 'POST') {
|
|
return new Response('Method not allowed', { status: 405 });
|
|
}
|
|
|
|
try {
|
|
const contentType = request.headers.get('content-type');
|
|
let text;
|
|
|
|
if (contentType && contentType.includes('application/json')) {
|
|
const body = await request.json();
|
|
text = body.text;
|
|
} else if (contentType && contentType.includes('application/x-www-form-urlencoded')) {
|
|
const formData = await request.formData();
|
|
text = formData.get('text');
|
|
} else {
|
|
return new Response('Content-Type must be application/json or application/x-www-form-urlencoded', { status: 400 });
|
|
}
|
|
|
|
if (!text) {
|
|
return new Response('Missing text parameter', { status: 400 });
|
|
}
|
|
|
|
// Collect client request information
|
|
const clientIP = request.headers.get('CF-Connecting-IP') ||
|
|
request.headers.get('X-Forwarded-For') ||
|
|
request.headers.get('X-Real-IP') ||
|
|
'Unknown';
|
|
|
|
const userAgent = request.headers.get('User-Agent') || 'Unknown';
|
|
const country = request.cf?.country || 'Unknown';
|
|
const city = request.cf?.city || 'Unknown';
|
|
const region = request.cf?.region || 'Unknown';
|
|
const latitude = request.cf?.latitude || 'Unknown';
|
|
const longitude = request.cf?.longitude || 'Unknown';
|
|
const timezone = request.cf?.timezone || 'Unknown';
|
|
const url = request.url || 'Unknown';
|
|
const timestamp = new Date().toISOString();
|
|
|
|
// Format the message with request information
|
|
const hasCoordinates = latitude !== 'Unknown' && longitude !== 'Unknown';
|
|
const mapLink = hasCoordinates
|
|
? `https://www.google.com/maps?q=${latitude},${longitude}`
|
|
: null;
|
|
|
|
const requestInfo = `URL: ${url}
|
|
IP: ${clientIP}
|
|
Browser: ${userAgent}
|
|
Country: ${country}
|
|
Region: ${region}
|
|
City: ${city}
|
|
Coordinates: ${latitude}, ${longitude}
|
|
Timezone: ${timezone}
|
|
Timestamp: ${timestamp}
|
|
Original text:`;
|
|
|
|
const formattedMessage = `<code>
|
|
${requestInfo}
|
|
</code>
|
|
${mapLink ? `<a href="${mapLink}">📍 View Location on Map</a>\n\n` : ''}${text}`;
|
|
|
|
const telegramToken = env.TELEGRAM_TOKEN;
|
|
const telegramChatId = env.TELEGRAM_CHAT_ID;
|
|
|
|
if (!telegramToken || !telegramChatId) {
|
|
return new Response('Missing TELEGRAM_TOKEN or TELEGRAM_CHAT_ID environment variables', { status: 500 });
|
|
}
|
|
|
|
const telegramUrl = `https://api.telegram.org/bot${telegramToken}/sendMessage`;
|
|
const telegramPayload = {
|
|
chat_id: telegramChatId,
|
|
text: formattedMessage,
|
|
parse_mode: 'HTML'
|
|
};
|
|
|
|
const telegramResponse = await fetch(telegramUrl, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(telegramPayload)
|
|
});
|
|
|
|
const telegramResult = await telegramResponse.json();
|
|
|
|
if (!telegramResponse.ok) {
|
|
return new Response(JSON.stringify({
|
|
success: false
|
|
}), {
|
|
status: 500,
|
|
headers: { 'Content-Type': 'application/json' }
|
|
});
|
|
}
|
|
|
|
return new Response(JSON.stringify({
|
|
success: true
|
|
}), {
|
|
headers: { 'Content-Type': 'application/json' }
|
|
});
|
|
|
|
} catch (error) {
|
|
return new Response(JSON.stringify({
|
|
success: false
|
|
}), {
|
|
status: 500,
|
|
headers: { 'Content-Type': 'application/json' }
|
|
});
|
|
}
|
|
},
|
|
};
|