<?php
define('BOT_TOKEN', '8291470736:AAHMwfbO6ABPoe3OtzRujXmaLzXJaMPphIQ'); // Replace with your actual bot token
define('WEBHOOK_URL', $urlpoin.'/myWebhook.php'); // Replace with your HTTPS URL

/*
///////////////////////////////////////////////////////////////////////////////////////////
$api_url = 'https://api.telegram.org/bot' . BOT_TOKEN . '/setWebhook?url=' . WEBHOOK_URL;
$response = file_get_contents($api_url);

if ($response) {
    echo "Webhook successfully set up!";
} else {
    echo "Failed to set up webhook.";
}

*/
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
$urlsoal = "https://isekolah.my.id/myWebhook/soal/";
$urlpoin= 'https://isekolah.my.id/myWebhook';
$update = json_decode(file_get_contents('php://input'), true);
define('chat_id', $update['message']['chat']['id']); // Replace with your HTTPS URL
define('text', $update['message']['text']); // Replace with your HTTPS URL

$chat_id = $update['message']['chat']['id'];
$chatcq_id = $update['callback_query']['message']['chat']['id'];
//sendMessage($chatcq_id,  json_encode($update['callback_query']));
$text = $update['message']['text'];
// include database file
include_once 'mongodb_config.php';
$db = new DbManager();
$conn = $db->getConnection();
$dbname="myWebhook";
$collection_user="user";
$collection_soal="soal";
$collection_kunci="kunci";
$chat_id_admin="304835260";

    $filter1 = ['id' => (string)$chat_id_admin,];
	//$options = ['projection' => ['_id' => 0.0]];
    $options1 = ['projection' => []];
	$read1 = new MongoDB\Driver\Query($filter1, $options1);
	$records1 = $conn->executeQuery("$dbname.$collection_user", $read1);
 	$res1= json_encode(iterator_to_array($records1)); //echo  
    $result1= json_decode($res1, true); 
    

    echo "1)";
 	echo $jsonString1 = json_encode($result1[0]);  echo "\n";echo "\n";//1){"_id":{"$oid":"69532ce57b3df17e1e0c73d2"},"id":"304835260","nama":"nzmz","kelas":"","role":"siswa","edit":0,"delete":0,"select":0}
	echo "2)";
    echo $str1 = base64_encode($jsonString1); echo "\n";echo "\n";    // 2)eyJfaWQiOnsiJG9pZCI6IjY5NTMyY2U1N2IzZGYxN2UxZTBjNzNkMiJ9LCJpZCI6IjMwNDgzNTI2MCIsIm5hbWEiOiJuem16Iiwia2VsYXMiOiIiLCJyb2xlIjoic2lzd2EiLCJlZGl0IjowLCJkZWxldGUiOjAsInNlbGVjdCI6MH0=
    echo "3)";
    echo $count1= json_encode(count($result1));  echo "\n";echo "\n";//3)6
    echo "4)";
    echo $oidValue1 = $result1[0]['_id']['$oid']; echo "\n";echo "\n"; //4)69532ce57b3df17e1e0c73d2
    echo "5)";
    echo $nm1 = $result1[0]['nama']; // 5)nzmz
    //    var_dump($result1[0]['nama']) ;

    echo "\n";echo "\n";echo "6)";



    $filter2 = ['id' => (string)$chat_id,];
	//$options = ['projection' => ['_id' => 0.0]];
     $options2 = ['projection' => []];
	$read2 = new MongoDB\Driver\Query($filter2, $options2);
	$records2 = $conn->executeQuery("$dbname.$collection_user", $read2);
 	$res2= json_encode(iterator_to_array($records2)); //echo  
    $result2= json_decode($res2, true); 
    

    
 	 $jsonString2 = json_encode($result2[0]);  //1){"_id":{"$oid":"69532ce57b3df17e1e0c73d2"},"id":"304835260","nama":"nzmz","kelas":"","role":"siswa","edit":0,"delete":0,"select":0}
	
    $str2 = base64_encode($jsonString2);    // 2)eyJfaWQiOnsiJG9pZCI6IjY5NTMyY2U1N2IzZGYxN2UxZTBjNzNkMiJ9LCJpZCI6IjMwNDgzNTI2MCIsIm5hbWEiOiJuem16Iiwia2VsYXMiOiIiLCJyb2xlIjoic2lzd2EiLCJlZGl0IjowLCJkZWxldGUiOjAsInNlbGVjdCI6MH0=
  
    $count2= json_encode(count($result2));  //3)6
  
    $oidValue2= $result2[0]['_id']['$oid'];  //4)69532ce57b3df17e1e0c73d2
 
    $nm2 = $result2[0]['nama']; // 5)nama
    $kls2 = $result2[0]['kelas'];
// sendMessage($chat_id, 'oid user='.$oidValue2 );
//sendMessage($chat_id,  $jsonString2);

    $filter3 = [];
	//$options = ['projection' => ['_id' => 0.0]];
     $options3 = ['projection' => []];
	$read3 = new MongoDB\Driver\Query($filter3, $options3);
	$records3 = $conn->executeQuery("$dbname.$collection_kunci", $read3);
 	$res3= json_encode(iterator_to_array($records3)); //echo  
    $result3= json_decode($res3, true); 
       
 	 $jsonString3 = json_encode($result3[0]);  //1){"_id":{"$oid":"69532ce57b3df17e1e0c73d2"},"id":"304835260","nama":"nzmz","kelas":"","role":"siswa","edit":0,"delete":0,"select":0}
	
    $str3 = base64_encode($jsonString3);    // 2)eyJfaWQiOnsiJG9pZCI6IjY5NTMyY2U1N2IzZGYxN2UxZTBjNzNkMiJ9LCJpZCI6IjMwNDgzNTI2MCIsIm5hbWEiOiJuem16Iiwia2VsYXMiOiIiLCJyb2xlIjoic2lzd2EiLCJlZGl0IjowLCJkZWxldGUiOjAsInNlbGVjdCI6MH0=
  
     $count3= json_encode(count($result3));  //3)6
  
     $oidValue3= $result3[0]['_id']['$oid'];  //4)69532ce57b3df17e1e0c73d2
 
      $pel3 = $result3[0]['pel']; // 5)nama
      $kls = $result3[0]['jekas'];
$mykode = $result3[0]['kode'];
//sendMessage($chat_id, $nama );
//sendMessage($chat_id,  $jsonString3);

//////////////////////////////////////////////////////////
    $filter4 = ['id' => (string)$chat_id,];
 
	//$options = ['projection' => ['_id' => 0.0]];
     $options4 = ['projection' => []];
	$read4 = new MongoDB\Driver\Query($filter4, $options4);
	$records4 = $conn->executeQuery("$dbname.$collection_soal", $read4);
 	$res4= json_encode(iterator_to_array($records4)); //echo  
    $result4= json_decode($res4, true); 
    

    
 	 $jsonString4 = json_encode($result4[0]);  //1){"_id":{"$oid":"69532ce57b3df17e1e0c73d2"},"id":"304835260","nama":"nzmz","kelas":"","role":"siswa","edit":0,"delete":0,"select":0}
	
    $str4 = base64_encode($jsonString4);    // 2)eyJfaWQiOnsiJG9pZCI6IjY5NTMyY2U1N2IzZGYxN2UxZTBjNzNkMiJ9LCJpZCI6IjMwNDgzNTI2MCIsIm5hbWEiOiJuem16Iiwia2VsYXMiOiIiLCJyb2xlIjoic2lzd2EiLCJlZGl0IjowLCJkZWxldGUiOjAsInNlbGVjdCI6MH0=
  
     $count4= json_encode(count($result4));  //3)6
     $sesi4= json_encode(count($result4));  //3)6
     $sesi0 =$sesi+1;
 // sendMessage($chat_id, 'sesi4='.$sesi4 );


     $oidValue4= $result4[0]['_id']['$oid'];  //4)69532ce57b3df17e1e0c73d2
 
     $b4 = $result4[0]['01']+$result4[0]['02']+$result4[0]['03']+$result4[0]['04']+$result4[0]['05']+$result4[0]['06']+$result4[0]['07']+$result4[0]['08']+$result4[0]['09']+$result4[0]['10']; // 5)nama
// $b4 = $result4[0]['01']+$result4[0]['02']+$result4[0]['03']+$result4[0]['04']+$result4[0]['05']+$result4[0]['06']+$result4[0]['07']+$result4[0]['08']+$result4[0]['09']+$result4[0]['10']; // 5)nama


      //////////////////////////////////////////////////////////////////////////////////////////
