Advertisement
rfspd

Telegram PHP Bot

Apr 24th, 2020
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.94 KB | None | 0 0
  1. <?php
  2.  
  3. // telegram bot
  4. // https://t.me/Desconfinamiento_Bot
  5.  
  6. // bot de telegram que al compartir con él tu ubicación te responde con un mapa
  7. // con círculo de 1k de radio sobrepuesto en el mapa, para saber la distancia
  8. // permitida en el desconfinamiento que comienza el dia 26 de abril
  9. //
  10. // confinamiento motivado por el COVID-19.
  11.  
  12. // mapquest.com
  13. define ("MAP_API_KEY","###############################");
  14. define ("CACHE",dirname(__FILE__).'/data/cache/');
  15.  
  16. //telegram API
  17. define ("BOTTOKEN",'###############################');
  18.  
  19. // variables
  20.  
  21. $ayuda_es = "Envía tu localización de Telegram (automática o manualmente) y te mostramos un mapa con un círculo superpuesto con el radio de salida permitido.";
  22. $code_es = "El código PHP de este bot está disponible en: https://pastebin.com/m8YLuGvv";
  23. $start_es = "Visualiza el radio (1km) de salida permitido tras el inicio del desconfinamiento el 26 de abril de 2020.\n/help para las instrucciones.\n\nmade by @rfspd - a confined web developer - icon by flaticon web";
  24.  
  25. $website = 'https://api.telegram.org/bot'.BOTTOKEN;
  26.  
  27. // bot
  28.  
  29. $update_json = file_get_contents('php://input');
  30. $update = json_decode($update_json, true);
  31.  
  32. $chatId = $update['message']['chat']['id'];
  33. $messageId = $update['message']['message_id'];
  34. $chatType = filter_var($update['message']['chat']['type'], FILTER_SANITIZE_STRING);
  35.  
  36. $latitude = $update['message']['location']['latitude'];
  37. $longitude = $update['message']['location']['longitude'];
  38.  
  39. $radius = 1;
  40.  
  41. $message = strtolower($update['message']['text']);
  42.  
  43. $acciones = array('/help', '/last', '/start', '/ayuda', '/code');
  44.  
  45.  
  46.  
  47. if ($chatType!='private') {
  48.  
  49.   $response = 'Este bot es sólo para chats privados. '.$update_json;
  50.   sendResponse($chatId, $response);
  51.  
  52. } elseif (in_array($message, $acciones)) {
  53.  
  54.   switch ($message) {
  55.     case '/help':
  56.     case '/ayuda':
  57.       $response = urlencode($ayuda_es);
  58.       sendResponse($chatId, $response);
  59.       break;
  60.     case '/code':
  61.       $response = urlencode($code_es);
  62.       sendResponse($chatId, $response);
  63.       break;
  64.     case '/start':
  65.       $response = urlencode($start_es);
  66.       sendResponse($chatId, $response);
  67.       break;
  68.   }
  69.  
  70. } elseif ($latitude!='' && $longitude!='') {
  71.  
  72.   $cacheFile = md5(MAP_API_KEY.$chatId.$latitude.$longitude.'1').'.jpg';
  73.   $imageUrl = 'https://www.mapquestapi.com/staticmap/v5/map?key='.MAP_API_KEY.'&shape=radius:'.$radius.'km|weight:4|'.$latitude.','.$longitude.'|border:000000&banner=1km&size=600,1000@2x&type=map&locations='.$latitude.','.$longitude.'';
  74.   getImageCache($imageUrl, CACHE.$cacheFile);
  75.   sendImage($chatId, CACHE.$cacheFile);
  76.   deleteMessage($chatId, $messageId);
  77.  
  78. } elseif ($latitude=='' || $longitude=='') {
  79.  
  80.   $response = 'No se ha encontrado la localización.';
  81.   sendResponse($chatId, $response);
  82.  
  83. }
  84.  
  85. function sendResponse($chatId, $response) {
  86.   global $website;
  87.  
  88.   $url = $website.'/sendMessage?chat_id='.$chatId.'&parse_mode=HTML&text='.$response;
  89.   $response = file_get_contents($url);
  90. }
  91.  
  92. function getImageCache($imageUrl, $cacheFile) {
  93.     // capturamos la imagen momentaneamente...
  94.     file_put_contents($cacheFile, file_get_contents($imageUrl));
  95. }
  96.  
  97. function sendImage($chatId, $cacheFile) {
  98.   global $website;
  99.  
  100.   $url = $website . "/sendPhoto?chat_id=" . $chatId ;
  101.  
  102.   $post_fields = array(
  103.     'chat_id'   => $chatId,
  104.     'photo'     => new CURLFile(realpath($cacheFile))
  105.   );
  106.  
  107.   $ch = curl_init();
  108.   curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  109.       "Content-Type:multipart/form-data"
  110.   ));
  111.   curl_setopt($ch, CURLOPT_URL, $url);
  112.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  113.   curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
  114.   $output = curl_exec($ch);
  115.  
  116.   // nada más enviar borramos la imagen capturada.
  117.   @unlink($cacheFile);
  118.  
  119. }
  120.  
  121. function deleteMessage($chatId, $messageId) {
  122.   global $website;
  123.   $url = $website.'/deleteMessage?chat_id='.$chatId.'&message_id='.$messageId;
  124.   file_get_contents($url);
  125. }
  126.  
  127. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement