【发布时间】:2016-07-15 19:55:52
【问题描述】:
我正在尝试使用 webhook 设置电报机器人。我可以让它与 getUpdates 一起使用,但我希望它与 webhook 一起使用。
我的网站(托管 bot php 脚本)的 SSL 证书有效(我在地址栏中获得了绿色锁):
我设置了 webhook
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;
}
?>
但是当我向机器人写任何东西时,我没有收到任何答案......
有什么想法吗?
谢谢
【问题讨论】:
标签: php webhooks telegram telegram-bot