$filter41 = ['id' => (string)$chat_id,'sesi' => (string)$sesi5];
 
	//$options = ['projection' => ['_id' => 0.0]];
     $options41 = ['projection' => []];
	$read41 = new MongoDB\Driver\Query($filter41, $options41);
	$records41 = $conn->executeQuery("$dbname.$collection_soal", $read41);
 	$res41= json_encode(iterator_to_array($records41)); //echo  
    $result41= json_decode($res41, true);   
 	//  $jsonString41 = json_encode($result41[0]);  //1){"_id":{"$oid":"69532ce57b3df17e1e0c73d2"},"id":"3041835260","nama":"nzmz","kelas":"","role":"siswa","edit":0,"delete":0,"select":0}
    // $str41 = base641_encode($jsonString41);    // 2)eyJfaWQiOnsiJG9pZCI6IjY5NTMyY2U1N2IzZGYxN2UxZTBjNzNkMiJ9LCJpZCI6IjMwNDgzNTI2MCIsIm5hbWEiOiJuem16Iiwia2VsYXMiOiIiLCJyb2xlIjoic2lzd2EiLCJlZGl0IjowLCJkZWxldGUiOjAsInNlbGVjdCI6MH0=
    //  $count41= json_encode(count($result41));  //3)6
    //  $sesi41= json_encode(count($result41));  //3)6
    //  $oidValue41= $result41[0]['_id']['$oid'];  //41)69532ce57b3df17e1e0c73d2
 
     $b41 = $result41[0]['01']+$result41[0]['02']+$result41[0]['03']+$result41[0]['041']+$result41[0]['05']+$result41[0]['06']+$result41[0]['07']+$result41[0]['08']+$result41[0]['09']+$result41[0]['10']; // 5)nama
// $b41 = $result4[0]['01']+$result4[0]['02']+$result4[0]['03']+$result4[0]['04']+$result4[0]['05']+$result4[0]['06']+$result4[0]['07']+$result4[0]['08']+$result4[0]['09']+$result4[0]['10']; // 5)nama

//////////////////////////////////////////////////////////

if($update['callback_query']){
    $chatcq_id = $update['callback_query']['message']['chat']['id'];
    // sendMessage($chatcq_id,  json_encode($update['callback_query']));
    //sendMessage($chatcq_id,  json_encode($chatcq_id));
//user
    $filter2 = ['id' => (string)$chatcq_id,];
	//$options = ['projection' => ['_id' => 0.0]];
     $options2 = ['projection' => []];
	$read2 = new MongoDB\Driver\Query($filter2, $options2);
	$records2 = $conn->executeQuery("$dbname.$collection_user", $read2);
 	$res2= json_encode(iterator_to_array($records2)); //echo  
    $result2= json_decode($res2, true); 
    

    
 	 $jsonString2 = json_encode($result2[0]);  //1){"_id":{"$oid":"69532ce57b3df17e1e0c73d2"},"id":"304835260","nama":"nzmz","kelas":"","role":"siswa","edit":0,"delete":0,"select":0}
	
    $str2 = base64_encode($jsonString2);    // 2)eyJfaWQiOnsiJG9pZCI6IjY5NTMyY2U1N2IzZGYxN2UxZTBjNzNkMiJ9LCJpZCI6IjMwNDgzNTI2MCIsIm5hbWEiOiJuem16Iiwia2VsYXMiOiIiLCJyb2xlIjoic2lzd2EiLCJlZGl0IjowLCJkZWxldGUiOjAsInNlbGVjdCI6MH0=
  
    $count2= json_encode(count($result2));  //3)6
  
    $oidValue2= $result2[0]['_id']['$oid'];  //4)69532ce57b3df17e1e0c73d2
 
    $nm2 = $result2[0]['nama']; // 5)nama
    $kls2 = $result2[0]['kelas'];

//soal
    $filter5 = ['id' => (string)$chatcq_id,];
 	//$options = ['projection' => ['_id' => 0.0]];
     $options5 = ['projection' => []];
	$read5 = new MongoDB\Driver\Query($filter5, $options5);
	$records5 = $conn->executeQuery("$dbname.$collection_soal", $read5);
 	$res5= json_encode(iterator_to_array($records5)); //echo  
    $result5= json_decode($res5, true); 
    

    
 	 $jsonString5 = json_encode($result5[0]);  //1){"_id":{"$oid":"69532ce57b3df17e1e0c73d2"},"id":"305835260","nama":"nzmz","kelas":"","role":"siswa","edit":0,"delete":0,"select":0}
	
    // $str5 = base65_encode($jsonString5);    // 2)eyJfaWQiOnsiJG9pZCI6IjY5NTMyY2U1N2IzZGYxN2UxZTBjNzNkMiJ9LCJpZCI6IjMwNDgzNTI2MCIsIm5hbWEiOiJuem16Iiwia2VsYXMiOiIiLCJyb2xlIjoic2lzd2EiLCJlZGl0IjowLCJkZWxldGUiOjAsInNlbGVjdCI6MH0=
  
     $count5= json_encode(count($result5));  //3)6
     $sesi5= json_encode(count($result5));  //3)6
    //  sendMessage($chatcq_id,  $jsonString5);
    // sendMessage($chatcq_id, 'sesi5='.$sesi5 );


     $oidValue5= $result5[0]['_id']['$oid'];  //5)69532ce57b3df17e1e0c73d2
 
     //    $b5 = $result5[0]['01']+$result5[0]['02']+$result5[0]['03']+$result5[0]['05']+$result5[0]['05']+$result5[0]['06']+$result5[0]['07']+$result5[0]['08']+$result5[0]['09']+$result5[0]['10']; // 5)nama
     $b5 = $result5[0]['01']+$result5[0]['02']+$result5[0]['03']+$result5[0]['05']+$result5[0]['05']+$result5[0]['06']+$result5[0]['07']+$result5[0]['08']+$result5[0]['09']+$result5[0]['10']; // 5)nama
}
      //////////////////////////////////////////////////////////////////////////////////////////
   //////////////////////////////////////////////////////////////////////////////////////////
$filter41 = ['id' => (string)$chatcq_id,'sesi' => (string)$sesi5];
 
	//$options = ['projection' => ['_id' => 0.0]];
     $options41 = ['projection' => []];
	$read41 = new MongoDB\Driver\Query($filter41, $options41);
	$records41 = $conn->executeQuery("$dbname.$collection_soal", $read41);
 	$res41= json_encode(iterator_to_array($records41)); //echo  
    $result41= json_decode($res41, true);   
 	 $jsonString41 = json_encode($result41[0]);  //1){"_id":{"$oid":"69532ce57b3df17e1e0c73d2"},"id":"3041835260","nama":"nzmz","kelas":"","role":"siswa","edit":0,"delete":0,"select":0}
    // $str41 = base641_encode($jsonString41);    // 2)eyJfaWQiOnsiJG9pZCI6IjY5NTMyY2U1N2IzZGYxN2UxZTBjNzNkMiJ9LCJpZCI6IjMwNDgzNTI2MCIsIm5hbWEiOiJuem16Iiwia2VsYXMiOiIiLCJyb2xlIjoic2lzd2EiLCJlZGl0IjowLCJkZWxldGUiOjAsInNlbGVjdCI6MH0=
    //  $count41= json_encode(count($result41));  //3)6
    //  $sesi41= json_encode(count($result41));  //3)6
    //  $oidValue41= $result41[0]['_id']['$oid'];  //41)69532ce57b3df17e1e0c73d2
 $user41 = $result41[0]['user'];
     $b41 = $result41[0]['01']+$result41[0]['02']+$result41[0]['03']+$result41[0]['041']+$result41[0]['05']+$result41[0]['06']+$result41[0]['07']+$result41[0]['08']+$result41[0]['09']+$result41[0]['10']; // 5)nama
// $b41 = $result4[0]['01']+$result4[0]['02']+$result4[0]['03']+$result4[0]['04']+$result4[0]['05']+$result4[0]['06']+$result4[0]['07']+$result4[0]['08']+$result4[0]['09']+$result4[0]['10']; // 5)nama
//sendMessage($chatcq_id,  $jsonString41);
     
        $mysesi = $sesi4;
      
        $mysesi = $sesi5;
      
      
//sendMessage($chat_id,  $b4);

//insert_dt();
 function insert_dt() {
        
    global $conn, $dbname, $chat_id,$nm2, $kls2,$text ,$collection_soal,$mysesi, $chatcq_id;


    $pecah = (explode(',',$text));
        $_txt0= $pecah['0'];
        $_txt1= $pecah['1'];
        $txt2= $pecah['2'];

    $data1 = ['id' => (string)$chatcq_id,
                'nama' => $nm2,
                'kelas' => $kls2, 
            'user' => $_txt1,
            'sesi' => (string)$mysesi
    ];

    sendMessage($chatcq_id, json_encode($data1));
    $insert = new MongoDB\Driver\BulkWrite();
    $insert->insert($data1);

    $result = $conn->executeBulkWrite("$dbname.$collection_soal", $insert);

    // verify
    if ($result->getInsertedCount() == 1) {
        echo json_encode(
            array("message" => "Record successfully created")
        );
       
         sendMessage($chat_id, "Record successfully created");
    } else {
        echo json_encode(
                array("message" => "Error while saving record")
        );
         sendMessage($chat_id, "Error while saving record");
    }

}

