-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.js
More file actions
94 lines (85 loc) · 3.14 KB
/
main.js
File metadata and controls
94 lines (85 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const TELEGRAM_API_TOKEN = '*******************************';
const IPINFO_TOKEN = '**********';
const TELEGRAM_API_URL = `https://api.telegram.org/bot${TELEGRAM_API_TOKEN}`;
async function fetchIPInfo(ip) {
try {
const response = await fetch(`https://ipinfo.io/${ip}?token=${IPINFO_TOKEN}`);
if (!response.ok) {
throw new Error(`IPInfo API returned status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('Error fetching IP info:', error);
throw new Error('Failed to fetch IP information.');
}
}
async function handleUpdate(update) {
const message = update.message;
if (!message || !message.chat || !message.chat.id) {
console.error('Invalid update structure:', update);
return;
}
const chatId = message.chat.id;
const text = message.text?.trim();
if (!text) {
await sendMessage(chatId, 'Please send a valid IP address (IPv4 or IPv6).');
return;
}
if (text === '/start') {
await sendMessage(chatId, 'Hi! Send me an IP address, and I will show you its information.');
} else if (/^(\d{1,3}\.){3}\d{1,3}$/.test(text) || /^[a-fA-F0-9:]+$/.test(text)) {
try {
const ipInfo = await fetchIPInfo(text);
const responseMessage = `
*IP Information:*
- *IP:* ${ipInfo.ip}
- *City:* ${ipInfo.city || 'Unknown'}
- *Region:* ${ipInfo.region || 'Unknown'}
- *Country:* ${ipInfo.country || 'Unknown'}
- *Location:* ${ipInfo.loc || 'Unknown'}
- *Organization:* ${ipInfo.org || 'Unknown'}
- *Postal Code:* ${ipInfo.postal || 'Unknown'}
- *Timezone:* ${ipInfo.timezone || 'Unknown'}
`;
await sendMessage(chatId, responseMessage, 'Markdown');
} catch (error) {
await sendMessage(chatId, 'Sorry, I couldn’t fetch the IP information. Please try again later.');
}
} else {
await sendMessage(chatId, 'Invalid IP address. Please send a valid IPv4 or IPv6 address.');
}
}
async function sendMessage(chatId, text, parseMode = 'Markdown') {
try {
const response = await fetch(`${TELEGRAM_API_URL}/sendMessage`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_id: chatId,
text: text,
parse_mode: parseMode
})
});
if (!response.ok) {
console.error('Failed to send message:', await response.text());
}
} catch (error) {
console.error('Error sending message:', error);
}
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
if (request.method === 'POST') {
try {
const update = await request.json();
await handleUpdate(update);
return new Response('OK', { status: 200 });
} catch (error) {
console.error('Error handling update:', error);
return new Response('Internal Server Error', { status: 500 });
}
}
return new Response('Method Not Allowed', { status: 405 });
}