电报PHP内联键盘

omvjsjqw  于 2023-01-01  发布在  PHP
关注(0)|答案(2)|浏览(209)

我尝试创建一个机器人,在接收文本“/start”时发送inline_keyboard,问题是我使用此函数发送键盘时看不到响应

function sendKeyboard($chat_id, $text) {
$keyboard = ['inline_keyboard' => [                 
                ['text':'Yes'],              
                ['text':'No']            
            ],
   'resize_keyboard' => true,           
   'one_time_keyboard' => true,      
   'selective' => true          
];
$keyboard = json_encode($keyboard);
$url = $GLOBALS[website] . "/sendMessage?chat_id=".$chat_id."&    
reply_markup=".$keyboard."&text=".urlencode($text);
file_get_contents($url);
}

有人知道如何解决这个问题吗?

k7fdbhmy

k7fdbhmy1#

内联键盘按钮是Button数组的数组,resize_keyboardone_time_keyboardselective不是内联键盘的参数,而是应答键盘的参数。
您的代码只有Button数组,并且Button只有text字段,需要添加callback_dataurl,否则会出错。
关于细节,你最好去看看参考资料.

c9qzyr3d

c9qzyr3d2#

function robot($method,$datas=[]){
  $url = "https://api.telegram.org/bot".API_KEY."/".$method;
  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  curl_setopt($ch,CURLOPT_POSTFIELDS,$datas);
  $res = curl_exec($ch);
  if(curl_error($ch)){
      var_dump(curl_error($ch));
  }else{
      return json_decode($res);
  }
}
robot('sendmessage', [
    "chat_id" => $chat_id,
    'message_id'=>$messageid,
    "text" => "* buttonwith out link *
    ",
    'reply_markup' => json_encode([
        "one_time_keyboard" => true,
        'inline_keyboard'=> [
             [
['text' => "button 1", 'callback_data' => "buttoncode-39500"]
]
]
])
]);

相关问题