function inserts_dt() {
        
    global $conn, $dbname, $chat_id,$nm2, $kls2,$text ,$collection_soal,$sesi4;


    $pecah = (explode(',',$text));
        $_txt1= $pecah['1'];

    $data1 = [  'id' => (string)$chat_id,
                'nama' => $nm2,
                'kelas' => $kls2, 
                'user' => $_txt1,
                'sesi' => (string)($sesi4+1)
    ];

    //sendMessage($chat_id, json_encode($data1));
    $insert = new MongoDB\Driver\BulkWrite();
    $insert->insert($data1);
    $result = $conn->executeBulkWrite("$dbname.$collection_soal", $insert);

    // verify
    if ($result->getInsertedCount() == 1) {
        echo json_encode(
            array("message" => "Record successfully created")
        );
       
        // sendMessage($chat_id, "Record successfully created");
    } else {
        echo json_encode(
                array("message" => "Error while saving record")
        );
       //  sendMessage($chat_id, "Error while saving record");
    }

}



////////////////////////////////////////soal/////////////////////////////////////////sendMessage($chat_id, 'oid soalr='//.$oidValue3 );
   $callback_data = $update['callback_query']['data'];
$chat_idq = $update['callback_query']['message']['chat']['id'];
$message_id = $update['callback_query']['message']['message_id'];

$pecah = (explode(',',$text));
     $_txt0= $pecah['0'];
     $_txt1= $pecah['1'];
     $txt2= $pecah['2'];
//sendMessage($chat_id, $_txt0.$_txt1 );


if ($_txt0 == $result3[0]['kode'] and ($count4 >= $result3[0]['kali'])) {
  inserts_dt();
    // sendMessage($chat_id, 'mulai sesi '.$sesi);
   //sendMessage($chat_id, $_txt0.$_txt1 );
/////////////////////////////
        //sendMessage($chat_id, "Hello!Selamat Datang to your bot.");
        $url_img = $urlsoal.'selamat.png'; // Mendapatkan file_id
        //$chat_id = $update['message']['chat']['id'];
        $caption1 = "Mata Pelajaran : ".$pel3 ;
        $inlineKeyboard = [
                [
                    ['text' => 'Mulai', 'callback_data' => 'no1']
                    
                ],
                // Second row of buttons (optional)
                [
                    ['text' => 'Petunjuk', 'url' => 'https://www.google.com'] // URL button example
                ] 
        ];
        sendpoto($chat_id,$url_img,$inlineKeyboard,$caption1);
        }



if(preg_match('/^no1/', $callback_data ) ){  
      sendMessage($chatcq_id, 'mulai sesi '.$sesi5 .  ' , User : '.$user41);
//insert_dt();
$url_img = $urlsoal.'1.JPG'; // Mendapatkan file_id
$caption1 = "no1";
$inlineKeyboard = [
         [
            ['text' => 'A BENAR', 'callback_data' => '01a'],
            ['text' => 'D BENAR', 'callback_data' => '01d']         
        ],
        [
            ['text' => 'B BENAR', 'callback_data' => '01b'],
            ['text' => 'E BENAR', 'callback_data' => '01e']
        ],
        [
            ['text' => 'C BENAR', 'callback_data' => '01c'],
            ['text' => 'Semua Salah', 'callback_data' => '01s']
        ] 
];

//if( $count4 <= $result3[0]['kali']){
    
     //   }
sendpoto($chat_idq,$url_img,$inlineKeyboard,$caption1);
// //sendMessage($chat_idq, 'sesi='.$sesi5);
 // insert_dt() ;


// }
}



if(preg_match('/^01/', $callback_data)){  
$url_img = $urlsoal.'2.JPG'; // Mendapatkan file_id
$caption1 = "no2";
$inlineKeyboard = [
         [
            ['text' => 'A BENAR', 'callback_data' => '02a'],
            ['text' => 'D BENAR', 'callback_data' => '02d']         
        ],
        [
            ['text' => 'B BENAR', 'callback_data' => '02b'],
            ['text' => 'E BENAR', 'callback_data' => '02e']
        ],
        [
            ['text' => 'C BENAR', 'callback_data' => '02c'],
            ['text' => 'Semua Salah', 'callback_data' => '02s']
        ] 
];
// sendMessage($chat_idq, $chat_idq);
// sendMessage($chat_idq, "jno1=".$callback_data);
// sendMessage($chat_idq, "kno1=".$result3[0]['01']);
sendpoto($chat_idq,$url_img,$inlineKeyboard,$caption1);

if($callback_data == $result3[0]['01']){
$data1 = ['01' => 1];
sendMessage($chat_idq, 'benar');
}else{
$data1 = ['01' => 0]; 
sendMessage($chat_idq, 'salah');
}

//sendMessage($chat_idq, 'sesi='.$sesi5);

 updateid_db($chat_idq, $data1, $dbname,$collection_soal,$sesi5);

}

if(preg_match('/^02/', $callback_data)){   
$url_img = $urlsoal.'3.JPG'; // Mendapatkan file_id
$caption1 = "no3";
$inlineKeyboard = [
         [
            ['text' => 'A BENAR', 'callback_data' => '03a'],
            ['text' => 'D BENAR', 'callback_data' => '03d']         
        ],
        [
            ['text' => 'B BENAR', 'callback_data' => '03b'],
            ['text' => 'E BENAR', 'callback_data' => '03e']
        ],
        [
            ['text' => 'C BENAR', 'callback_data' => '03c'],
            ['text' => 'Semua Salah', 'callback_data' => '03s']
        ] 
];
// sendMessage($chat_idq, "jno2=".$callback_data);
// sendMessage($chat_idq, "kno2=".$result3[0]['02']);
sendpoto($chat_idq,$url_img,$inlineKeyboard,$caption1);
if($callback_data == $result3[0]['02']){
$data1 = ['02' => 1];
sendMessage($chat_idq, 'benar');
}else{
$data1 = ['02' => 0]; 
sendMessage($chat_idq, 'salah');
}
//sendMessage($chat_idq, 'sesi'.$sesi5);
 updateid_db($chat_idq, $data1, $dbname,$collection_soal,$sesi5);
}


if(preg_match('/^03/', $callback_data)){  
$url_img = $urlsoal.'4.JPG'; // Mendapatkan file_id
$caption1 = "no4";
$inlineKeyboard = [
         [
            ['text' => 'A BENAR', 'callback_data' => '04a'],
            ['text' => 'D BENAR', 'callback_data' => '04d']         
        ],
        [
            ['text' => 'B BENAR', 'callback_data' => '04b'],
            ['text' => 'E BENAR', 'callback_data' => '04e']
        ],
        [
            ['text' => 'C BENAR', 'callback_data' => '04c'],
            ['text' => 'Semua Salah', 'callback_data' => '04s']
        ] 
];
// sendMessage($chat_idq, "jno3=".$callback_data);
// sendMessage($chat_idq, "kno3=".$result3[0]['03']);
sendpoto($chat_idq,$url_img,$inlineKeyboard,$caption1);
 if($callback_data == $result3[0]['03']){
$data1 = ['03' => 1];
sendMessage($chat_idq, 'benar');
}else{
$data1 = ['03' => 0]; 
sendMessage($chat_idq, 'salah');
}
//sendMessage($chat_idq, 'sesi='.$sesi5);
 updateid_db($chat_idq, $data1, $dbname,$collection_soal,$sesi5);
}


if(preg_match('/^04/', $callback_data)){   
$url_img = $urlsoal.'5.JPG'; // Mendapatkan file_id
$caption1 = "no5";
$inlineKeyboard = [
         [
            ['text' => 'A BENAR', 'callback_data' => '05a'],
            ['text' => 'D BENAR', 'callback_data' => '05d']         
        ],
        [
            ['text' => 'B BENAR', 'callback_data' => '05b'],
            ['text' => 'E BENAR', 'callback_data' => '05e']
        ],
        [
            ['text' => 'C BENAR', 'callback_data' => '05c'],
            ['text' => 'Semua Salah', 'callback_data' => '05s']
        ] 
];
// sendMessage($chat_idq, "jno4=".$callback_data);
// sendMessage($chat_idq, "kno4=".$result3[0]['04']);
sendpoto($chat_idq,$url_img,$inlineKeyboard,$caption1);
if($callback_data == $result3[0]['04']){
$data1 = ['04' => 1];
sendMessage($chat_idq, 'benar');
}else{
$data1 = ['04' => 0]; 
sendMessage($chat_idq, 'salah');
}
//sendMessage($chat_idq, 'sesi='.$sesi5);
 updateid_db($chat_idq, $data1, $dbname,$collection_soal,$sesi5);
}


