2017-08-24 1 views
0

J'écris mes codes de télégramme bot en PHP. Je voudrais diviser mon clavier en ligne en 2 ou 3 colonnes. Voici mon code:télégramme bot diviser les lignes de clavier [colonnes]

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); 

mais mon code ne fonctionne pas. Où est le problème et comment puis-je résoudre mon problème?

EDIT:

Je viens d'utiliser ce code

$keyoptions = array_chunk($keyoptions,3); 

mais ne peut toujours pas trouver le problème;

Répondre

0

Je ne sais pas ce que la bibliothèque que vous utilisez et quels sont les domaines dans votre code, mais un travail avec l'API de télégramme natif:

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; 
}