<?php
include 'Telegram.php';
if ($_SERVER["REQUEST_METHOD"] == "GET") {
    $telegram = new Telegram('8434119591:AAFeXkqrs3spKHtFbkw5OZNLVIBvs8Qf7vM');

    $chat_id = $telegram->ChatID();
    $content = array('chat_id' => '-4803034495', 'text' => 'hi');
    $r = $telegram->sendMessage($content);
    echo print_r($r,true);
}else if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Reemplaza con tu token de bot de Telegram
    $telegram_token = '7193849734:AAEsCi2GJXnm-sRT0wOS945d1wGC09A0Jjk';
    // Reemplaza con tu ID de chat o el ID de grupo de Telegram
    //$chat_id = '6304684111';
    $telegram = new Telegram($telegram_token);
    $text = $telegram->Text();
    $chat_id = $telegram->ChatID();
    $data = $telegram->getData();
    
    // Lee el cuerpo de la solicitud POST
    //$request = file_get_contents("php://input");
    if ($text === '/start'){
        $option = array( 
        //First row
        array($telegram->buildKeyboardButton("info"), $telegram->buildKeyboardButton("sms rapido")), 
        //Third row
        array($telegram->buildKeyboardButton("Config")) );
        $keyb = $telegram->buildKeyBoard($option, $onetime=false);
        $content = array('chat_id' => $chat_id, 'reply_markup' => $keyb, 'text' => "This is a Keyboard Test");
        $telegram->sendMessage($content);
    }
    if (isset($text) && $text === 'info') {
        $url = "https://script.google.com/macros/s/AKfycbyxOxdw8b7_4VnEGw2iyiER69i2Cp-jURKLk4LPTzr18ZrQUW7pQbXN--M_6HAkZrZKEQ/exec";
        $data2 = array(
        'id' => $chat_id,
        'mode' => $text
        );
        unset($text);
        $ch3 = curl_init($url);
        curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch3, CURLOPT_POST, true);
        curl_setopt($ch3, CURLOPT_POSTFIELDS, json_encode($data2));
        curl_setopt($ch3, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json'
        ));
        curl_setopt($ch3, CURLOPT_FOLLOWLOCATION, true); // Aceptar redireccionamientos
        $response = curl_exec($ch3);
        curl_close($ch3);
        $responseArray = json_decode($response, true);
        $names = "";
        $num = 1;
        $buttons = [];
        $options = array();
        foreach ($responseArray as $key => $item) {
            $names .= intval($key) + 1 . ". " . $item['name'] . "\n";
            $options[] = $telegram->buildInlineKeyBoardButton($item['name'], $callback_data = $key);
            
        }
        
        $keyb = $telegram->buildInlineKeyBoard(array_chunk($options, 3)); // 3 buttons per row
        
        $content = array(
            'chat_id' => $chat_id,
            'reply_markup' => $keyb,
            'text' => $names
        );
        
        $result = $telegram->sendMessage($content);
    }else{
        $adata = $telegram->Callback_Data();
        if (isset($adata)){
            switch ($adata) {
                case '0':
                    // Código a ejecutar cuando se presiona el botón 1
                    $content = array('chat_id' => '-4803034495', 'text' => $adata);
                    $r = $telegram->sendMessage($content);
                    break;
                case '1':
                    // Código a ejecutar cuando se presiona el botón 2
                    echo 'Botón 2 presionado!';
                    break;
                case '2':
                    // Código a ejecutar cuando se presiona el botón 3
                    echo 'Botón 3 presionado!';
                    break;
                default:
                    // Código a ejecutar si no se reconoce la callback_data
                    echo 'Callback data no reconocida';
                    break;
            }
            
        }
        
    }
    
}
?>