if(preg_match('/^05/', $callback_data)){   
$url_img = $urlsoal.'6.JPG'; // Mendapatkan file_id
$caption1 = "no6";
$inlineKeyboard = [
         [
            ['text' => 'A BENAR', 'callback_data' => '06a'],
            ['text' => 'D BENAR', 'callback_data' => '06d']         
        ],
        [
            ['text' => 'B BENAR', 'callback_data' => '06b'],
            ['text' => 'E BENAR', 'callback_data' => '06e']
        ],
        [
            ['text' => 'C BENAR', 'callback_data' => '06c'],
            ['text' => 'Semua Salah', 'callback_data' => '06s']
        ] 
];
// sendMessage($chat_idq, "jno5=".$callback_data);
// sendMessage($chat_idq, "kno5=".$result3[0]['05']);
sendpoto($chat_idq,$url_img,$inlineKeyboard,$caption1);
if($callback_data == $result3[0]['05']){
$data1 = ['05' => 1];
sendMessage($chat_idq, 'benar');
}else{
$data1 = ['05' => 0]; 
sendMessage($chat_idq, 'salah');
}
//sendMessage($chat_idq, 'sesi='.$sesi5);
updateid_db($chat_idq, $data1, $dbname,$collection_soal,$sesi5);
}

if(preg_match('/^06/', $callback_data)){   
$url_img = $urlsoal.'7.JPG'; // Mendapatkan file_id
$caption1 = "no7";
$inlineKeyboard = [
         [
            ['text' => 'A BENAR', 'callback_data' => '07a'],
            ['text' => 'D BENAR', 'callback_data' => '07d']         
        ],
        [
            ['text' => 'B BENAR', 'callback_data' => '07b'],
            ['text' => 'E BENAR', 'callback_data' => '07e']
        ],
        [
            ['text' => 'C BENAR', 'callback_data' => '07c'],
            ['text' => 'Semua Salah', 'callback_data' => '07s']
        ] 
];
// sendMessage($chat_idq, "jno6=".$callback_data);
// sendMessage($chat_idq, "kno6=".$result3[0]['06']);
sendpoto($chat_idq,$url_img,$inlineKeyboard,$caption1);
if($callback_data == $result3[0]['06']){
$data1 = ['06' => 1];
sendMessage($chat_idq, 'benar');
}else{
$data1 = ['06' => 0]; 
sendMessage($chat_idq, 'salah');
}
//sendMessage($chat_idq, 'sesi='.$sesi5);
updateid_db($chat_idq, $data1, $dbname,$collection_soal,$sesi5);
}

if(preg_match('/^07/', $callback_data)){   
$url_img = $urlsoal.'8.JPG'; // Mendapatkan file_id
$caption1 = "no8";
$inlineKeyboard = [
         [
            ['text' => 'A BENAR', 'callback_data' => '08a'],
            ['text' => 'D BENAR', 'callback_data' => '08d']         
        ],
        [
            ['text' => 'B BENAR', 'callback_data' => '08b'],
            ['text' => 'E BENAR', 'callback_data' => '08e']
        ],
        [
            ['text' => 'C BENAR', 'callback_data' => '08c'],
            ['text' => 'Semua Salah', 'callback_data' => '08s']
        ] 
];
// sendMessage($chat_idq, "jno7=".$callback_data);
// sendMessage($chat_idq, "kno7=".$result3[0]['07']);
sendpoto($chat_idq,$url_img,$inlineKeyboard,$caption1);
if($callback_data == $result3[0]['07']){
$data1 = ['07' => 1];
sendMessage($chat_idq, 'benar');
}else{
$data1 = ['07' => 0]; 
sendMessage($chat_idq, 'salah');
}
//sendMessage($chat_idq, 'sesi='.$sesi5);
updateid_db($chat_idq, $data1, $dbname,$collection_soal,$sesi5);
}


if(preg_match('/^08/', $callback_data)){   
$url_img = $urlsoal.'9.JPG'; // Mendapatkan file_id
$caption1 = "no9";
$inlineKeyboard = [
         [
            ['text' => 'A BENAR', 'callback_data' => '09a'],
            ['text' => 'D BENAR', 'callback_data' => '09d']         
        ],
        [
            ['text' => 'B BENAR', 'callback_data' => '09b'],
            ['text' => 'E BENAR', 'callback_data' => '09e']
        ],
        [
            ['text' => 'C BENAR', 'callback_data' => '09c'],
            ['text' => 'Semua Salah', 'callback_data' => '09s']
        ] 
];
// sendMessage($chat_idq, "jno8=".$callback_data);
// sendMessage($chat_idq, "kno8=".$result3[0]['08']);
sendpoto($chat_idq,$url_img,$inlineKeyboard,$caption1);
if($callback_data == $result3[0]['08']){
$data1 = ['08' => 1];
sendMessage($chat_idq, 'benar');
}else{
$data1 = ['08' => 0]; 
sendMessage($chat_idq, 'salah');
}
//sendMessage($chat_idq, 'sesi='.$sesi5);
updateid_db($chat_idq, $data1, $dbname,$collection_soal,$sesi5);
}

if(preg_match('/^09/', $callback_data)){   
$url_img = $urlsoal.'10.JPG'; // Mendapatkan file_id
$caption1 = "no10";
$inlineKeyboard = [
         [
            ['text' => 'A BENAR', 'callback_data' => '10a'],
            ['text' => 'D BENAR', 'callback_data' => '10d']         
        ],
        [
            ['text' => 'B BENAR', 'callback_data' => '10b'],
            ['text' => 'E BENAR', 'callback_data' => '10e']
        ],
        [
            ['text' => 'C BENAR', 'callback_data' => '10c'],
            ['text' => 'Semua Salah', 'callback_data' => '10s']
        ] 
];
// sendMessage($chat_idq, "jno9=".$callback_data);
// sendMessage($chat_idq, "kno9=".$result3[0]['09']);
sendpoto($chat_idq,$url_img,$inlineKeyboard,$caption1);
if($callback_data == $result3[0]['09']){
$data1 = ['09' => 1];
sendMessage($chat_idq, 'benar');
}else{
$data1 = ['09' => 0]; 
sendMessage($chat_idq, 'salah');
}
//sendMessage($chat_idq, 'sesi='.$sesi5);
 updateid_db($chat_idq, $data1, $dbname,$collection_soal,$sesi5);
}


if(preg_match('/^10/', $callback_data)){   
$url_img = $urlsoal.'selesai.JPG'; // Mendapatkan file_id
$caption1 = "Selesai";
$inlineKeyboard = [
         [
            ['text' => 'ok', 'callback_data' => '00']       
        ]
        
];
// sendMessage($chat_idq, "jno10=".$callback_data);
// sendMessage($chat_idq, "kno10=".$result3[0]['10']);
sendpoto($chat_idq,$url_img,$inlineKeyboard,$caption1);
// sendMessage($chat_idq, "no10=".$callback_data );
if($callback_data == $result3[0]['10']){
$data1 = ['10' => 1];
sendMessage($chat_idq, 'benar');
}else{
$data1 = ['10' => 0]; 
sendMessage($chat_idq, 'salah');
}
//sendMessage($chat_idq, 'sesi='.$sesi5);
 updateid_db($chat_idq, $data1, $dbname,$collection_soal,$sesi5);
}

if(preg_match('/^00/', $callback_data)){  ///ok

    $data1 = ['skor' => $b41]; 
updateid_db($chat_idq, $data1, $dbname,$collection_soal,$sesi5);
sendMessage($chat_idq, 'selesai sesi '.$sesi5);
}
/////////////////////////////////////////////////////////////////



if ($text == 'skor') {
sendMessage($chat_id, ' skor ->'.$b41 .' benar/10 soal' );
}

$del = (explode(',',$text));
     $del0= $del['0'];
     $del1= $del['1'];
     $del2= $del['2'];

if ($del0 == 'del_soal' and $del1 and $del2 ) {

//     function deleteid_db($chat_id, $dbname,$collection) {
// global $conn ;

// delete record
$delete = new MongoDB\Driver\BulkWrite();
$delete->delete(
	['id' => (string)$del1,'sesi' => (string)$del2],
	['limit' => 0]
);

$result = $conn->executeBulkWrite("$dbname.$collection_soal", $delete);

//print_r($result);

// verify
if ($result->getDeletedCount() == 1) {
    echo json_encode(
		array("message" => "Record successfully deleted")
        
	);
    sendMessage($chat_id, "Record successfully deleted");
} else {
    echo json_encode(
            array("message" => "Error while deleting record")
    );
    sendMessage($chat_id, "Error while deleting record");
}


// }
// deleteid_db($del1, $dbname,$collection_soal);
}

//if ($del0 == 'del1' and $del1 ) {
//deleteid_db($del1, $dbname,$collection_user);
//}

 $nm2 = $result2[0]['nama']; // 5)nama
$kls2 = $result2[0]['kelas'];

$bat = (explode(',',$text));
     $bat0= $bat['0'];
     $bat1= $bat['1'];
     $bat2= $bat['2'];

if ($bat0 == 'batal' and $bat1 ) {
sendMessage($chat_id_admin, 'del_soal,'.$chat_id .','.$bat1);
}


if ($text == 'admin' and ($chat_id == $chat_id_admin)) {   //admin

    sendMessage($chat_id_admin, 'update = kode,<#123> !  kode = '.$mykode.' ! daftar soal = '.$mykode.',<nama user>');
    sendMessage($chat_id_admin, 'daftar user = #daftar,<nama>  /qr2,/qr20  batal,<sesi> ' );
}







$up = (explode(',',$text));
     $up0= $up['0'];
     $up1= $up['1'];
     $up2= $up['2'];

// function updateid_db($chat_id, $set_values, $dbname,$collection,$sesi) {
// global $conn ;
if ($up0 == 'kode' and $up1 and ($chat_id == $chat_id_admin)) {   //admin

$oid = '6953dd2fff58422f31018a42';
$set_values = ['kode' => $up1];   //admin
$update = new MongoDB\Driver\BulkWrite();
$update->update(
	['_id' => new MongoDB\BSON\ObjectId($oid)], ['$set' => $set_values], ['multi' => false, 'upsert' => false]
);

$result = $conn->executeBulkWrite("$dbname.$collection_kunci", $update);

// verify
if ($result->getModifiedCount() == 1) {
    
     sendMessage($chat_id_admin, "Record successfully updated");
} else {
    
     sendMessage($chat_id_admin, "Error while updating record");
}
return false;
}
// }$query = [
 //   '_id' => new ObjectID('6953dd2fff58422f31018a42')
//];


////////////////////////////////////end soal/////////////////////////////////////////////////////





if ($text == '/coba') {
/////////////////////////////
   $data1 = [
            'id' => (string)$chat_id,
            'nama' => 'nama',
            'kelas' => 'kelas',
            'role' => "siswa",
            'edit' => 0,
            'delete' => 0,

            'select' => 0

            ];

      // insert_db( $data1, $chat_id, $dbname, $collection_user);
     //  delete_db("695307ff319bcab3e20910b2", $chat_id, $dbname,$collection_user);
      // update_db("69532e43fa687104a7042a82",$chat_id, $data1, $dbname,$collection_user);
     //  read_db("69532e43fa687104a7042a82",$chat_id,$dbname,$collection_user);
     //  read_all_db($chat_id,$dbname,$collection_user);
     //  count_db($chat_id,$dbname,$collection_user) ;
  //sendMessage($chat_id, $oidValue11 );
  //sendMessage($chat_id, count_db($chat_id,$dbname,$collection_user));

       
    }









//  sendMessage($chat_id_admin, json_encode($update['message']['contact']['phone_number']));

// sendMessage($chat_id_admin, json_encode($update));

////////////  /daftar

    $callback_data = $update['callback_query']['data'];
if($callback_data == 'TKR'){
// function del($chat_idq){
$chat_idq = $update['callback_query']['message']['chat']['id'];
$message_id = $update['callback_query']['message']['message_id'];


$botToken = BOT_TOKEN;
$apiURL = "https://api.telegram.org/bot" . $botToken . "/";
$deleteUrl = $apiURL . "deleteMessage?chat_id=" . $chat_idq . "&message_id=" . $message_id;
    $response = file_get_contents($deleteUrl);

// sendMessage($chat_id, $message_id );
sendMessage($chat_idq, $message_id );
}
 


/////delete//////////////////////////////////////////////
    // $regex = '/^\d\d/';
$callback_data = $update['callback_query']['data'];

if(preg_match('/^\d\d/', $callback_data) or preg_match('/^_/', $callback_data) or preg_match('/^#/', $callback_data) ){   
    ///delete ^00  , no1
    $chat_idq = $update['callback_query']['message']['chat']['id'];
    $message_id = $update['callback_query']['message']['message_id'];
    $botToken = BOT_TOKEN;
    $apiURL = "https://api.telegram.org/bot" . $botToken . "/";
    $deleteUrl = $apiURL . "deleteMessage?chat_id=" . $chat_idq . "&message_id=" . $message_id;
    $response = file_get_contents($deleteUrl);
    }
   
if( $callback_data == 'no1' or $callback_data == '00kontak'){   
    ///delete ^00  , no1
    $chat_idq = $update['callback_query']['message']['chat']['id'];
    $message_id = $update['callback_query']['message']['message_id'];
    $botToken = BOT_TOKEN;
    $apiURL = "https://api.telegram.org/bot" . $botToken . "/";
    $deleteUrl = $apiURL . "deleteMessage?chat_id=" . $chat_idq . "&message_id=" . $message_id;
    $response = file_get_contents($deleteUrl);
    }


$chat_id = $update['message']['chat']['id'];
$text = $update['message']['text'];

if($text == '/mulai' or $text == '/daftar' or preg_match('/^#/', $text) or preg_match('/^del/', $text))
        ///delete /mulai /daftar
    {                               
    $message_id = $update['message']['message_id'];
    $botToken = BOT_TOKEN;
    $apiURL = "https://api.telegram.org/bot" . $botToken . "/";
    $deleteUrl = $apiURL . "deleteMessage?chat_id=" . $chat_id. "&message_id=" . $message_id;
    $response = file_get_contents($deleteUrl);
    // sendMessage($chat_id, '$message_id' );
    }


 ////////////////end delete /////   



$callback_data = $update['callback_query']['data'];
$chat_idq = $update['callback_query']['message']['chat']['id'];
// sendMessage($chat_idq, $callback_data);



if(preg_match('/^_/', $callback_data)){   
// sendMessage($chat_idq, "/if");
$text1 = trim($callback_data,"_");
$data1 = ['kelas' => $text1];
 updateid_db($chat_idq, $data1, $dbname,$collection_user,$sesi);

$url_img = 'https://isekolah.my.id/myWebhook/soal/kontak.JPG'; // Mendapatkan file_id
$caption1 = "";
$inlineKeyboard = [
         [
            ['text' => 'Lanjut', 'callback_data' => '#0kontak']       
        ]
        
];
// sendMessage($chat_idq, "jno10=".$callback_data);
// sendMessage($chat_idq, "kno10=".$result3[0]['10']);
sendpoto($chat_idq,$url_img,$inlineKeyboard,$caption1);
   }   //sendMessage($chat_id, $txt0."&".$txt1."&".$txt2);


if( $callback_data == '#0kontak' ){
    // sendMessage($chat_idq, "/if ".$callback_data);


$botToken = BOT_TOKEN;

$response = "Klik - Kirim Kontak - untuk lanjut:";
  $keyboard = [
     'keyboard' => [
        [
            ['text' => 'Kirim Kontak', 'request_contact' => true]
        ]
    ],
    'one_time_keyboard' => true,
    'resize_keyboard' => true
];

// Encode the keyboard array into a JSON object
$encodedKeyboard = json_encode($keyboard);

// Prepare the data for the API request
$data = array(
    'chat_id' => $chat_idq,
    'text' => $response,
    'reply_markup' => $encodedKeyboard
);

    $url = "https://api.telegram.org/bot" . $botToken . "/sendMessage";
    $options = array(
        'http' => array(
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($data)
        )
    );
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    
// }

}


if (($update['message']['contact'])) {
    $chat_id = $update['message']['chat']['id'];
// sendMessage($chat_id, json_encode($update['message']['contact']['phone_number']));

 $data1 = ['nohp' => json_encode($update['message']['contact']['phone_number'])];
 updateid_db($chat_id, $data1, $dbname,$collection_user,$sesi);
 sendMessage($chat_id, "berhasik terdaftar");
}













/////////////////////////daftar /////////////////////////////////////////

$chat_id = $update['message']['chat']['id'];
$text = $update['message']['text'];

if ($text == '/daftar') {
// $botToken = BOT_TOKEN;
// $response = "Jurusans:";
//   $keyboard = [
//     'inline_keyboard' => [
//         // First row of buttons
//         [
//             ['text' => 'TKR', 'callback_data' => '#TKR'],
//             ['text' => 'DPIB', 'callback_data' => '#DPIB']
//         ],
//         // Second row of buttons (optional)
//        [
//             ['text' => 'TKJ', 'callback_data' => '#TKJ'],
//             ['text' => 'TPM', 'callback_data' => '#TPM']
//         ]
//     ]
// ];
// $encodedKeyboard = json_encode($keyboard);
// $parameters = array(
//     'chat_id' => $chat_id,
//     'text' => $response,
//     'reply_markup' => $encodedKeyboard
// );
// send('sendMessage', $parameters, $botToken);



$url_img = $urlsoal.'selesai.JPG'; // Mendapatkan file_id
$caption1 = "";
$inlineKeyboard = [
         [
            ['text' => 'TKR', 'callback_data' => '#TKR'],
            ['text' => 'DPIB', 'callback_data' => '#DPIB']
        ],
        // Second row of buttons (optional)
       [
            ['text' => 'TKJ', 'callback_data' => '#TKJ'],
            ['text' => 'TPM', 'callback_data' => '#TPM']
        ]
        
];
sendpoto($chat_idq,$url_img,$inlineKeyboard,$caption1);








}


