mar*_*lov 21 api ssl telegram-bot
为什么我的webhook无法正常工作?我没有从电报bot API获得任何数据.这是我的问题的详细解释:
我从StartSSL获得SSL证书,它在我的网站上工作正常(根据GeoCerts SSL检查器),但似乎我的webhook到Telegram Bot API不起作用(尽管它说webhook设置我没有得到任何数据) .
我正在以这种形式在我的网站上制作一个webhook到我的脚本:
https://api.telegram.org/bot<token>/setWebhook?url=https://mywebsite.com/path/to/giveawaysbot.php
Run Code Online (Sandbox Code Playgroud)
我得到这个文本作为回应:
{"ok":true,"result":true,"description":"Webhook was set"}
Run Code Online (Sandbox Code Playgroud)
所以它必须有效,但实际上并非如此.
这是我的脚本代码:
<?php
ini_set('error_reporting', E_ALL);
$botToken = "<token>";
$website = "https://api.telegram.org/bot".$botToken;
$update = file_get_contents('php://input');
$update = json_decode($update);
print_r($update); // this is made to check if i get any data or not
$chatId = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
switch ($message) {
case "/test":
sendMessage($chatId,"test complete");
break;
case "/hi":
sendMessage($chatId,"hey there");
break;
default:
sendMessage($chatId,"nono i dont understand you");
}
function sendMessage ($chatId, $message) {
$url = $GLOBALS[website]."/sendMessage?chat_id=".$chatId."&text=".urlencode($message);
file_get_contents($url);
}
?>
Run Code Online (Sandbox Code Playgroud)
我实际上并没有收到任何数据到$ update.所以webhook无效.为什么?
Nig*_*ist 15
请再等一下,为什么你的 webhook 不起作用。
就我而言,原因是allowed_updates webhook 参数。
通过致电:
https://api.telegram.org/bot<your_bot_token>/getWebhookInfo
Run Code Online (Sandbox Code Playgroud)
你可以看到
{
"ok": true,
"result": {
"url": "<your webhook url should be here>",
"has_custom_certificate": false,
"pending_update_count": 0,
"max_connections": 40,
"allowed_updates": [
"callback_query"
]
}
}
Run Code Online (Sandbox Code Playgroud)
这意味着您的机器人无法对您的短信做出反应,并且您将不会收到任何网络钩子!
您可以注意到,“allowed_updates”包含数组。因此,目前它只会对内联按钮事件做出反应(作为键盘布局传递!)。根据setWebhook文档,allowed_updates是一个“可选”参数。
要开始接收短信,您需要将“message”添加到“allowed_updates”属性中。为此,只需再次设置您的 webhook 并将其添加到查询中即可。像这儿 :
https://api.telegram.org/bot<your_token>/setWebHook?url=<your_url>&allowed_updates=["callback_query","message"]
Run Code Online (Sandbox Code Playgroud)
您将收到类似“url 已添加”之类的信息,但不用担心,即使在这种情况下, allowed_updates 也会更新。只需尝试向机器人输入消息并测试您的网络钩子即可。
就这样,现在,telegram 会将网络钩子发送到从您到机器人的每条直接消息。希望,它对某人有帮助。
我遇到了这个问题.我试图到处寻找并找不到我的问题的解决方案,因为人们总是说问题是SSL证书.但是我发现了这个问题,而且代码中缺少很多与电报API webhook相互作用的东西,包括卷曲和这种东西.在我查看电报机器人文档的示例后,我解决了我的问题.看看这个例子https://core.telegram.org/bots/samples/hellobot
<?php
//telegram example
define('BOT_TOKEN', '12345678:replace-me-with-real-token');
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
function apiRequestWebhook($method, $parameters) {
if (!is_string($method)) {
error_log("Method name must be a string\n");
return false;
}
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
error_log("Parameters must be an array\n");
return false;
}
$parameters["method"] = $method;
header("Content-Type: application/json");
echo json_encode($parameters);
return true;
}
function exec_curl_request($handle) {
$response = curl_exec($handle);
if ($response === false) {
$errno = curl_errno($handle);
$error = curl_error($handle);
error_log("Curl returned error $errno: $error\n");
curl_close($handle);
return false;
}
$http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
curl_close($handle);
if ($http_code >= 500) {
// do not wat to DDOS server if something goes wrong
sleep(10);
return false;
} else if ($http_code != 200) {
$response = json_decode($response, true);
error_log("Request has failed with error {$response['error_code']}: {$response['description']}\n");
if ($http_code == 401) {
throw new Exception('Invalid access token provided');
}
return false;
} else {
$response = json_decode($response, true);
if (isset($response['description'])) {
error_log("Request was successfull: {$response['description']}\n");
}
$response = $response['result'];
}
return $response;
}
function apiRequest($method, $parameters) {
if (!is_string($method)) {
error_log("Method name must be a string\n");
return false;
}
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
error_log("Parameters must be an array\n");
return false;
}
foreach ($parameters as $key => &$val) {
// encoding to JSON array parameters, for example reply_markup
if (!is_numeric($val) && !is_string($val)) {
$val = json_encode($val);
}
}
$url = API_URL.$method.'?'.http_build_query($parameters);
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_TIMEOUT, 60);
return exec_curl_request($handle);
}
function apiRequestJson($method, $parameters) {
if (!is_string($method)) {
error_log("Method name must be a string\n");
return false;
}
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
error_log("Parameters must be an array\n");
return false;
}
$parameters["method"] = $method;
$handle = curl_init(API_URL);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_TIMEOUT, 60);
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($parameters));
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
return exec_curl_request($handle);
}
function processMessage($message) {
// process incoming message
$message_id = $message['message_id'];
$chat_id = $message['chat']['id'];
if (isset($message['text'])) {
// incoming text message
$text = $message['text'];
if (strpos($text, "/start") === 0) {
apiRequestJson("sendMessage", array('chat_id' => $chat_id, "text" => 'Hello', 'reply_markup' => array(
'keyboard' => array(array('Hello', 'Hi')),
'one_time_keyboard' => true,
'resize_keyboard' => true)));
} else if ($text === "Hello" || $text === "Hi") {
apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'Nice to meet you'));
} else if (strpos($text, "/stop") === 0) {
// stop now
} else {
apiRequestWebhook("sendMessage", array('chat_id' => $chat_id, "reply_to_message_id" => $message_id, "text" => 'Cool'));
}
} else {
apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'I understand only text messages'));
}
}
define('WEBHOOK_URL', 'https://my-site.example.com/secret-path-for-webhooks/');
if (php_sapi_name() == 'cli') {
// if run from console, set or delete webhook
apiRequest('setWebhook', array('url' => isset($argv[1]) && $argv[1] == 'delete' ? '' : WEBHOOK_URL));
exit;
}
$content = file_get_contents("php://input");
$update = json_decode($content, true);
if (!$update) {
// receive wrong update, must not happen
exit;
}
if (isset($update["message"])) {
processMessage($update["message"]);
}
?>
Run Code Online (Sandbox Code Playgroud)
小智 2
它可能是 SSL 证书。我遇到了同样的问题:Webhook 已确认,但实际上 SSL 证书已中断。
这个reddit线程很有帮助:https://www.reddit.com/r/Telegram/comments/3b4z1k/bot_api_receering_nothing_on_a_ Correctly/
归档时间: |
|
查看次数: |
25985 次 |
最近记录: |