2016-03-28 165 views
2

我想用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; 
} 

?> 

但是,當我寫什麼我沒有收到答覆的機器人......

任何想法,爲什麼?

謝謝

回答

2

在你的問題它不清楚腳本的位置。看到您的代碼,似乎您嘗試通過url_get_contents加載請求以檢索電報服務器響應。如果您的機器人工作,這是正確的方法沒有 webhook。否則,在設置webhook之後,您必須處理傳入的請求。

即,如果你設置網絡掛接到https://example.com/mywebhook.php,在你https://example.com/mywebhook.php腳本,你必須寫這樣的事:

+0

感謝您的幫助。我按照你的例子設置了webhook,並且讓我改變了你的建議,但是bot仍然像以前一樣保持沉默。 – firefreeman

+0

如果你知道你自己的聊天ID,做一些測試發送消息給你。你確定webhook設置正確嗎? – fusion3k

+0

抓住了聊天ID,現在嘗試 – firefreeman