我想实现的功能:数据发送到电报后,用户必须发送/成功的机器人和网站将重定向到/成功页面,如果用户写/错误的电报机器人,网站将重定向到/错误页面。
<?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;
}
}
?>
型
1条答案
按热度按时间3okqufwl1#
要实现您所描述的功能,您需要在Telegram bot中处理命令(
/success
和/error
),然后使用这些命令在您的网站上触发重定向。以下是如何修改代码的示例:字符串
在这段代码中,我添加了一个新变量
$telegramCommand
来捕获从Telegram bot发送的命令。请确保在HTML表单中包含此变量作为隐藏输入字段:型
然后,在你的Telegram bot中,像这样处理
/success
和/error
命令:型
这样,当用户向bot发送
/success
或/error
时,bot将在表单中设置相应的命令,并且在提交表单时,PHP脚本将重定向用户到适当的页面。