mirror of
https://github.com/tiennm99/miti-telegram.git
synced 2026-04-17 13:21:34 +00:00
251
src/index.js
251
src/index.js
@@ -1,78 +1,78 @@
|
|||||||
export default {
|
export default {
|
||||||
async fetch(request, env, ctx) {
|
async fetch(request, env, ctx) {
|
||||||
// Handle CORS preflight requests
|
// Handle CORS preflight requests
|
||||||
if (request.method === 'OPTIONS') {
|
if (request.method === 'OPTIONS') {
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
status: 204,
|
status: 204,
|
||||||
headers: {
|
headers: {
|
||||||
'Access-Control-Allow-Origin': '*',
|
'Access-Control-Allow-Origin': '*',
|
||||||
'Access-Control-Allow-Methods': 'POST, OPTIONS',
|
'Access-Control-Allow-Methods': 'POST, OPTIONS',
|
||||||
'Access-Control-Allow-Headers': 'Content-Type',
|
'Access-Control-Allow-Headers': 'Content-Type',
|
||||||
'Access-Control-Max-Age': '86400',
|
'Access-Control-Max-Age': '86400',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.method !== 'POST') {
|
if (request.method !== 'POST') {
|
||||||
return new Response('Method not allowed', {
|
return new Response('Method not allowed', {
|
||||||
status: 405,
|
status: 405,
|
||||||
headers: {
|
headers: {
|
||||||
'Access-Control-Allow-Origin': '*',
|
'Access-Control-Allow-Origin': '*',
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const contentType = request.headers.get('content-type');
|
const contentType = request.headers.get('content-type');
|
||||||
let text;
|
let text;
|
||||||
|
|
||||||
if (contentType && contentType.includes('application/json')) {
|
if (contentType && contentType.includes('application/json')) {
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
text = body.text;
|
text = body.text;
|
||||||
} else if (contentType && contentType.includes('application/x-www-form-urlencoded')) {
|
} else if (contentType && contentType.includes('application/x-www-form-urlencoded')) {
|
||||||
const formData = await request.formData();
|
const formData = await request.formData();
|
||||||
text = formData.get('text');
|
text = formData.get('text');
|
||||||
} else {
|
} else {
|
||||||
return new Response('Content-Type must be application/json or application/x-www-form-urlencoded', {
|
return new Response('Content-Type must be application/json or application/x-www-form-urlencoded', {
|
||||||
status: 400,
|
status: 400,
|
||||||
headers: {
|
headers: {
|
||||||
'Access-Control-Allow-Origin': '*',
|
'Access-Control-Allow-Origin': '*',
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!text) {
|
if (!text) {
|
||||||
return new Response('Missing text parameter', {
|
return new Response('Missing text parameter', {
|
||||||
status: 400,
|
status: 400,
|
||||||
headers: {
|
headers: {
|
||||||
'Access-Control-Allow-Origin': '*',
|
'Access-Control-Allow-Origin': '*',
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Collect client request information
|
// Collect client request information
|
||||||
const clientIP = request.headers.get('CF-Connecting-IP') ||
|
const clientIP = request.headers.get('CF-Connecting-IP') ||
|
||||||
request.headers.get('X-Forwarded-For') ||
|
request.headers.get('X-Forwarded-For') ||
|
||||||
request.headers.get('X-Real-IP') ||
|
request.headers.get('X-Real-IP') ||
|
||||||
'Unknown';
|
'Unknown';
|
||||||
|
|
||||||
const userAgent = request.headers.get('User-Agent') || 'Unknown';
|
const userAgent = request.headers.get('User-Agent') || 'Unknown';
|
||||||
const country = request.cf?.country || 'Unknown';
|
const country = request.cf?.country || 'Unknown';
|
||||||
const city = request.cf?.city || 'Unknown';
|
const city = request.cf?.city || 'Unknown';
|
||||||
const region = request.cf?.region || 'Unknown';
|
const region = request.cf?.region || 'Unknown';
|
||||||
const latitude = request.cf?.latitude || 'Unknown';
|
const latitude = request.cf?.latitude || 'Unknown';
|
||||||
const longitude = request.cf?.longitude || 'Unknown';
|
const longitude = request.cf?.longitude || 'Unknown';
|
||||||
const timezone = request.cf?.timezone || 'Unknown';
|
const timezone = request.cf?.timezone || 'Unknown';
|
||||||
const url = request.url || 'Unknown';
|
const url = request.url || 'Unknown';
|
||||||
const timestamp = new Date().toISOString();
|
const timestamp = new Date().toISOString();
|
||||||
|
|
||||||
// Format the message with request information
|
// Format the message with request information
|
||||||
const hasCoordinates = latitude !== 'Unknown' && longitude !== 'Unknown';
|
const hasCoordinates = latitude !== 'Unknown' && longitude !== 'Unknown';
|
||||||
const mapLink = hasCoordinates
|
const mapLink = hasCoordinates
|
||||||
? `https://www.google.com/maps?q=${latitude},${longitude}`
|
? `https://www.google.com/maps?q=${latitude},${longitude}`
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
const requestInfo = `<b>IP</b>: <code>${clientIP}</code> <a href="https://ipinfo.io/${clientIP}">🔍</a>
|
const requestInfo = `<b>IP</b>: <code>${clientIP}</code> <a href="https://ipinfo.io/${clientIP}">🔍</a>
|
||||||
<b>Browser</b>: <code>${userAgent}</code>
|
<b>Browser</b>: <code>${userAgent}</code>
|
||||||
<b>Country</b>: <code>${country}</code>
|
<b>Country</b>: <code>${country}</code>
|
||||||
<b>Region</b>: <code>${region}</code>
|
<b>Region</b>: <code>${region}</code>
|
||||||
@@ -82,77 +82,70 @@ export default {
|
|||||||
<b>Timestamp</b>: <code>${timestamp}</code>
|
<b>Timestamp</b>: <code>${timestamp}</code>
|
||||||
<b>Original text</b>:`;
|
<b>Original text</b>:`;
|
||||||
|
|
||||||
const formattedMessage = `${requestInfo}
|
const formattedMessage = `${requestInfo}
|
||||||
|
|
||||||
${text}`;
|
${text}`;
|
||||||
|
|
||||||
const telegramToken = env.TELEGRAM_TOKEN;
|
const telegramToken = env.TELEGRAM_TOKEN;
|
||||||
const telegramChatId = env.TELEGRAM_CHAT_ID;
|
const telegramChatId = env.TELEGRAM_CHAT_ID;
|
||||||
|
|
||||||
if (!telegramToken || !telegramChatId) {
|
if (!telegramToken || !telegramChatId) {
|
||||||
return new Response('Missing TELEGRAM_TOKEN or TELEGRAM_CHAT_ID environment variables', {
|
return new Response('Missing TELEGRAM_TOKEN or TELEGRAM_CHAT_ID environment variables', {
|
||||||
status: 500,
|
status: 500,
|
||||||
headers: {
|
headers: {
|
||||||
'Access-Control-Allow-Origin': '*',
|
'Access-Control-Allow-Origin': '*',
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const telegramUrl = `https://api.telegram.org/bot${telegramToken}/sendMessage`;
|
const telegramUrl = `https://api.telegram.org/bot${telegramToken}/sendMessage`;
|
||||||
const telegramPayload = {
|
const telegramPayload = {
|
||||||
chat_id: telegramChatId,
|
chat_id: telegramChatId,
|
||||||
text: formattedMessage,
|
text: formattedMessage,
|
||||||
parse_mode: 'HTML'
|
parse_mode: 'HTML'
|
||||||
};
|
};
|
||||||
|
|
||||||
const telegramResponse = await fetch(telegramUrl, {
|
const telegramResponse = await fetch(telegramUrl, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify(telegramPayload)
|
body: JSON.stringify(telegramPayload)
|
||||||
});
|
});
|
||||||
|
|
||||||
const telegramResult = await telegramResponse.json();
|
const telegramResult = await telegramResponse.json();
|
||||||
|
|
||||||
if (!telegramResponse.ok) {
|
if (!telegramResponse.ok) {
|
||||||
return new Response(JSON.stringify({
|
return new Response(JSON.stringify({
|
||||||
success: false
|
success: false
|
||||||
}), {
|
}), {
|
||||||
status: 500,
|
status: 500,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Access-Control-Allow-Origin': '*',
|
'Access-Control-Allow-Origin': '*',
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Response(JSON.stringify({
|
return new Response(JSON.stringify({
|
||||||
success: true
|
success: true
|
||||||
}), {
|
}), {
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Access-Control-Allow-Origin': '*',
|
'Access-Control-Allow-Origin': '*',
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error processing request:', error);
|
return new Response(JSON.stringify({
|
||||||
console.error('Request details:', {
|
success: false
|
||||||
method: request.method,
|
}), {
|
||||||
url: request.url,
|
status: 500,
|
||||||
headers: Object.fromEntries(request.headers.entries())
|
headers: {
|
||||||
});
|
'Content-Type': 'application/json',
|
||||||
|
'Access-Control-Allow-Origin': '*',
|
||||||
return new Response(JSON.stringify({
|
}
|
||||||
success: false
|
});
|
||||||
}), {
|
}
|
||||||
status: 500,
|
},
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user