Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.95 KB | None | 0 0
  1. <?php
  2. define('BOT_TOKEN', 'XXX');
  3.  
  4. define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
  5. define('CHAT_ID_JAVIER', 'XXX');
  6.  
  7. define('SERVER', 'XXX');
  8.  
  9. require_once("conexion_bd.php");
  10. include("Telegram.php");
  11.  
  12.  
  13.  
  14.  
  15. // Create object
  16. $telegram = new Telegram(BOT_TOKEN);
  17.  
  18. // read incoming info and grab the chatID
  19. $content = file_get_contents("php://input");
  20. $update = json_decode($content, true);
  21. $chat_id = $update["message"]["chat"]["id"];
  22. $text = $update["message"]["text"];
  23. $words = explode(" ",$text);
  24.  
  25. // Photos
  26. $photo = $update["message"]["photo"][0];
  27. $file_size = $photo["file_size"];
  28. $file_id = $photo["file_id"];
  29.  
  30. // Documents
  31. $document = $update["message"]["document"];
  32. $file_id2 = $document["file_id"];
  33.  
  34. // Exit if call comes from HTTP (i.e. empy $update object)
  35. if ($update=="") exit("Page not found");
  36.  
  37.  
  38.  
  39. $usage="USO
  40. /pass NIA (manda contrasenia a tu correo)
  41. /alta NIA password (da de alta el NIA en el bot)
  42. /preg (manda una pregunta al azar)
  43. /preg_num (manda la pregunta con ese numero)
  44. /estado (cuenta preguntas respondidas por tema)
  45. /pendientes (lista de preguntas pendientes de contestar)
  46. /error texto del error (para avisar de un fallo)";
  47.  
  48. $usage_admin="ADMIN
  49. /add_nia /para_todos /contra_nia /para_nia /resumen
  50. ".$usage;
  51.  
  52.  
  53.  
  54.  
  55.  
  56. // ##########################
  57. // ########################## FROM THIS POINT, THE USER **MUST** BE REGISTERED
  58. // ##########################
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. // USUARIO **NO** DADO DE ALTA
  66. $NIA=get_NIA($chat_id);
  67. if ( $NIA=="" ) {
  68. $reply="Lo siento pero no estás dado de alta. Debes mandar /pass NIA";
  69. send_message($chat_id,$reply);
  70. leave_bot($chat_id);
  71. }
  72.  
  73.  
  74.  
  75.  
  76. // ----------------------------------------------------- /preg_num
  77. if ($words[0]=="/preg_num" && sizeof($words)==2) {
  78. $id_ques=$words[1];
  79.  
  80. // Busco esa pregunta
  81. // XXX Les dejo que respondan a preguntas "caducadas"
  82. $sql="select * from botf_questions where id_ques=$id_ques";
  83. $resul=mysql_query($sql);
  84. $row = mysql_fetch_array($resul);
  85.  
  86. // No existe ese id
  87. if ($row=="") {
  88. $reply="No existe una pregunta con el numero $id_ques";
  89. send_message($chat_id,$reply);
  90. leave_bot($chat_id);
  91. }
  92.  
  93. // Mando la pregunta: TEXTO + IMAGEN + EJEMPLO
  94. $id_ques=$row['id_ques'];
  95. $chap=$row['chap'];
  96. $text=$row['text'];
  97. $reply="PREG $id_ques TEMA $chap $text";
  98. send_message($chat_id,$reply);
  99.  
  100. $file_url=SERVER."/bot_fluids/QUEST/$id_ques.jpg";
  101. send_doc($telegram,$chat_id,$file_url);
  102.  
  103. $file_url=SERVER."/bot_fluids/QUEST/$id_ques-eje.jpg";
  104. send_doc($telegram,$chat_id,$file_url);
  105.  
  106. // Fijo esa pregunta como activa
  107. set_pending($id_ques,$NIA);
  108. leave_bot($chat_id);
  109. } // end preg_num
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. // ----------------------------------------------------- /error
  117. if ($words[0]=="/error" && sizeof($words)>=2) {
  118. // Mandar a Javier
  119. $mensaje="ERROR de $NIA: ".substr($text,7);
  120. $reply=manda_correo("jaba09@gmail.com","Bot: ERROR de $NIA",$NIA,$mensaje);
  121. debug("Correo error $NIA $reply: $mensaje");
  122.  
  123. // Gracias al alumno
  124. $reply="Muchas gracias por avisar de este error. El profesor lo revisara lo antes posible y te respondera por correo";
  125. send_message($chat_id,$reply);
  126. leave_bot($chat_id);
  127. }
  128.  
  129.  
  130.  
  131.  
  132.  
  133. // ----------------------------------------------------- /preg
  134. if ($words[0]=="/preg" && sizeof($words)==1) {
  135.  
  136. // Busco preguntas no respondidas
  137. $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()";
  138. $resul=mysql_query($sql);
  139. $row = mysql_fetch_array($resul);
  140.  
  141. // Ya no quedan preguntas
  142. if ($row=="") {
  143. $reply="Ya has respondido todas las preguntas de este tema";
  144. send_message($chat_id,$reply);
  145. leave_bot($chat_id);
  146. }
  147.  
  148. // Mando una pregunta al azar: TEXTO + IMAGEN + EJEMPLO
  149. $id_ques=$row['id_ques'];
  150. $chap=$row['chap'];
  151. $text=$row['text'];
  152. $reply="PREG $id_ques TEMA $chap $text";
  153. send_message($chat_id,$reply);
  154.  
  155. $file_url=SERVER."/bot_fluids/QUEST/$id_ques.jpg";
  156. send_doc($telegram,$chat_id,$file_url);
  157.  
  158. $file_url=SERVER."/bot_fluids/QUEST/$id_ques-eje.jpg";
  159. send_doc($telegram,$chat_id,$file_url);
  160.  
  161. // Fijo esa pregunta como activa
  162. set_pending($id_ques,$NIA);
  163. leave_bot($chat_id);
  164.  
  165. } // FIN PREG
  166.  
  167.  
  168.  
  169.  
  170.  
  171. // ----------------------------------------------------- /estado
  172. if ($words[0]=="/estado" && sizeof($words)==1) {
  173. // Cuento preguntas activas totales
  174. $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";
  175. $resul=mysql_query($sql);
  176. $totpreg=0;
  177. while ( $row = mysql_fetch_array($resul) ) {
  178. $chap=$row[0];
  179. $npreg[$chap]=$row[1];
  180. $totpreg+=$row[1];
  181. }
  182.  
  183. // Cuento preguntas respondidas de las activas
  184. $reply="Preguntas respondidas: \n";
  185. $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";
  186. $resul=mysql_query($sql);
  187. $totresp=0;
  188. while ( $row = mysql_fetch_array($resul) ) {
  189. $chap=$row[0];
  190. $reply.="Tema $chap: $row[1] de $npreg[$chap] (fecha limite: ".$row[2].")\n";
  191. $totresp+=$row[1];
  192. }
  193. $porcen=round($totresp*100/$totpreg);
  194. $reply.="TOTAL: $totresp de $totpreg ($porcen %)";
  195. send_message($chat_id,$reply);
  196. leave_bot($chat_id);
  197. } // FIN ESTADO
  198.  
  199.  
  200.  
  201.  
  202. // ----------------------------------------------------- /pendientes
  203. if ($words[0]=="/pendientes" && sizeof($words)==1) {
  204. $reply="CAPITULO - PREGUNTA \n";
  205. $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";
  206. $resul=mysql_query($sql);
  207. while ( $row = mysql_fetch_array($resul) ) {
  208. $chap=$row['cap'];
  209. $id_ques=$row['id1'];
  210. $reply.="$chap $id_ques \n";
  211. }
  212.  
  213. send_message($chat_id,$reply);
  214. leave_bot($chat_id);
  215. } // FIN PENDIENTES
  216.  
  217.  
  218.  
  219.  
  220.  
  221. // ----------------------------------------------------- Si es una foto
  222. if ($file_id!="") {
  223. // $method = API_URL."getFile?file_id=".$file_id;
  224. // $content = file_get_contents($method);
  225. // $file = json_decode($content, true);
  226. // $file_path=$file['result']['file_path'];
  227. // $telegram->downloadFile($file_path, "./foto.png");
  228. // reset_pending($NIA);
  229. $reply="Para mandar fotos, elige la opción ARCHIVO y luego selecciona la foto del carrete";
  230. send_message($chat_id,$reply);
  231. leave_bot($chat_id);
  232. }
  233.  
  234.  
  235.  
  236. // ----------------------------------------------------- Si es un documento
  237. if ($file_id2!="") {
  238. $id_ques=get_current_question($NIA);
  239. // Si no hay pregunta activa
  240. if ($id_ques=="") {
  241. $reply="Debes elegir primero una pregunta";
  242. send_message($chat_id,$reply);
  243. leave_bot($chat_id);
  244. }
  245.  
  246. // Mark it as replied
  247. mark_as_replied($id_ques,$NIA);
  248.  
  249. // Get the doc and save it
  250. $method = API_URL."getFile?file_id=".$file_id2;
  251. $content = file_get_contents($method);
  252. $file = json_decode($content, true);
  253. $file_path=$file['result']['file_path'];
  254. $telegram->downloadFile($file_path, "./REPLIES/".$NIA."_".$id_ques.".jpg");
  255. send_message($chat_id,"Pregunta $id_ques respondida");
  256.  
  257. // Send solution and reset pending
  258. $sql="select * from botf_questions where id_ques=$id_ques";
  259. $resul=mysql_query($sql);
  260. $row = mysql_fetch_array($resul);
  261. $sol=$row['sol'];
  262. $reply="SOLUC: $sol ";
  263. send_message($chat_id,$reply);
  264.  
  265. // Manda jpg si existe
  266. $file_url=SERVER."/bot_fluids/QUEST/".$id_ques."-sol.jpg";
  267. send_doc($telegram,$chat_id,$file_url);
  268.  
  269. reset_pending($NIA);
  270. leave_bot($chat_id);
  271. }
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278. // ----------------------------------------------------- Default reply
  279.  
  280. $reply=$usage;
  281. if ($chat_id==CHAT_ID_JAVIER) $reply=$usage_admin;
  282. send_message($chat_id,$reply);
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300. // ==================== FUNCTIONS =====================
  301. // ==================== FUNCTIONS =====================
  302. // ==================== FUNCTIONS =====================
  303.  
  304.  
  305.  
  306. //------------------------------- Manda un documento
  307. function send_doc($telegram,$chat_id,$file_url) {
  308. $content = array('chat_id' => $chat_id, 'document' => $file_url);
  309. $telegram->sendDocument($content);
  310. }
  311.  
  312. //------------------------------- Add NIA to botf_matri
  313. function add_nia($NIA) {
  314. $sql="insert into botf_matri (NIA,correo) VALUES($NIA,'$NIA@unizar.es')";
  315. mysql_query($sql) or die($sql." ".mysql_error());
  316. }
  317.  
  318.  
  319.  
  320. //------------------------------- Mark question as replied
  321. function mark_as_replied($id_ques,$NIA) {
  322. $sql="insert into botf_replies (NIA,id_ques,date) VALUES($NIA,$id_ques,now() )";
  323. mysql_query($sql) or die($sql." ".mysql_error());
  324. }
  325.  
  326.  
  327.  
  328. //-------------------------------Get active question
  329. function get_current_question($NIA)
  330. {
  331. $sql="select id_ques from botf_matri where NIA=$NIA";
  332. $resul=mysql_query($sql);
  333. $row = mysql_fetch_array($resul);
  334. $id_ques=$row['id_ques'];
  335. return $id_ques;
  336. }
  337.  
  338.  
  339. //------------------------------- BORRA el id_ques de la pregunta en curso
  340. function reset_pending($NIA) {
  341. $sql="update botf_matri set id_ques=NULL where NIA=$NIA";
  342. mysql_query($sql) or die($sql." ".mysql_error());
  343. }
  344.  
  345.  
  346.  
  347. //------------------------------- Fija el id_ques de la pregunta en curso
  348. function set_pending($id_ques,$NIA) {
  349. $sql="update botf_matri set id_ques=$id_ques where NIA=$NIA";
  350. mysql_query($sql) or die($sql." ".mysql_error());
  351. }
  352.  
  353.  
  354.  
  355. //-------------------------------Función que dice si el chat_id está dado de alta
  356. function get_NIA($chat_id)
  357. {
  358. $sql="select * from botf_matri where chat_id=$chat_id";
  359. $resul=mysql_query($sql);
  360. $row = mysql_fetch_array($resul);
  361. $NIA=$row['NIA'];
  362. return $NIA;
  363. }
  364.  
  365.  
  366. // ----------------------------------------------------- FUNCTION to send messages
  367. function send_message($chat_id,$text) {
  368. // $new_text=utf8_encode($text);
  369. // $new_text=utf8_encode(urlencode($text));
  370. $new_text=urlencode(utf8_encode($text));
  371. $sendto =API_URL."sendmessage?chat_id=".$chat_id."&text=".$new_text;
  372. file_get_contents($sendto);
  373. }
  374.  
  375.  
  376. // ----------------------------------------------------- FUNCTION to send log info to Javier
  377. function debug($text) {
  378. send_message(301783337,"DEBUG ".$text);
  379. }
  380.  
  381.  
  382.  
  383. //-------------------------------Función que manda contraseña
  384. function manda_correo($email,$asunto,$NIA,$texto_correo)
  385. {
  386.  
  387. require_once("class.phpmailer.php");
  388. require_once("class.smtp.php");
  389.  
  390. $nombre="Alumno";
  391. $cc=$NIA."@unizar.es";
  392.  
  393.  
  394. $mail = new PHPMailer();
  395. $mail->IsSMTP();
  396. //$mail->SMTPAuth = true;
  397. //$mail->SMTPSecure = "ssl";
  398. $mail->Host = "smtp.unizar.es";
  399. $mail->Port = 587;
  400. $mail->Username = "jablasal";
  401. $mail->Password = "XXX";
  402.  
  403. $mail->From = "jablasal@unizar.es";
  404. $mail->FromName = "Javier Blasco";
  405. $mail->Subject = $asunto;
  406. $mail->MsgHTML($texto_correo);
  407. $mail->AddAddress($email, $nombre);
  408. $mail->AddAddress($cc, "usuario");
  409. $mail->IsHTML(true);
  410.  
  411. if(!$mail->Send())
  412. return("Error: $email " . $mail->ErrorInfo);
  413. else
  414. return ("SENT");
  415. }
  416.  
  417.  
  418.  
  419. //-------------------------------Función que manda contraseña
  420. function manda_contra($email,$contra)
  421. {
  422.  
  423. require_once("class.phpmailer.php");
  424. require_once("class.smtp.php");
  425.  
  426. $asunto="Contrasenia";
  427. $texto_correo="Su contrasenia es $contra";
  428. //$email="jaba09@gmail.com";
  429. $nombre="Alumno";
  430.  
  431.  
  432. $mail = new PHPMailer();
  433. $mail->IsSMTP();
  434. //$mail->SMTPAuth = true;
  435. //$mail->SMTPSecure = "ssl";
  436. $mail->Host = "smtp.unizar.es";
  437. $mail->Port = 587;
  438. $mail->Username = "jablasal";
  439. $mail->Password = "XXX";
  440.  
  441. $mail->From = "jablasal@unizar.es";
  442. $mail->FromName = "Javier Blasco";
  443. $mail->Subject = $asunto;
  444. $mail->MsgHTML($texto_correo);
  445. $mail->AddAddress($email, $nombre);
  446. if ($cc!="") $mail->AddCC($cc, "usuario");
  447. $mail->IsHTML(true);
  448.  
  449. if(!$mail->Send())
  450. return("Error: $email " . $mail->ErrorInfo);
  451. else
  452. return ("SENT");
  453. }
  454.  
  455.  
  456.  
  457. //-------------------------------Función de salida que pone la fecha start a cero
  458. function leave_bot($chat_id) {
  459. // require_once("conexion_bd.php");
  460.  
  461. // $sql="update botf_log set start=NULL where chat_id=$chat_id";
  462. // mysql_query($sql) or die($sql." ".mysql_error());
  463. // mysql_commit();
  464. http_response_code(200);
  465. exit();
  466. }
  467.  
  468. //-------------------------------Función que obtiene correo (si está matriculado)
  469. function correo($NIA)
  470. {
  471. $sql="select * from botf_matri where nia=$NIA";
  472. $resul=mysql_query($sql);
  473. $row = mysql_fetch_array($resul);
  474. $email=$row['correo'];
  475. return ($email);
  476. }
  477.  
  478.  
  479. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement