我想用webhook建立一個電報bot。我可以讓它與getUpdates一起工作,但我希望它可以使用webhook。電報webhook php bot不回答
我的網站(承載機器人PHP腳本)擁有SSL證書的工作(我得到在地址欄綠色鎖定):
我成立了網絡掛接與
https://api.telegram.org/bot<token>/setwebhook?url=https://www.example.com/bot/bot.php
我得到:{「ok」:true,「result」:true,「description」:「Webhook已設置」}
(我不知道這是否重要,但我已賦予文件夾和rwx權限劇本)
PHP的機器人:(https://www.example.com/bot/bot.php)
<?php
$botToken = <token>;
$website = "https://api.telegram.org/bot".$botToken;
#$update = url_get_contents('php://input');
$update = file_get_contents('php://input');
$update = json_decode($update, TRUE);
$chatId = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
switch($message) {
case "/test":
sendMessage($chatId, "test");
break;
case "/hi":
sendMessage($chatId, "hi there!");
break;
default:
sendMessage($chatId, "default");
}
function sendMessage ($chatId, $message) {
$url = $GLOBALS[website]."/sendMessage?chat_id=".$chatId."&text=".urlencode($message);
url_get_contents($url);
}
function url_get_contents($Url) {
if(!function_exists('curl_init')) {
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
?>
但是,當我寫什麼我沒有收到答覆的機器人......
任何想法,爲什麼?
謝謝
感謝您的幫助。我按照你的例子設置了webhook,並且讓我改變了你的建議,但是bot仍然像以前一樣保持沉默。 – firefreeman
如果你知道你自己的聊天ID,做一些測試發送消息給你。你確定webhook設置正確嗎? – fusion3k
抓住了聊天ID,現在嘗試 – firefreeman