public function execute()
 {
     $update = $this->getUpdate();
     $message = $this->getMessage();
     $chat_id = $message->getChat()->getId();
     $data = array();
     $data['chat_id'] = $chat_id;
     $miTest = new Test();
     //$data['text'] = 'Comenzando el trivial...';
     $data['text'] = '';
     $temaElegido = $message->getText(true);
     //Comprobar si existe el tema solicitado.
     if ($miTest->existeTema($temaElegido)) {
         //Se obtiene la primera de las preguntas.
         //$data['text'] .= "\n" ."Buscando pregunta...";
         $preguntaMostrar = $miTest->obtenerPreguntaAleatoria($temaElegido);
         if (!is_null($preguntaMostrar)) {
             //$data['text'] .= "\n" ."Pregunta encontrada, se muestra y se guarda el fichero...";
             $data['text'] .= "\n" . $preguntaMostrar['pregunta'];
             //Se crea un nuevo fichero de test
             $miTest->crearFicheroTest($chat_id, "respuestacorrecta=" . $preguntaMostrar['s'] . ";temaElegido=" . $temaElegido);
             //$data['text'] .= "\n" ."Fichero creado, se debería de haber mostrado la pregunta...";
             //$data['text'] .= "\n" ."Contenido del fichero: respuestacorrecta=".$preguntaMostrar['s'].";temaElegido=".$temaElegido;
             $contenido = $miTest->obtenerContenidoFichero($chat_id);
             //$data['text'] .= "\n" ."Y el contenido real es: " . $contenido;
         } else {
             $data['text'] .= "\n" . 'Ha ocurrido un error obteniendo las preguntas, seleccione otro tema.';
         }
     } else {
         //No existe el tema, mostrar los temas disponibles e indicar que se debe
         //elegir uno de los temas.
         $temas = $miTest->obtenerTemas();
         $data['text'] .= "\n" . "El tema indicado no esta disponible. Debe elegir uno de los siguientes:";
         foreach ($temas as $temaAct) {
             $data['text'] .= "\n" . $temaAct;
         }
     }
     $result = Request::sendMessage($data);
     return $result;
 }
Exemple #2
0
 /**
  * Handle bot request
  *
  * @return \Longman\TelegramBot\Telegram
  */
 public function handle()
 {
     $this->input = Request::getInput();
     if (empty($this->input)) {
         throw new TelegramException('Input is empty!');
     }
     $post = json_decode($this->input, true);
     if (empty($post)) {
         throw new TelegramException('Invalid JSON!');
     }
     $update = new Update($post, $this->bot_name);
     $this->insertRequest($update);
     $message = $update->getMessage();
     // check type
     $type = $message->getType();
     switch ($type) {
         default:
         case 'text':
             // do nothing
             $chat_id = $message->getChat()->getId();
             $message_id = $message->getMessageId();
             $text = $message->getText();
             //Si estamos en modo test
             $test = new Test();
             $datatest = $test->realizarAccionesTest($message);
             if (!is_null($datatest)) {
                 return Request::sendMessage($datatest);
             } else {
                 // do nothing
             }
             break;
         case 'command':
             // execute command
             $command = $message->getCommand();
             return $this->executeCommand($command, $update);
             break;
         case 'new_chat_participant':
             // trigger new participant
             return $this->executeCommand('Newchatparticipant', $update);
             break;
         case 'left_chat_participant':
             // trigger left chat participant
             return $this->executeCommand('Leftchatparticipant', $update);
             break;
         case 'new_chat_title':
             // trigger new_chat_title
             break;
         case 'delete_chat_photo':
             // trigger delete_chat_photo
             break;
         case 'group_chat_created':
             // trigger group_chat_created
             break;
     }
 }