Search code examples
phptelegramtelegram-botphp-telegram-bot

how to make simple quiz and button answer in telegram bot with php?


I want to make a simple question and the answer in multiple choice, the result what i want is like this picture : this is picture for desired the multiple choice button
but the result is not like what i desired this is the picture for result from my code : this is picture for not desired result
and this is the code that i have tried :

<?php
$token = "##################################";
$website = "https://api.telegram.org/bot".$token;

$array = array(array("1","2","3"));
$btn = array("keyboard" => $array,"hide_keyboard" => true,'selective' => true,);
$reply = json_encode($btn);
$url = $website."/sendmessage?chat_id=$chatIdtext=select : &reply_markup=".$reply;
file_get_contents($url);
?>

Solution

  • try this code, maybe the result like what you want.

    <?php
    $token = "######################################";
    $website = "https://api.telegram.org/bot".$token;
    
    $name1 = 'das';
    $name2 = 'dsa';
    $placeId1 = '1';
    $placeId2 = '2';
    $keyboard = [
                'inline_keyboard' => [[['text' =>  $name1, 'callback_data' => $placeId1]], [['text' =>  $name2, 'callback_data' => $placeId2]]],
            ];
    
    $reply = json_encode($keyboard);
    $url = $website."/sendmessage?chat_id=130434215&text=select : &reply_markup=".$reply;
    file_get_contents($url);
    ?>