我正在尝试用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_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);
}
希望这会有所帮助!试一试!