【问题标题】:Iinline keyboard. I made a button, but what to do with the callback_data?内联键盘。我做了一个按钮,但是如何处理 callback_data 呢?
【发布时间】:2017-01-02 03:06:44
【问题描述】:

需要内联键盘方面的帮助。我做了一个按钮,但是回调怎么办?我知道我需要以某种方式获得 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);
}

【问题讨论】:

    标签: php telegram telegram-bot php-telegram-bot


    【解决方案1】:

    如果有人单击该按钮,您将收到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"
    

    【讨论】:

    • 对不起,我不明白c message_id怎么办,我以为一定是这样,但是没有用。它可以是我的代码的一个例子,好吗? switch($message_id){ case '/plz': sendMessage($chat_id, "plz"); break; }
    • 您需要使用$data上的开关找到/plz
    • 不行,怎么回事?更新消息中的代码。
    • 您的$chat_id 未定义,因为$output['message'] 不存在。您需要从callback_query["message"]['chat']['id'] 中获取$chat_id
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多