我试图在PHP中开发Telegram Bot,但是当我按下内联按钮时,我无法让我的机器人回答用户。 有人帮我在调用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);
}
谢谢
答案 0 :(得分:0)
好的,我已经理解了这个问题:
在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);
}
希望这可能会有所帮助! 试一试!