内联键盘。我做了一个按钮,但是callback_data怎么办?
Iinline keyboard. I made a button, but what to do with the callback_data?
需要内联键盘方面的帮助。我做了一个按钮,但是回调怎么办?我知道我需要以某种方式获取 callback_data
并发布新消息。
<?php
$access_token = 'xxx';
$api = 'https://api.telegram.org/bot' . $access_token;
$output = json_decode(file_get_contents('php://input'), TRUE);
$chat_id = $output['message']['chat']['id'];
$first_name = $output['message']['chat']['first_name'];
$message = $output['message']['text'];
$callback_query = $output['callback_query'];
$data = $callback_query['data'];
$message_id = ['callback_query']['message']['message_id'];
switch($message) {
case '/test':
$inline_button1 = array("text"=>"Google url","url"=>"http://google.com");
$inline_button2 = array("text"=>"work plz","callback_data"=>'/plz');
$inline_keyboard = [[$inline_button1,$inline_button2]];
$keyboard=array("inline_keyboard"=>$inline_keyboard);
$replyMarkup = json_encode($keyboard);
sendMessage($chat_id, "ok", $replyMarkup);
break;
}
switch($data){
case '/plz':
sendMessage($chat_id, "plz");
break;
}
function sendMessage($chat_id, $message, $replyMarkup) {
file_get_contents($GLOBALS['api'] . '/sendMessage?chat_id=' . $chat_id . '&text=' . urlencode($message) . '&reply_markup=' . $replyMarkup);
}
如果有人单击该按钮,您将收到一个 callback_query 对象而不是消息对象。 callback_query 本身包含最初发送的消息。
$output = json_decode(file_get_contents('php://input'), TRUE);
$callback_query = $output["callback_query"]
$data = $callback_query["data"] // in your case $data is "/plz"
需要内联键盘方面的帮助。我做了一个按钮,但是回调怎么办?我知道我需要以某种方式获取 callback_data
并发布新消息。
<?php
$access_token = 'xxx';
$api = 'https://api.telegram.org/bot' . $access_token;
$output = json_decode(file_get_contents('php://input'), TRUE);
$chat_id = $output['message']['chat']['id'];
$first_name = $output['message']['chat']['first_name'];
$message = $output['message']['text'];
$callback_query = $output['callback_query'];
$data = $callback_query['data'];
$message_id = ['callback_query']['message']['message_id'];
switch($message) {
case '/test':
$inline_button1 = array("text"=>"Google url","url"=>"http://google.com");
$inline_button2 = array("text"=>"work plz","callback_data"=>'/plz');
$inline_keyboard = [[$inline_button1,$inline_button2]];
$keyboard=array("inline_keyboard"=>$inline_keyboard);
$replyMarkup = json_encode($keyboard);
sendMessage($chat_id, "ok", $replyMarkup);
break;
}
switch($data){
case '/plz':
sendMessage($chat_id, "plz");
break;
}
function sendMessage($chat_id, $message, $replyMarkup) {
file_get_contents($GLOBALS['api'] . '/sendMessage?chat_id=' . $chat_id . '&text=' . urlencode($message) . '&reply_markup=' . $replyMarkup);
}
如果有人单击该按钮,您将收到一个 callback_query 对象而不是消息对象。 callback_query 本身包含最初发送的消息。
$output = json_decode(file_get_contents('php://input'), TRUE);
$callback_query = $output["callback_query"]
$data = $callback_query["data"] // in your case $data is "/plz"