$callback_data = $update['callback_query']['data'];
if( $callback_data == "#TKR"){   
$chat_idq = $update['callback_query']['message']['chat']['id'];


  $botToken = BOT_TOKEN;
$response = "Pilih Kelas:";
  $keyboard = [
    'inline_keyboard' => [
        // First row of buttons
        [
            ['text' => 'XI TKRA', 'callback_data' => '#XI TKRA'],
            ['text' => 'XI TKRB', 'callback_data' => '#XI TKRB']
        ],
        // Second row of buttons (optional)
       [
            ['text' => 'XI TKRC', 'callback_data' => '#XI TKRC'],
            ['text' => 'XI DPIB', 'callback_data' => '#XI DPIB']
        ]
    ]
];
$encodedKeyboard = json_encode($keyboard);
$parameters = array(
    'chat_id' => $chat_idq,
    'text' => $response,
    'reply_markup' => $encodedKeyboard
);
send('sendMessage', $parameters, $botToken);  
}



if($callback_data == '#XI TKRA'){   
$chat_idq = $update['callback_query']['message']['chat']['id'];
 
 
sendMessage($chat_idq, "Masukkan Nama ");  

// sendMessage($chat_id_admin, json_encode($update));
}

$text = $update['message']['text'];
if(preg_match('/^#/', $text)){
$chat_id = $update['message']['chat']['id'];
 $text1 = trim($text,"#");
// sendMessage($chat_id,'nama = '. $text1); 
}
//////////////////////////end daftar//////////////////////////////////////////






// $regex = '/^data/';


// if (($update['callback_query'])) {
    $botToken = BOT_TOKEN;
    $callback_query_id = $update['callback_query']['id'];
    $callback_data = $update['callback_query']['data'];
    $chat_idq = $update['callback_query']['message']['chat']['id'];
    $message_id = $update['callback_query']['message']['message_id'];
    // sendMessage($chat_id, "berhasik kirim");
    // del_msg($chat_id,$callbackid);
//    sendMessage($chat_id, json_encode($update['callback_query']['data']));



if($callback_data == "data_A"){
$response = "Click a button below:";
$keyboard = [
    'inline_keyboard' => [
        // First row of buttons
        [
            ['text' => 'Option A1', 'callback_data' => 'data_A'],
            ['text' => 'Option B1', 'callback_data' => 'data_B'],
            ['text' => 'Option D1', 'callback_data' => 'no1']
        ],
        // Second row of buttons (optional)
        [
            ['text' => 'Visit Google1', 'url' => 'https://www.google.com'] // URL button example
        ]
    ]
];
$encodedKeyboard = json_encode($keyboard);
$parameters = array(
    'chat_id' => $chat_idq,
    'text' => $response,
    'reply_markup' => $encodedKeyboard
);
send('sendMessage', $parameters, $botToken);

}





// }
/// end callback_quer




