1

我正在尝试用 PHP 开发 Telegram Bot,但是当我按下内联按钮时,我的机器人无法回答用户。有人可以在调用方法后
帮我发送消息(方法)吗?sendMessageanswerCallback

这是我最后的试用代码:

if ($call_back_query != null) {
    
    $response = $call_back_query;
    $botUrl = "https://api.telegram.org/bot" . BOT_TOKEN . "/answerCallbackQuery";
    $postFields = array('callback_query_id' =>  $call_back_id, 'text' => $response);
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
    curl_setopt($ch, CURLOPT_URL, $botUrl); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    $output = curl_exec($ch);
    
    //send Text
    $response = "help me if you can, i'm feeling down";
    header("Content-Type: application/json");
    $parameters = array('chat_id' => $chatId, "text" => $response);
    $parameters["method"] = "sendMessage";
    echo json_encode($parameters);                
  }
4

1 回答 1

3

好的,我已经理解了这个问题:

sendText我必须使用$call_back_from而不是$chatId,因为他正在回答回调查询而不是简单的消息(我想是的......)

尽管

$chatId = isset($message['chat']['id']) ? $message['chat']['id'] : "";

$call_back_from = isset ($update['callback_query']['from']['id']) ? $update['callback_query']['from']['id'] : "";

代码将是

if ($call_back_query != null) {  
    $response = $call_back_query;
    $botUrl = "https://api.telegram.org/bot" . BOT_TOKEN . "/answerCallbackQuery";
    $postFields = array('callback_query_id' =>  $call_back_id, 'text' => $response);
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
    curl_setopt($ch, CURLOPT_URL, $botUrl); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    $output = curl_exec($ch);

    //send Text
    $response = "help me if you can, i'm feeling down";
    header("Content-Type: application/json");
    $parameters = array('chat_id' => $call_back_from, "text" => $response);
    $parameters["method"] = "sendMessage";
    echo json_encode($parameters);        
}

希望这会有所帮助!试一试!

于 2017-05-25T06:27:36.513 回答