Sending data from the website feedback form to Telegram

July 29, 2024

Creating a Telegram Bot:

  • Find BotFather in Telegram and create a new bot by following his instructions.
  • Get the bot's API key.
  • Enable the bot by opening it in Telegram and clicking "Start."

Setting up the PHP Script:

Create a PHP script to handle form data and send it to Telegram.

In the PHP script, do the following:

  • Get the form data ($_POST or $_GET).
  • Format the message to be sent to Telegram.
  • Use the file_get_contents or cURL function to send a request to the Telegram Bot API.

Configuring Telegram:

Find the chat_id for the chat where you want to send messages. You can use @userinfobot in Telegram to get this information.

You can send messages either to a channel or to yourself as a direct message. Here's how to do it in each case:

Sending Messages to Yourself:

Find your user_id:

  • Open Telegram and find the @userinfobot.
  • Send him the /start command.
  • He will reply with a message containing your user_id.

Sending Messages to a Channel:

Create a Channel:

  1. Open Telegram and create a new channel if you don't have one.
  2. Remember its name or @username.
  3. Add the Bot to the Channel:

    • Add your bot (created via BotFather) to the channel as an administrator. To do this, open the channel settings, go to "Administrators," and add the bot.

    Find the Channel's chat_id:

    • You can find the chat_id using a special service or by messaging the bot directly. The channel's chat_id usually starts with -100.

    Now you can send messages both to yourself and to the channel.

    Complete Example:

    When the user fills out the form and submits it, the data will be sent to send_form.php, which will process this data and send the message to Telegram. Here is what the final HTML and PHP code looks like:

    HTML (Form):

    <form id="feedbackForm" method="post" action="/send_form.php" accept-charset="UTF-8">
        <input type="text" placeholder="Name" class="form-control" id="name" name="name" required="">
        <input type="tel" placeholder="Phone" class="form-control" id="phone" name="phone" required="">
        <textarea class="form-control" placeholder="Message" id="message" name="message" rows="5" required=""></textarea>
        <button type="submit" class="btn btn-lb">SEND</button>
    </form>

    PHP (send_form.php):

    <?php
    // Replace <YOUR_BOT_TOKEN--> with your bot's API key
    $botToken = "<your_bot_token>";
    // Replace <your_user_id> with your user_id
    $chatId = "<your_user_id>";
    
    // Get the form data
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
    
    // Format the message for Telegram
    $telegramMessage = "New message from the form:\n\n";
    $telegramMessage .= "<b>Name:</b> $name\n";
    $telegramMessage .= "<b>Phone:</b> $phone\n";
    $telegramMessage .= "<b>Message:</b> $message";
    
    // Telegram API URL
    $telegramApiUrl = "https://api.telegram.org/bot$botToken/sendMessage?chat_id=$chatId&text=" . urlencode($telegramMessage) . "&parse_mode=HTML";
    
    // Send the request to the Telegram API
    $response = file_get_contents($telegramApiUrl);
    
    // Check the response (you can add error handling)
    if ($response) {
        echo "Message successfully sent to Telegram!";
    } else {
        echo "Error sending message to Telegram.";
    }
    ?>

    Follow these steps, and you will be able to send form data to Telegram successfully. If you have any questions or need further assistance, feel free to ask!

Ready to assist in creating a new website starting from $250 or making changes to an existing one.
Contact us for professional support.

Contact me on Telegram: @lb_user Write by e-mail: [email protected]

Add comment