Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // telegram bot
- // https://t.me/Desconfinamiento_Bot
- // bot de telegram que al compartir con él tu ubicación te responde con un mapa
- // con círculo de 1k de radio sobrepuesto en el mapa, para saber la distancia
- // permitida en el desconfinamiento que comienza el dia 26 de abril
- //
- // confinamiento motivado por el COVID-19.
- // mapquest.com
- define ("MAP_API_KEY","###############################");
- define ("CACHE",dirname(__FILE__).'/data/cache/');
- //telegram API
- define ("BOTTOKEN",'###############################');
- // variables
- $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.";
- $code_es = "El código PHP de este bot está disponible en: https://pastebin.com/m8YLuGvv";
- $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";
- $website = 'https://api.telegram.org/bot'.BOTTOKEN;
- // bot
- $update_json = file_get_contents('php://input');
- $update = json_decode($update_json, true);
- $chatId = $update['message']['chat']['id'];
- $messageId = $update['message']['message_id'];
- $chatType = filter_var($update['message']['chat']['type'], FILTER_SANITIZE_STRING);
- $latitude = $update['message']['location']['latitude'];
- $longitude = $update['message']['location']['longitude'];
- $radius = 1;
- $message = strtolower($update['message']['text']);
- $acciones = array('/help', '/last', '/start', '/ayuda', '/code');
- if ($chatType!='private') {
- $response = 'Este bot es sólo para chats privados. '.$update_json;
- sendResponse($chatId, $response);
- } elseif (in_array($message, $acciones)) {
- switch ($message) {
- case '/help':
- case '/ayuda':
- $response = urlencode($ayuda_es);
- sendResponse($chatId, $response);
- break;
- case '/code':
- $response = urlencode($code_es);
- sendResponse($chatId, $response);
- break;
- case '/start':
- $response = urlencode($start_es);
- sendResponse($chatId, $response);
- break;
- }
- } elseif ($latitude!='' && $longitude!='') {
- $cacheFile = md5(MAP_API_KEY.$chatId.$latitude.$longitude.'1').'.jpg';
- $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.'';
- getImageCache($imageUrl, CACHE.$cacheFile);
- sendImage($chatId, CACHE.$cacheFile);
- deleteMessage($chatId, $messageId);
- } elseif ($latitude=='' || $longitude=='') {
- $response = 'No se ha encontrado la localización.';
- sendResponse($chatId, $response);
- }
- function sendResponse($chatId, $response) {
- global $website;
- $url = $website.'/sendMessage?chat_id='.$chatId.'&parse_mode=HTML&text='.$response;
- $response = file_get_contents($url);
- }
- function getImageCache($imageUrl, $cacheFile) {
- // capturamos la imagen momentaneamente...
- file_put_contents($cacheFile, file_get_contents($imageUrl));
- }
- function sendImage($chatId, $cacheFile) {
- global $website;
- $url = $website . "/sendPhoto?chat_id=" . $chatId ;
- $post_fields = array(
- 'chat_id' => $chatId,
- 'photo' => new CURLFile(realpath($cacheFile))
- );
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- "Content-Type:multipart/form-data"
- ));
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
- $output = curl_exec($ch);
- // nada más enviar borramos la imagen capturada.
- @unlink($cacheFile);
- }
- function deleteMessage($chatId, $messageId) {
- global $website;
- $url = $website.'/deleteMessage?chat_id='.$chatId.'&message_id='.$messageId;
- file_get_contents($url);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement