Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define('BOT_TOKEN', 'XXX');
- define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
- define('CHAT_ID_JAVIER', 'XXX');
- define('SERVER', 'XXX');
- require_once("conexion_bd.php");
- include("Telegram.php");
- // Create object
- $telegram = new Telegram(BOT_TOKEN);
- // read incoming info and grab the chatID
- $content = file_get_contents("php://input");
- $update = json_decode($content, true);
- $chat_id = $update["message"]["chat"]["id"];
- $text = $update["message"]["text"];
- $words = explode(" ",$text);
- // Photos
- $photo = $update["message"]["photo"][0];
- $file_size = $photo["file_size"];
- $file_id = $photo["file_id"];
- // Documents
- $document = $update["message"]["document"];
- $file_id2 = $document["file_id"];
- // Exit if call comes from HTTP (i.e. empy $update object)
- if ($update=="") exit("Page not found");
- $usage="USO
- /pass NIA (manda contrasenia a tu correo)
- /alta NIA password (da de alta el NIA en el bot)
- /preg (manda una pregunta al azar)
- /preg_num (manda la pregunta con ese numero)
- /estado (cuenta preguntas respondidas por tema)
- /pendientes (lista de preguntas pendientes de contestar)
- /error texto del error (para avisar de un fallo)";
- $usage_admin="ADMIN
- /add_nia /para_todos /contra_nia /para_nia /resumen
- ".$usage;
- // ##########################
- // ########################## FROM THIS POINT, THE USER **MUST** BE REGISTERED
- // ##########################
- // USUARIO **NO** DADO DE ALTA
- $NIA=get_NIA($chat_id);
- if ( $NIA=="" ) {
- $reply="Lo siento pero no estás dado de alta. Debes mandar /pass NIA";
- send_message($chat_id,$reply);
- leave_bot($chat_id);
- }
- // ----------------------------------------------------- /preg_num
- if ($words[0]=="/preg_num" && sizeof($words)==2) {
- $id_ques=$words[1];
- // Busco esa pregunta
- // XXX Les dejo que respondan a preguntas "caducadas"
- $sql="select * from botf_questions where id_ques=$id_ques";
- $resul=mysql_query($sql);
- $row = mysql_fetch_array($resul);
- // No existe ese id
- if ($row=="") {
- $reply="No existe una pregunta con el numero $id_ques";
- send_message($chat_id,$reply);
- leave_bot($chat_id);
- }
- // Mando la pregunta: TEXTO + IMAGEN + EJEMPLO
- $id_ques=$row['id_ques'];
- $chap=$row['chap'];
- $text=$row['text'];
- $reply="PREG $id_ques TEMA $chap $text";
- send_message($chat_id,$reply);
- $file_url=SERVER."/bot_fluids/QUEST/$id_ques.jpg";
- send_doc($telegram,$chat_id,$file_url);
- $file_url=SERVER."/bot_fluids/QUEST/$id_ques-eje.jpg";
- send_doc($telegram,$chat_id,$file_url);
- // Fijo esa pregunta como activa
- set_pending($id_ques,$NIA);
- leave_bot($chat_id);
- } // end preg_num
- // ----------------------------------------------------- /error
- if ($words[0]=="/error" && sizeof($words)>=2) {
- // Mandar a Javier
- $mensaje="ERROR de $NIA: ".substr($text,7);
- $reply=manda_correo("jaba09@gmail.com","Bot: ERROR de $NIA",$NIA,$mensaje);
- debug("Correo error $NIA $reply: $mensaje");
- // Gracias al alumno
- $reply="Muchas gracias por avisar de este error. El profesor lo revisara lo antes posible y te respondera por correo";
- send_message($chat_id,$reply);
- leave_bot($chat_id);
- }
- // ----------------------------------------------------- /preg
- if ($words[0]=="/preg" && sizeof($words)==1) {
- // Busco preguntas no respondidas
- $sql="select * from botf_questions q, botf_chap c where c.chap=q.chap and now() between start and end and id_ques not in (select id_ques from botf_replies where NIA=$NIA) order by rand()";
- $resul=mysql_query($sql);
- $row = mysql_fetch_array($resul);
- // Ya no quedan preguntas
- if ($row=="") {
- $reply="Ya has respondido todas las preguntas de este tema";
- send_message($chat_id,$reply);
- leave_bot($chat_id);
- }
- // Mando una pregunta al azar: TEXTO + IMAGEN + EJEMPLO
- $id_ques=$row['id_ques'];
- $chap=$row['chap'];
- $text=$row['text'];
- $reply="PREG $id_ques TEMA $chap $text";
- send_message($chat_id,$reply);
- $file_url=SERVER."/bot_fluids/QUEST/$id_ques.jpg";
- send_doc($telegram,$chat_id,$file_url);
- $file_url=SERVER."/bot_fluids/QUEST/$id_ques-eje.jpg";
- send_doc($telegram,$chat_id,$file_url);
- // Fijo esa pregunta como activa
- set_pending($id_ques,$NIA);
- leave_bot($chat_id);
- } // FIN PREG
- // ----------------------------------------------------- /estado
- if ($words[0]=="/estado" && sizeof($words)==1) {
- // Cuento preguntas activas totales
- $sql="select q.chap, count(*) from botf_questions q, botf_chap ch where ch.chap=q.chap and now()>start group by q.chap order by q.chap";
- $resul=mysql_query($sql);
- $totpreg=0;
- while ( $row = mysql_fetch_array($resul) ) {
- $chap=$row[0];
- $npreg[$chap]=$row[1];
- $totpreg+=$row[1];
- }
- // Cuento preguntas respondidas de las activas
- $reply="Preguntas respondidas: \n";
- $sql="select q.chap, count(*),end from botf_questions q, botf_chap ch, botf_replies r where ch.chap=q.chap and r.id_ques=q.id_ques and NIA=$NIA and now()>start group by q.chap order by q.chap";
- $resul=mysql_query($sql);
- $totresp=0;
- while ( $row = mysql_fetch_array($resul) ) {
- $chap=$row[0];
- $reply.="Tema $chap: $row[1] de $npreg[$chap] (fecha limite: ".$row[2].")\n";
- $totresp+=$row[1];
- }
- $porcen=round($totresp*100/$totpreg);
- $reply.="TOTAL: $totresp de $totpreg ($porcen %)";
- send_message($chat_id,$reply);
- leave_bot($chat_id);
- } // FIN ESTADO
- // ----------------------------------------------------- /pendientes
- if ($words[0]=="/pendientes" && sizeof($words)==1) {
- $reply="CAPITULO - PREGUNTA \n";
- $sql="select cap,id1 from botf_chap c, (select q.chap as cap,q.id_ques as id1,r.id_ques as id2 from botf_questions q left join botf_replies r on nia=$NIA and q.id_ques=r.id_ques order by q.chap,q.id_ques) as subsel where cap=chap and now()>=start and id2 is null";
- $resul=mysql_query($sql);
- while ( $row = mysql_fetch_array($resul) ) {
- $chap=$row['cap'];
- $id_ques=$row['id1'];
- $reply.="$chap $id_ques \n";
- }
- send_message($chat_id,$reply);
- leave_bot($chat_id);
- } // FIN PENDIENTES
- // ----------------------------------------------------- Si es una foto
- if ($file_id!="") {
- // $method = API_URL."getFile?file_id=".$file_id;
- // $content = file_get_contents($method);
- // $file = json_decode($content, true);
- // $file_path=$file['result']['file_path'];
- // $telegram->downloadFile($file_path, "./foto.png");
- // reset_pending($NIA);
- $reply="Para mandar fotos, elige la opción ARCHIVO y luego selecciona la foto del carrete";
- send_message($chat_id,$reply);
- leave_bot($chat_id);
- }
- // ----------------------------------------------------- Si es un documento
- if ($file_id2!="") {
- $id_ques=get_current_question($NIA);
- // Si no hay pregunta activa
- if ($id_ques=="") {
- $reply="Debes elegir primero una pregunta";
- send_message($chat_id,$reply);
- leave_bot($chat_id);
- }
- // Mark it as replied
- mark_as_replied($id_ques,$NIA);
- // Get the doc and save it
- $method = API_URL."getFile?file_id=".$file_id2;
- $content = file_get_contents($method);
- $file = json_decode($content, true);
- $file_path=$file['result']['file_path'];
- $telegram->downloadFile($file_path, "./REPLIES/".$NIA."_".$id_ques.".jpg");
- send_message($chat_id,"Pregunta $id_ques respondida");
- // Send solution and reset pending
- $sql="select * from botf_questions where id_ques=$id_ques";
- $resul=mysql_query($sql);
- $row = mysql_fetch_array($resul);
- $sol=$row['sol'];
- $reply="SOLUC: $sol ";
- send_message($chat_id,$reply);
- // Manda jpg si existe
- $file_url=SERVER."/bot_fluids/QUEST/".$id_ques."-sol.jpg";
- send_doc($telegram,$chat_id,$file_url);
- reset_pending($NIA);
- leave_bot($chat_id);
- }
- // ----------------------------------------------------- Default reply
- $reply=$usage;
- if ($chat_id==CHAT_ID_JAVIER) $reply=$usage_admin;
- send_message($chat_id,$reply);
- // ==================== FUNCTIONS =====================
- // ==================== FUNCTIONS =====================
- // ==================== FUNCTIONS =====================
- //------------------------------- Manda un documento
- function send_doc($telegram,$chat_id,$file_url) {
- $content = array('chat_id' => $chat_id, 'document' => $file_url);
- $telegram->sendDocument($content);
- }
- //------------------------------- Add NIA to botf_matri
- function add_nia($NIA) {
- $sql="insert into botf_matri (NIA,correo) VALUES($NIA,'$NIA@unizar.es')";
- mysql_query($sql) or die($sql." ".mysql_error());
- }
- //------------------------------- Mark question as replied
- function mark_as_replied($id_ques,$NIA) {
- $sql="insert into botf_replies (NIA,id_ques,date) VALUES($NIA,$id_ques,now() )";
- mysql_query($sql) or die($sql." ".mysql_error());
- }
- //-------------------------------Get active question
- function get_current_question($NIA)
- {
- $sql="select id_ques from botf_matri where NIA=$NIA";
- $resul=mysql_query($sql);
- $row = mysql_fetch_array($resul);
- $id_ques=$row['id_ques'];
- return $id_ques;
- }
- //------------------------------- BORRA el id_ques de la pregunta en curso
- function reset_pending($NIA) {
- $sql="update botf_matri set id_ques=NULL where NIA=$NIA";
- mysql_query($sql) or die($sql." ".mysql_error());
- }
- //------------------------------- Fija el id_ques de la pregunta en curso
- function set_pending($id_ques,$NIA) {
- $sql="update botf_matri set id_ques=$id_ques where NIA=$NIA";
- mysql_query($sql) or die($sql." ".mysql_error());
- }
- //-------------------------------Función que dice si el chat_id está dado de alta
- function get_NIA($chat_id)
- {
- $sql="select * from botf_matri where chat_id=$chat_id";
- $resul=mysql_query($sql);
- $row = mysql_fetch_array($resul);
- $NIA=$row['NIA'];
- return $NIA;
- }
- // ----------------------------------------------------- FUNCTION to send messages
- function send_message($chat_id,$text) {
- // $new_text=utf8_encode($text);
- // $new_text=utf8_encode(urlencode($text));
- $new_text=urlencode(utf8_encode($text));
- $sendto =API_URL."sendmessage?chat_id=".$chat_id."&text=".$new_text;
- file_get_contents($sendto);
- }
- // ----------------------------------------------------- FUNCTION to send log info to Javier
- function debug($text) {
- send_message(301783337,"DEBUG ".$text);
- }
- //-------------------------------Función que manda contraseña
- function manda_correo($email,$asunto,$NIA,$texto_correo)
- {
- require_once("class.phpmailer.php");
- require_once("class.smtp.php");
- $nombre="Alumno";
- $cc=$NIA."@unizar.es";
- $mail = new PHPMailer();
- $mail->IsSMTP();
- //$mail->SMTPAuth = true;
- //$mail->SMTPSecure = "ssl";
- $mail->Host = "smtp.unizar.es";
- $mail->Port = 587;
- $mail->Username = "jablasal";
- $mail->Password = "XXX";
- $mail->From = "jablasal@unizar.es";
- $mail->FromName = "Javier Blasco";
- $mail->Subject = $asunto;
- $mail->MsgHTML($texto_correo);
- $mail->AddAddress($email, $nombre);
- $mail->AddAddress($cc, "usuario");
- $mail->IsHTML(true);
- if(!$mail->Send())
- return("Error: $email " . $mail->ErrorInfo);
- else
- return ("SENT");
- }
- //-------------------------------Función que manda contraseña
- function manda_contra($email,$contra)
- {
- require_once("class.phpmailer.php");
- require_once("class.smtp.php");
- $asunto="Contrasenia";
- $texto_correo="Su contrasenia es $contra";
- //$email="jaba09@gmail.com";
- $nombre="Alumno";
- $mail = new PHPMailer();
- $mail->IsSMTP();
- //$mail->SMTPAuth = true;
- //$mail->SMTPSecure = "ssl";
- $mail->Host = "smtp.unizar.es";
- $mail->Port = 587;
- $mail->Username = "jablasal";
- $mail->Password = "XXX";
- $mail->From = "jablasal@unizar.es";
- $mail->FromName = "Javier Blasco";
- $mail->Subject = $asunto;
- $mail->MsgHTML($texto_correo);
- $mail->AddAddress($email, $nombre);
- if ($cc!="") $mail->AddCC($cc, "usuario");
- $mail->IsHTML(true);
- if(!$mail->Send())
- return("Error: $email " . $mail->ErrorInfo);
- else
- return ("SENT");
- }
- //-------------------------------Función de salida que pone la fecha start a cero
- function leave_bot($chat_id) {
- // require_once("conexion_bd.php");
- // $sql="update botf_log set start=NULL where chat_id=$chat_id";
- // mysql_query($sql) or die($sql." ".mysql_error());
- // mysql_commit();
- http_response_code(200);
- exit();
- }
- //-------------------------------Función que obtiene correo (si está matriculado)
- function correo($NIA)
- {
- $sql="select * from botf_matri where nia=$NIA";
- $resul=mysql_query($sql);
- $row = mysql_fetch_array($resul);
- $email=$row['correo'];
- return ($email);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement