2017-08-24 110 views
0

我正在用PHP编写我的电报机器人代码。我想将我的内联键盘分成2列或3列。这里是我的代码:电报机器人分裂键盘行[列]

foreach ($categories as $cat) { 
    $key[] = array(
     array('text'=>$cat['name'],'callback_data'=>'sub-'.$cat['id']) 
    ); 

    if ($k % 2 == 0) { 
     $keyoptions[] = $key; 
     $key = array(); 
    } 

    $k++; 
} 
$telegram->SendMessage($userid, $keyoptions); 

但我的代码不起作用。问题在哪里,我该如何解决我的问题?

编辑:

我只是用这个代码

$keyoptions = array_chunk($keyoptions,3); 

,但仍然无法找到的问题;

回答

0

我不知道你使用的是什么库,什么都在你的代码这些领域,但与当地电报API这样的工作:

function inlineKeyboard($text, $chatID, $btnNames, $callBackDatas) 
{ 
    $inlineRow = array(); // this is array for each row of buttons 
    $i = 0; 
    foreach ($btnNames as $name) { 
     array_push($inlineRow, array("text" => $name, "callback_data" => $callBackDatas[$i])); 
     $i++; 
    } 
    /* if you need multiple rows then just create other inlineRow arrays 
     and push to this array below */ 
    $inlineKeyboard = array($inlineRow); 

    $keyboard = array(
     "inline_keyboard" => $inlineKeyboard 
    ); 
    $postfields = array 
    (
     'chat_id' => "$chatID", 
     'text' => $text, 
     'reply_markup' => json_encode($keyboard) 
    ); 

    send('sendMessage', $postfields); 

} 


define('BaseURL', 'https://api.telegram.org/bot<TOKEN>'); 
function send($method, $datas) 
{ 
    $url = BaseURL . "/" . $method; 

    if (!$curld = curl_init()) { 
     exit; 
    } 
    curl_setopt($curld, CURLOPT_POST, true); 
    curl_setopt($curld, CURLOPT_POSTFIELDS, $datas); 
    curl_setopt($curld, CURLOPT_URL, $url); 
    curl_setopt($curld, CURLOPT_RETURNTRANSFER, true); 
    $output = curl_exec($curld); 
    curl_close($curld); 
    return $output; 
}