///////>
if (($update['message']['text'])) {
    $chat_id = $update['message']['chat']['id'];
    $text1 = $update['message'];
    $jsonString1 = json_encode($text1);
    // sendMessage($chat_id, $jsonString1);
    $text = $update['message']['text'];
        $filter = ['id' => (string)$chat_id ];
	$option = [];
        $options = [
                'projection' => [
                '_id' => 0.0
           ]
          ];

	$read = new MongoDB\Driver\Query($filter, $options);

	//fetch records
	$records = $conn->executeQuery("$dbname.$collection_user", $read);

        echo	$res1= json_encode(iterator_to_array($records)); //echo
 	$result1= json_decode($res1, true); 
 	$count1= json_encode(count($result1)); //echo
	 $result2= json_decode($res1, true);

        $nama = $result2['0']["nama"];
        $kelas = $result2['0']["kelas"];
        $_id = $result2[0]["_id"]["$oid"];
        $edit = $result2['0']["edit"];
        $delete = $result2['0']["delete"];
        $select = $result2['0']["select"];
        $res3 = json_encode($result2['0']);

        // var_dump($result2) ;

       $jsonString = json_encode($res1);
       //  sendMessage($chat_id_admin, $jsonString);
        //sendMessage($chat_id_admin, $_id);
        //  $myObject = (object) ['oid' => '6571556e40995a947d0e4029']; // Example of creating a stdClass object
        // echo $myObject->oid;
//sendMessage($chat_id_admin, $text);

/////////////////////////////////
if ($text == '/start') {
/////////////////////////////

       sendMessage($chat_id, "Hello!Selamat Datang to your bot.");
    }


    




if ($text == '/if') {
/////////////////////////////

$botToken = BOT_TOKEN;

$response = "Klik - Kirim Kontak - untuk lanjut:";
  $keyboard = [
     'keyboard' => [
        [
            ['text' => 'Kirim Kontak', 'request_contact' => true]
        ]
    ],
    'one_time_keyboard' => true,
    'resize_keyboard' => true
];

// Encode the keyboard array into a JSON object
$encodedKeyboard = json_encode($keyboard);

// Prepare the data for the API request
$parameters = array(
    'chat_id' => $chat_id,
    'text' => $response,
    'reply_markup' => $encodedKeyboard
);

send('sendMessage', $parameters, $botToken);



    }



///////////////////////////
if($text == "hp"){
///////////////////////////
 $botToken = BOT_TOKEN;
$response = "Click a button below:";
  $keyboard = [
    'inline_keyboard' => [
        // First row of buttons
        [
            ['text' => 'Option A', 'callback_data' => 'data_A'],
            ['text' => 'Option B', 'callback_data' => 'data_B']
        ],
        // Second row of buttons (optional)
        [
            ['text' => 'Visit Google', 'url' => 'https://www.google.com'] // URL button example
        ]
    ]
];

// Encode the keyboard array into a JSON object
$encodedKeyboard = json_encode($keyboard);

// Prepare the data for the API request
$parameters = array(
    'chat_id' => $chat_id,
    'text' => $response,
    'reply_markup' => $encodedKeyboard
);
send('sendMessage', $parameters, $botToken);
}



/////////////////////////////////
if ($text == '/cek') {
/////////////////////////////////

    sendMessage($chat_id, $nm2."&".$kls2);
    sendMessage($chat_id, $chat_id);
    
    }





 $pecah = (explode(',',$text));
     $txt0= $pecah['0'];
     $txt1= $pecah['1'];
     $txt2= $pecah['2'];

///////////////////////////////////////////////////
if ($txt0 == '#daftar' && $txt1 ) {
////////////////////////////////////////////////////

    // sendMessage($chat_id, "daftar");

	
        if($count2 >= 1){

        echo json_encode(

                array("message" => "Record sudah ada")
            );

      sendMessage($chat_id, "Record sudah ada");

        }else{

           $data1 = [
            'id' => (string)$chat_id,
            'nama' => (string)$txt1,
            'kelas' => '',
            'role' => "siswa",
            'edit' => 0,
            'delete' => 0,

            'select' => 0

            ];


        // insert record
        $insert = new MongoDB\Driver\BulkWrite();
        $insert->insert($data1);

        $result = $conn->executeBulkWrite("$dbname.$collection_user", $insert);
       // $result = $conn->executeBulkWrite("$coll", $insert);
        // verify
        if ($result->getInsertedCount() == 1) {
          
        //  sendMessage($chat_id, "Record successfully created");


          

$url_img = 'https://isekolah.my.id/myWebhook/soal/pilihkelas.JPG'; // Mendapatkan file_id
$caption1 = "";
$inlineKeyboard = [
         [
                        ['text' => 'XI TKRA', 'callback_data' => '_XI TKRA'],
                        ['text' => 'XI TKRB', 'callback_data' => '_XI TKRB']
                        ],
                    // Second row of buttons (optional)
                        [
                        ['text' => 'XI TKRC', 'callback_data' => '_XI TKRC'],
                        ['text' => 'XI DPIB', 'callback_data' => '_XI DPIB']
                        ]
];
// sendMessage($chat_idq, "jno9=".$callback_data);
// sendMessage($chat_idq, "kno9=".$result3[0]['09']);
sendpoto($chat_id,$url_img,$inlineKeyboard,$caption1);







        } else {
            echo json_encode(
                    array("message" => "Error while saving record")
            );
       sendMessage($chat_id, "Error while saving record");
        }
        }
     }




///////////////////////////////////
if ($text == '/qr10' && $count1 == 1) {
/////////////////////////////////
        
    $url= 'temp/'.$chat_id.'.png';
    $pesan= date("Y-m-d H:i:s")."\n by : ".$nama;
    //sendMessage($chat_id, $url);
    photo($chat_id,$url,$pesan);
    }

///////////////////////////////////
if ($text == '/qr20' && $count1 == 1) {
/////////////////////////////////
        
    $url= 'logo/'.$chat_id.'.png';
    $pesan= date("Y-m-d H:i:s")."\n by : ".$nama;
    //sendMessage($chat_id, $url);
    photo($chat_id,$url,$pesan);
    }


/////////////////////////////////////////
if ($text == '/qr1' && $count1 == 1) {
///////////////////////////////////
    include "phpqrcode/qrlib.php"; 

    // nama folder tempat penyimpanan file qrcode
    $penyimpanan = "temp/";

    // membuat folder dengan nama "temp"
    if (!file_exists($penyimpanan))
    mkdir($penyimpanan);

     // isi qrcode yang ingin dibuat. akan muncul saat di scan

     $isi =  [   'id' => (string)$chat_id,
            'nama' => $nama,
            'kelas' => $kelas,
            'role' => "siswa"
            ];

     // $json2=json_decode($isi);
     // $json = json_decode($json2, true);
     $jsonString = json_encode($isi);
     $str1 = base64_decode($jsonString);
     //   echo $jsonString;
     sendMessage($chat_id, "qr code dibuat");


     /**/
     // perintah untuk membuat qrcode dan menyimpannya dalam folder temp
     // atur level pemulihan datanya dengan QR_ECLEVEL_L | QR_ECLEVEL_M | QR_ECLEVEL_Q | QR_ECLEVEL_H 
     // QRcode::png($isi, $penyimpanan.$chat_id'.'.png', QR_ECLEVEL_H);
       QRcode::png($res3, 'temp/'.$chat_id.'.png', QR_ECLEVEL_H);
     // QRcode::png($res3, $penyimpanan.'qrcodeku_H.png', QR_ECLEVEL_H, 10, 15);
    ///////////////////////
   
     sendMessage($chat_id, "qr code telah sukses dibuat");
    }



/////////////////////////////////////////
if ($text == '/qr2' && $count1 == 1) {
///////////////////////////////////
include "phpqrcode/qrlib.php"; 
     
$penyimpanan = "temp/";
$logo = "logo/";

// membuat folder dengan nama "temp"
    if (!file_exists($penyimpanan))
    mkdir($penyimpanan);
// membuat folder dengan nama "logo"
    if (!file_exists($logo))
    mkdir($penyimpanan);



$filepath = 'temp/'.$chat_id.'.png';
$filelog = 'logo/'.$chat_id.'.png';

// Image (logo) to be drawn
$logopath = $urlpoin.'/Logo-Tut.png';
// qr code content


$filter3 = ['id' => (string)$chat_id ];
	$option = [];
        $options3 = [
                'projection' => [
                '_id' => 0.0,
                'role' => 0.0

           ]
          ];
	$read3 = new MongoDB\Driver\Query($filter3, $options3);
	$records3 = $conn->executeQuery("$dbname.$collection_user", $read3);
        $res3= json_encode(iterator_to_array($records3)); //echo
 	$result3= json_decode($res3, true); 
 	$count3= json_encode(count($result3)); //echo
	$result3= json_decode($res3, true);
        $res30 = json_encode($result3['0']);
$res31 = base64_encode($res30);

// $str2 = base64_decode($str3 );         // dekresi json
// $json = json_decode($str2, true);
// var_dump($json) ;


//////////////////////////////////////////////////////

QRcode::png($res30,$filepath , QR_ECLEVEL_H, 20);

/////////////////////////////////////////////////////////

$QR = imagecreatefrompng($filepath);
$logo = imagecreatefromstring(file_get_contents($logopath));

imagecolortransparent($logo , imagecolorallocatealpha($logo , 0, 0, 0, 127));
imagealphablending($logo , false);
imagesavealpha($logo , true);

$QR_width = imagesx($QR);
$QR_height = imagesy($QR);

$logo_width = imagesx($logo);
$logo_height = imagesy($logo);

// Scale logo to fit in the QR Code
$logo_qr_width = $QR_width/3;
$scale = $logo_width/$logo_qr_width;
$logo_qr_height = $logo_height/$scale;

imagecopyresampled($QR, $logo, $QR_width/3, $QR_height/3, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);

// Save QR code again, but with logo on it
imagepng($QR,$filelog);

   
     sendMessage($chat_id, "qr code telah sukses dibuat");



    }

    
     $pecah = (explode(',',$text));
     $txt0= $pecah['0'];
     $txt1= $pecah['1'];
     $txt2= $pecah['2'];
     //sendMessage($chat_id, $txt0."&".$txt1."&".$txt2);

//////////////////////////////////////////////////
if ($txt0 == '/update' && $txt1 && $txt2 && $count1 == 1) {
////////////////////////////////////////////////

     //sendMessage($chat_id, "edit1");



           $data1 = [
            'id' => (string)$chat_id,
            'nama' => (string)$txt1,
            'kelas' => (string)$txt2,
            'role' => "siswa",
            'edit' => 0
            ];



        // update record
        $update = new MongoDB\Driver\BulkWrite();
        $update->update(
            // ['_id' => new MongoDB\BSON\ObjectId($id)],
            ["id" => (string)$chat_id,"edit" => 1],
            ['$set' => $data1],
            ['multi' => false, 'upsert' => false]
        );

        $result = $conn->executeBulkWrite("$dbname.$collection_user", $update);

        // verify
        if ($result->getModifiedCount() == 1) {
            echo json_encode(
                array("message" => "Record successfully updated")
            );
        sendMessage($chat_id, "Record successfully updated");
        } else {
            echo json_encode(
                    array("message" => "Error while updating record")
            );

        sendMessage($chat_id, "Error while updating record");
       }
       }


//////////////////////////////////////////////////////////
if ($txt0 == '/del'&&  $count1 == 1) {
////////////////////////////////////////////////////////

       // delete record
        $delete = new MongoDB\Driver\BulkWrite();
        $delete->delete(
            // ['_id' => new MongoDB\BSON\ObjectId($id)],
            ["id" => (string)$chat_id,'delete' => 1],
            ['limit' => 0]
        );

        $result = $conn->executeBulkWrite("$dbname.$collection_user", $delete);

        //print_r($result);

        // verify
        if ($result->getDeletedCount() == 1) {
            echo json_encode(
                array("message" => "Record successfully deleted")
            );
        sendMessage($chat_id, "Record successfully deleted");

        } else {
            echo json_encode(
                    array("message" => "Error while deleting record")
            );
        sendMessage($chat_id, "Error while deleting record");

        }
        }


}


//end text






////////photo//////////////////////////



if (($update['message']['photo'])) {
    $chat_id = $update['message']['chat']['id'];
    $photo1 = $update['message']['photo'][0]['file_id'];
    $photo = $update['message'];

    $jsonString = json_encode($photo);
   // sendMessage($chat_id_admin, $jsonString);

$token = "8291470736:AAHMwfbO6ABPoe3OtzRujXmaLzXJaMPphIQ";
$url = "https://api.telegram.org/bot".BOT_TOKEN."/sendPhoto";
$url1 = "https://api.telegram.org/bot8291470736:AAHMwfbO6ABPoe3OtzRujXmaLzXJaMPphIQ/sendPhoto";
$chat_id = $update['message']['chat']['id'];
$file_id = $update['message']['photo'][0]['file_id']; // Mendapatkan file_id
$caption = $update['message']['caption'];
$first_name = $update['message']['chat']['first_name'];
$last_name = $update['message']['chat']['last_name'];
$username = $update['message']['chat']['username'];
$caption1 = "user:@".$username."-fn:".$first_name."-ln:".$last_name;
// sendMessage($chat_id_admin, $caption);



$pecah1 = (explode(',',$caption));
 $cap0= $pecah1['0'];
 $cap1= $pecah1['1'];
$cap2= $pecah1['2'];



sendMessage($chat_id_admin, "Berhasil terkirim !");




///////////////////////////
if($caption == "tugas"){
///////////////////////////

   $curl = curl_init();
   curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => array('chat_id' => $chat_id_admin,'photo' => $file_id,'caption' => $caption1),
   ));

   $response = curl_exec($curl);

   curl_close($curl);
   echo $response;
    sendMessage($chat_id, "Berhasil terkirim !");
   }
   //sendMessage($chat_id, $caption."&".$cap0."&".$cap1."&".$cap2);

/////////////////////////////////
if($cap0 == "kirim" && $cap1 ){
/////////////////////////////////

        $filter = ['select' => 1];
	$option = [];
	$read = new MongoDB\Driver\Query($filter, $option);

	//fetch records
	$records = $conn->executeQuery("$dbname.$collection_user", $read);

	$res1= json_encode(iterator_to_array($records)); //echo
        //echo $_id = $res1->["_id"];  
        $result1= json_decode($res1, true); 
 	$count1= json_encode(count($result1)); //echo
	 $result2= json_decode($res1, true);

        foreach ($result2 as $document) {
        // echo $document['id'] . "\n";
       //sendMessage($chat_id, $document['id']);

       pht($document['id'],$file_id,$cap1,$chat_id,);

        }
      //   }else{
       //      sendMessage($chat_id, "tdk terkirim");

          }
 }
 
 
 
 // end photo







