batman batman - 1 year ago 317
PHP Question

Iinline keyboard. I made a button, but what to do with the callback_data?

Need help with inline keyboard. I made a button, but what to do with the callback? I understand I need to somehow get a

and issue a new message.

$access_token = 'xxx';
$api = '' . $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"=>"");
$inline_button2 = array("text"=>"work plz","callback_data"=>'/plz');
$inline_keyboard = [[$inline_button1,$inline_button2]];
$replyMarkup = json_encode($keyboard);
sendMessage($chat_id, "ok", $replyMarkup);
case '/plz':
sendMessage($chat_id, "plz");
function sendMessage($chat_id, $message, $replyMarkup) {
file_get_contents($GLOBALS['api'] . '/sendMessage?chat_id=' . $chat_id . '&text=' . urlencode($message) . '&reply_markup=' . $replyMarkup);

Answer Source

If someone clicks the button you will receive a callback_query object instead of a message object. The callback_query itself contains the originally sent message.

$callback_query = $output["callback_query"]
$data = $callback_query["data"] // in your case $data is "/plz"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download