2016-03-28 66 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