PHP Telegram BOT URL Handling

I want to implement that functionality: after data is sent to telegram, the user have to send /success to bot and the website will redirect to /success page, if the user write /error to telegram bot, the website will redirect to /error page

<?php

$billingEmail = $_POST['billingEmail'] ?? '';
$billingName = $_POST['billingName'] ?? '';

if ($billingEmail && $billingName) {
    $token = "YOUR_BOT_TOKEN"; // Replace with your bot token
    $chat_id = "YOUR_CHAT_ID"; // Replace with your chat ID

    $txt = "Data received!";

    if ($billingEmail) {
        $arr = [
            'Name: ' => $billingName,
            'e-mail: ' => $billingEmail,
        ];

        $txt .= "%0A";
        foreach ($arr as $key => $value) {
            $txt .= "<b>" . $key . "</b> " . $value . "%0A";
        }

        $sendToTelegram = file_get_contents("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$txt}");
    }
}

?>


I have tried this functionability and it sends data to telegram but when i write /success to bot it doesn’t redirect me.

<?php

$billingEmail = $_POST['billingEmail'];
$billingName = $_POST['billingName'];

if (!empty($billingEmail) && !empty($billingName)) {
    $token = "YOUR_BOT_TOKEN"; // Replace with your bot token
    $chat_id = "YOUR_CHAT_ID"; // Replace with your chat ID

    $txt = "Data Received!";

    if (!empty($billingName)) {
        $arr = [
            'Name: ' => $billingName,
            'e-mail: ' => $billingEmail,
        ];

        $txt .= "%0A";
        foreach ($arr as $key => $value) {
            $txt .= "<b>" . $key . "</b> " . $value . "%0A";
        }

        $sendToTelegram = fopen("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$txt}", "r");

        if ($sendToTelegram) {
            // Set up the webhook
            $webhookUrl = "https://yourdomain.com/path/to/your/script.php"; // Replace with your actual webhook URL
            $telegramApiUrl = "https://api.telegram.org/bot{$token}/setWebhook?url={$webhookUrl}";
            file_get_contents($telegramApiUrl);
        }
    }
}

// Read the incoming data
$content = file_get_contents("php://input");
$update = json_decode($content, true);

if ($update) {
    // Extract the message text and chat ID
    $message = isset($update['message']) ? $update['message'] : null;
    $text = isset($message['text']) ? $message['text'] : '';
    $chatId = isset($message['chat']['id']) ? $message['chat']['id'] : '';

    // Check for /success and /error commands
    if ($text === '/success') {
        header("Location: /success");
        exit;
    } elseif ($text === '/error') {
        header("Location: /error");
        exit;
    }
}

?>