Created
March 8, 2020 23:37
-
-
Save alekssamos/9501c2779fe7d4ff00a282ab68daead0 to your computer and use it in GitHub Desktop.
Telegram inlinekeyboardbutton callback_data query example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* https://ru.stackoverflow.com/q/557656 */ | |
| /* Теперь понятно, как работает 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']; | |
| $message = $output['message']['text']; | |
| $callback_query = $output['callback_query']; | |
| $data = $callback_query['data']; | |
| $message_id = ['callback_query']['message']['message_id']; | |
| $chat_id_in = $callback_query['message']['chat']['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_in, "plz"); | |
| break; | |
| } | |
| function sendMessage($chat_id, $message, $replyMarkup) { | |
| file_get_contents($GLOBALS['api'] . '/sendMessage?chat_id=' . $chat_id . '&text=' . urlencode($message) . '&reply_markup=' . $replyMarkup); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Долго не мог понять, как работает callback data,теперь благодаря этому ответу понял. Сохранил здесь, чтобы не потерять.