PHP Telegram BOT URL处理

q3qa4bjr  于 11个月前  发布在  PHP
关注(0)|答案(1)|浏览(137)

我想实现的功能:数据发送到电报后,用户必须发送/成功的机器人和网站将重定向到/成功页面,如果用户写/错误的电报机器人,网站将重定向到/错误页面。

<?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}");
    }
}

?>

字符串
我尝试过这个功能,它发送数据到电报,但当我写/成功机器人它不重定向我。

<?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;
    }
}

?>

3okqufwl

3okqufwl1#

要实现您所描述的功能,您需要在Telegram bot中处理命令(/success/error),然后使用这些命令在您的网站上触发重定向。以下是如何修改代码的示例:

<?php

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

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}");

        // Handle Telegram command
        if ($telegramCommand === '/success') {
            header("Location: /success");
            exit;
        } elseif ($telegramCommand === '/error') {
            header("Location: /error");
            exit;
        }
    }
}

?>

字符串
在这段代码中,我添加了一个新变量$telegramCommand来捕获从Telegram bot发送的命令。请确保在HTML表单中包含此变量作为隐藏输入字段:

<form action="your_php_script.php" method="post">
    <!-- your other form fields -->
    <input type="hidden" name="telegramCommand" value="">
    <button type="submit">Submit</button>
</form>


然后,在你的Telegram bot中,像这样处理/success/error命令:

// Handle incoming commands
if ($text === '/success') {
    // Perform any additional actions for success
} elseif ($text === '/error') {
    // Perform any additional actions for error
}


这样,当用户向bot发送/success/error时,bot将在表单中设置相应的命令,并且在提交表单时,PHP脚本将重定向用户到适当的页面。

相关问题