在论坛上进行了多次研究后,我决定写信看看您是否可以帮助我。
出于教育目的,我想用 PHP 创建机器人 Telegram 。 我阅读了它的文档,并在 CloudFlare 上为我的域创建了一个灵活的 SSL 证书。
我通过 Telegram 创建了我的机器人并收到了 token ,并使用以下代码设置了我的 webhook
https://api.telegram.org/bot <my token>? url = https: //miodominio.eu/page.php
答案是:
{"Ok": true, "result": false, "description": "Webhook was set"}
我把这段代码放在我的 page.php 中
<?php
function checkJSON($chatID,$update){
$myFile = "log.txt";
$updateArray = print_r($update,TRUE);
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, $chatID ."\n\n");
fwrite($fh, $updateArray."\n\n");
fclose($fh);
}
function sendMessage()
{
$message = "I am a baby bot.";
return $message;
}
define('BOT_TOKEN', '< mio token >');
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
// read incoming info and grab the chatID
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"];
// compose reply
$reply = sendMessage();
// send reply
$sendto =API_URL."sendmessage?chat_id=".$chatID."&text=".$reply;
file_get_contents($sendto);
checkJSON($chatID,$update);
?>
但没什么,如果我写入 Bootstrap ,我不会收到任何答复,也不会在日志文件中输入任何内容。 我该如何调试? 你有什么建议?? 提前感谢大家
最佳答案
我不确定这是否能解决您的问题,但我发现您的代码有两个问题。
要设置 webhook,您的 PHP 字符串应该是:
'https://api.telegram.org/bot<token>/setWebhook?url='.urlencode('https://miodominio.eu/page.php');
要发送消息,您的 PHP 字符串应该是:
API_URL."sendmessage?chat_id=".$chatID."&text=".urlencode($reply)
对于任何重要的参数,在将它们添加到查询字符串之前,您应该始终urlencode()
它们。
希望对您有所帮助。
关于PHP WebHook Telegram Bot - SSL CloudFlare,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33809008/