function sendpoto($chat_id,$url_img,$inlineKeyboard,$caption1) {

$url = "https://api.telegram.org/bot".BOT_TOKEN."/sendPhoto";
$replyMarkupInline = json_encode([
    'inline_keyboard' => $inlineKeyboard
]);

$curl = curl_init();
   curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => array('chat_id' => $chat_id,'photo' => $url_img,'caption' => $caption1,'reply_markup' =>$replyMarkupInline ),
   ));

   $response = curl_exec($curl);

   curl_close($curl);
   //echo $response;
    //sendMessage($chat_id, "Berhasil terkirim !");
   }













function sendMessage($chat_id, $text) {
    $url = 'https://api.telegram.org/bot' . BOT_TOKEN . '/sendMessage';
    $data = [
        'chat_id' => $chat_id,
        'text' => $text,
    ];

    $options = [
        'http' => [
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($data),
        ],
    ];
    $context  = stream_context_create($options);
    file_get_contents($url, false, $context);
}


function pht($chat_id,$file_id,$pesan,$chatid) {

$token = "8291470736:AAHMwfbO6ABPoe3OtzRujXmaLzXJaMPphIQ";
$url = "https://api.telegram.org/bot".BOT_TOKEN."/sendPhoto";


$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => array('chat_id' => $chat_id,'photo' => $file_id,'caption' => $pesan),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
sendMessage($chatid, "Berhasil terkirim ! ke -".$chat_id);

}


function photo($chat_id,$file_id,$pesan) {

$token = "8291470736:AAHMwfbO6ABPoe3OtzRujXmaLzXJaMPphIQ";
$url = "https://api.telegram.org/bot".BOT_TOKEN."/sendPhoto";


$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => array('chat_id' => $chat_id,'photo' => new CURLFile($file_id),'caption' => $pesan),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
//sendMessage($chatid, "Berhasil terkirim ! ke -".$chat_id);

}



info();




function info() {
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.telegram.org/bot' . BOT_TOKEN . '/getWebhookInfo',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => array('url' => WEBHOOK_URL),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}


function set() {
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.telegram.org/bot' . BOT_TOKEN . '/setWebhook',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => array('url' => WEBHOOK_URL,'max_connections'=> 100),
));

$response = curl_exec($curl);
curl_close($curl);
echo $response;

}





/*
//////////////////////////////////////////////////////////////////////////////


$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.telegram.org/bot' . BOT_TOKEN . '/setWebhook',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => array('url' => WEBHOOK_URL,'max_connections'=100),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;


$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.telegram.org/bot'.BOT_TOKEN.'/deleteWebhook',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
///////////////////////////////////////////////////////////////////////////
*/

function send($method, $data, $botToken) {
    $url = "https://api.telegram.org/bot" . $botToken . "/" . $method;
    $options = array(
        'http' => array(
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($data)
        )
    );
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return $result;
}


function read_db($oid,$chat_id,$dbname,$collection) {
global $conn ;
$db = new DbManager();
$conn = $db->getConnection();


    $filter = ['_id' => new MongoDB\BSON\ObjectId($oid)];
	//$options = ['projection' => ['_id' => 0.0]];
    $options = [];
	$read = new MongoDB\Driver\Query($filter, $options);
	$records = $conn->executeQuery("$dbname.$collection", $read);
 	$res1= json_encode(iterator_to_array($records)); //echo
        //echo $_id = $res1->["_id"];  
    $result1= json_decode($res1, true); 
 	$count1= json_encode(count($result1)); //echo
	 $result2= json_decode($res1, true);
sendMessage($chat_id, $res1);
  return $res1;
}


function count_db($chat_id,$dbname,$collection) {
global $conn ;
$db = new DbManager();
$conn = $db->getConnection();


    $filter = ['id' => (string)$chat_id];
	//$options = ['projection' => ['_id' => 0.0]];
    $options = [];
	$read = new MongoDB\Driver\Query($filter, $options);
	$records = $conn->executeQuery("$dbname.$collection", $read);
 	$res1= json_encode(iterator_to_array($records)); //echo
        //echo $_id = $res1->["_id"];  
    $result1= json_decode($res1, true); 
 	$count1= json_encode(count($result1)); //echo
	 $result2= json_decode($res1, true);
sendMessage($chat_id, $count1);
return $count1;


}






function read_all_db($chat_id,$dbname,$collection) {
global $conn ;
$db = new DbManager();
$conn = $db->getConnection();
// read all records
//$filter = [['_id' => new MongoDB\BSON\ObjectId($oid)]];
$filter = [];
	//$options = ['projection' => ['_id' => 0.0]];
    $options = [];
	$read = new MongoDB\Driver\Query($filter, $options);
	$records = $conn->executeQuery("$dbname.$collection", $read);
 	$res1= json_encode(iterator_to_array($records)); //echo
        //echo $_id = $res1->["_id"];  
    $result1= json_decode($res1, true); 
 	$count1= json_encode(count($result1)); //echo
	 $result2= json_decode($res1, true);

sendMessage($chat_id,  $count1);
}


function update_db($oid,$chat_id, $set_values, $dbname,$collection) {
global $conn ;
$update = new MongoDB\Driver\BulkWrite();
$update->update(
	['_id' => new MongoDB\BSON\ObjectId($oid)], ['$set' => $set_values], ['multi' => false, 'upsert' => false]
);

$result = $conn->executeBulkWrite("$dbname.$collection", $update);

// verify
if ($result->getModifiedCount() == 1) {
    echo json_encode(
		array("message" => "Record successfully updated")
	);
    sendMessage($chat_id, "Record successfully updated");
} else {
    echo json_encode(
            array("message" => "Error while updating record")
    );
    sendMessage($chat_id, "Error while updating record");
}

}


function updateid_db($chat_id, $set_values, $dbname,$collection,$sesi) {
global $conn ;
$update = new MongoDB\Driver\BulkWrite();
$update->update(
	['id' => (string)$chat_id,'sesi'=>(string)$sesi], ['$set' => $set_values], ['multi' => false, 'upsert' => false]
);

$result = $conn->executeBulkWrite("$dbname.$collection", $update);

// verify
if ($result->getModifiedCount() == 1) {
    echo json_encode(
		array("message" => "Record successfully updated")
	);
    // sendMessage($chat_id, "Record successfully updated");
} else {
    echo json_encode(
            array("message" => "Error while updating record")
    );
    // sendMessage($chat_id, "Error while updating record");
}

}

function insert_db($data, $chat_id, $dbname,$collection) {
global $conn ;
$insert = new MongoDB\Driver\BulkWrite();
$insert->insert($data);

$result = $conn->executeBulkWrite("$dbname.$collection", $insert);

// verify
if ($result->getInsertedCount() == 1) {
    echo json_encode(
		array("message" => "Record successfully created")
	);
    // sendMessage($chat_id, "Record successfully created");
} else {
    echo json_encode(
            array("message" => "Error while saving record")
    );
    // sendMessage($chat_id, "Error while saving record");
}

}


function delete_db($oid, $chat_id, $dbname,$collection) {

global $conn ;
// delete record
$delete = new MongoDB\Driver\BulkWrite();
$delete->delete(
	['_id' => new MongoDB\BSON\ObjectId($oid)],
	['limit' => 0]
);

$result = $conn->executeBulkWrite("$dbname.$collection", $delete);

//print_r($result);

// verify
if ($result->getDeletedCount() == 1) {
    echo json_encode(
		array("message" => "Record successfully deleted")
        
	);
    sendMessage($chat_id, "Record successfully deleted");
} else {
    echo json_encode(
            array("message" => "Error while deleting record")
    );
    sendMessage($chat_id, "Error while deleting record");
}


}

function deleteid_db($chat_id, $dbname,$collection) {
global $conn ;

// delete record
$delete = new MongoDB\Driver\BulkWrite();
$delete->delete(
	['id' => (string)$chat_id],
	['limit' => 0]
);

$result = $conn->executeBulkWrite("$dbname.$collection", $delete);

//print_r($result);

// verify
if ($result->getDeletedCount() == 1) {
    echo json_encode(
		array("message" => "Record successfully deleted")
        
	);
    sendMessage($chat_id, "Record successfully deleted");
} else {
    echo json_encode(
            array("message" => "Error while deleting record")
    );
    sendMessage($chat_id, "Error while deleting record");
}


}


function contak($chat_id) {
global $botToken ;

$response = "Klik - Kirim Kontak - untuk lanjut:";
  $keyboard = [
     'keyboard' => [
        [
            ['text' => 'Kirim Kontak', 'request_contact' => true]
        ]
    ],
    'one_time_keyboard' => true,
    'resize_keyboard' => true
];

// Encode the keyboard array into a JSON object
$encodedKeyboard = json_encode($keyboard);

// Prepare the data for the API request
$parameters = array(
    'chat_id' => $chat_id,
    'text' => $response,
    'reply_markup' => $encodedKeyboard
);

send('sendMessage', $parameters, $botToken);


}




?>
