电报BOT Api在发送命令时不起作用

时间:2016-03-08 09:20:38

标签: php api telegram telegram-bot

我刚接到电报BOT Api,我只是设置了webhook,当我手动浏览我的网站链接时,我的机器人正在接收消息,但是当我从电报机器人发送命令或任何文本时,我没有收到服务器的任何回复

我在设置webhook时没有遇到任何问题,我的证书是自签名的,我还在设置webhook时上传了自签名证书,因为电报说要上传自签名(.PEM)证书,但我仍然没有收到当我从Telegram BOT发送命令或任何文本但手动冲浪正在工作时的任何消息

这是我的示例代码

<?php 
ini_set('error_reporting', E_ALL);
$botToken = "MY_TOKEN";
$website ="https://api.telegram.org/bot".$botToken;

$update=file_get_contents("php://input");
$update = json_decode($content, TRUE);

$chatID = $update["message"]["chat"]["id"];
$message =$update["message"]["text"];

switch ($message) {
    case "/test":
        sendMessage($chatID, "test");
        break;
    case "/cancel":
        sendMessage($chatID, "cancel");
        break;

    default:
        sendMessage($chatID, "default");
        break;
}

function sendMessage($chat_id, $msg){
    $url = $GLOBALS["website"]."/sendMessage?chat_id=".$chat_id."&text=".urlencode($msg);
    file_get_contents($url);
}

请帮助我,我犯了错误?

3 个答案:

答案 0 :(得分:0)

在sendMessage()函数中你已经写入了url sendMessage,而是尝试sendmessage(lowcase)

function sendMessage($chat_id, $msg){
    $url = $GLOBALS["website"]."/sendMessage?chat_id=".$chat_id."&text=".urlencode($msg);
    file_get_contents($url);
}

答案 1 :(得分:0)

这里必须改变::

之前:

$update=file_get_contents("php://input");
$update = json_decode($content, TRUE);

之后:

$update=file_get_contents("php://input");
$update = json_decode($update, TRUE);

答案 2 :(得分:0)

此代码正常工作

<?php 
ini_set('error_reporting', E_ALL);
$botToken = "MY_TOKEN";
$website ="https://api.telegram.org/bot".$botToken;

//$update=file_get_contents("php://input");
$content = file_get_contents("php://input"); <-----
$update = json_decode($content, TRUE);

$chatID = $update["message"]["chat"]["id"];
$message =$update["message"]["text"];

switch ($message) {
    case "/test":
        sendMessage($chatID, "test");
        break;
    case "/cancel":
        sendMessage($chatID, "cancel");
        break;

    default:
        sendMessage($chatID, "default");
        break;
}

function sendMessage($chat_id, $msg){
    $url = $GLOBALS["website"]."/sendMessage?chat_id=".$chat_id."&text=".urlencode($msg);
    file_get_contents($url);
}