Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在电报中应答回拨查询后发送消息?_Php_Telegram_Telegram Bot_Php Telegram Bot - Fatal编程技术网

Php 如何在电报中应答回拨查询后发送消息?

Php 如何在电报中应答回拨查询后发送消息?,php,telegram,telegram-bot,php-telegram-bot,Php,Telegram,Telegram Bot,Php Telegram Bot,我正试图用PHP开发一个电报机器人,但当用户按下内联按钮时,我没能让我的机器人回答用户。 在调用answerCallback方法后,有人能帮我发送消息(sendMessage方法)吗 这是我最后的试用代码: if ($call_back_query != null) { $response = $call_back_query; $botUrl = "https://api.telegram.org/bot" . BOT_TOKEN . "

我正试图用PHP开发一个电报机器人,但当用户按下内联按钮时,我没能让我的机器人回答用户。
在调用
answerCallback
方法后,有人能帮我发送消息(
sendMessage
方法)吗

这是我最后的试用代码:

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);                
  }

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

sendText
中,我必须使用
$call\u back\u from
而不是
$chatId
,因为他回答的是回调查询,而不是简单的消息(我想是这样的…)

$chatId=isset($message['chat']['id'])$消息['chat']['id']:“”

$call\u back\u from=isset($update['callback\u query']['from']['id'])$更新['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);        
}
希望这能有所帮助! 试试看