2017-08-24 1 views
0

Ich schreibe meine Telegramm-Bot-Codes in PHP. Ich möchte meine Inline-Tastatur in 2 oder 3 Spalten aufteilen. Hier ist mein Code:Telegramm Bot Split Tastatur Zeilen [Spalten]

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

aber mein Code funktioniert nicht. Wo ist das Problem und wie kann ich mein Problem lösen?

EDIT:

i verwendet nur diesen Code

$keyoptions = array_chunk($keyoptions,3); 

aber immer noch nicht das Problem finden kann;

Antwort

0

Ich weiß nicht, welche Bibliothek Sie verwenden, und was sind die Felder in Ihrem Code, aber dies eine Arbeit mit nativen Telegramm 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; 
}