├── .htaccess ├── Procfile ├── README.md ├── composer.json ├── config.json ├── images ├── .htaccess └── senor.jpg ├── index.php ├── lib ├── .htaccess ├── athletic.php ├── curl.php ├── estropadak.php ├── imgur.php └── telegram.php ├── tmp ├── .htaccess └── imgur │ └── .htaccess └── view ├── .htaccess └── default.tpl /.htaccess: -------------------------------------------------------------------------------- 1 | 2 | Order allow,deny 3 | Deny from all 4 | 5 | 6 | RewriteEngine on 7 | RewriteCond %{REQUEST_FILENAME} !-d 8 | RewriteCond %{REQUEST_FILENAME} !-f 9 | RewriteRule . index.php [L] 10 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: vendor/bin/heroku-php-apache2 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | php-telebot-heroku 2 | ================== 3 | PHP Telegram BOT for Heroku 4 | 5 | EDIT: **config.json** with **Heroku APP ID** and **TELEGRAM API KEY** 6 | 7 | EDIT: **lib/telegram.php** to write your commands 8 | 9 | Get Telegram API Key: https://core.telegram.org/bots#botfather 10 | 11 | GNU/Linux Instructions: 12 | ======================= 13 | 14 | Create an account in Heroku: 15 | https://www.heroku.com 16 | 17 | Create a new app in Heroku: 18 | https://dashboard.heroku.com/new 19 | 20 | Install Heroku 21 | ``` 22 | su 23 | wget -O- https://toolbelt.heroku.com/install-ubuntu.sh | sh 24 | exit 25 | ``` 26 | 27 | Download project: 28 | ``` 29 | mkdir -p /home/projects 30 | cd /home/projects 31 | git clone https://github.com/ZiTAL/php-telebot-heroku.git 32 | cd /home/projects/php-telebot-heroku 33 | rm -rf .git 34 | ``` 35 | 36 | Connect the project to Heroku's git: 37 | ``` 38 | heroku login 39 | heroku create 40 | git init 41 | ``` 42 | Edit **config.json** with **HEROKU APP ID** and **TELEGRAM API KEY** 43 | Upload changes to Heroku and Deploy the application: 44 | ``` 45 | git add . 46 | git commit -m "my first commit" 47 | heroku git:remote -a HEROKU_APP_ID 48 | git push heroku master 49 | ``` 50 | 51 | License 52 | ======= 53 | GPLv3 54 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | {} 2 | -------------------------------------------------------------------------------- /config.json: -------------------------------------------------------------------------------- 1 | { 2 | "APP_ID": "change with heroku app key", 3 | "TELEGRAM_API_KEY": "change with telegram api key" 4 | } 5 | -------------------------------------------------------------------------------- /images/.htaccess: -------------------------------------------------------------------------------- 1 | deny from all 2 | -------------------------------------------------------------------------------- /images/senor.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZiTAL/php-telebot-heroku/3f9b7ee3844133f1a5f34ef8fd05a454427d8e10/images/senor.jpg -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | parse($result); 19 | 20 | break; 21 | } 22 | case '/getUpdates': 23 | { 24 | $offset = null; 25 | $method = 'getUpdates'; 26 | 27 | $params = array 28 | ( 29 | 'offset' => $offset, 30 | 'limit' => null, 31 | 'timeout' => null 32 | ); 33 | 34 | do 35 | { 36 | $result = $telegram->request($method, $params); 37 | 38 | echo "
";
 39 | 			print_r($result);
 40 | 			echo "
"; 41 | 42 | // parse each comment 43 | foreach($result['result'] as $r) 44 | { 45 | $params['offset'] = $r['update_id']; 46 | $telegram->parse($r); 47 | } 48 | 49 | // to remove notifications, else always get the last 50 | $params['offset']++; 51 | } 52 | while($result['ok']===true && count($result['result'])>0); 53 | break; 54 | } 55 | 56 | case '/setWebhook': 57 | { 58 | $method = 'setWebhook'; 59 | $url = "https://".$config['APP_ID'].".herokuapp.com/webHookUpdates"; 60 | $params = array 61 | ( 62 | 'url' => $url 63 | ); 64 | $result = $telegram->request($method, $params); 65 | 66 | echo "
";
 67 | 		print_r($result);
 68 | 		echo "
"; 69 | 70 | break; 71 | } 72 | 73 | case '/unsetWebhook': 74 | { 75 | $method = 'setWebhook'; 76 | $params = array 77 | ( 78 | 'url' => null 79 | ); 80 | $result = $telegram->request($method, $params); 81 | 82 | echo "
";
 83 |                 print_r($result);
 84 |                 echo "
"; 85 | 86 | break; 87 | } 88 | 89 | default: 90 | { 91 | include('view/default.tpl'); 92 | } 93 | } 94 | 95 | function getRequest() 96 | { 97 | $postdata = file_get_contents("php://input"); 98 | $json = json_decode($postdata, true); 99 | if($json) 100 | return $json; 101 | return $postdata; 102 | } 103 | -------------------------------------------------------------------------------- /lib/.htaccess: -------------------------------------------------------------------------------- 1 | deny from all 2 | -------------------------------------------------------------------------------- /lib/athletic.php: -------------------------------------------------------------------------------- 1 | request($url, 'GET'); 14 | 15 | $dom = new DOMDocument('1.0', 'utf-8'); 16 | @$dom->loadHTML($r); 17 | 18 | $xpath = new DOMXpath($dom); 19 | 20 | $trs = $xpath->query('//table[@class="table table-striped"]/tbody/tr'); 21 | 22 | $result = array(); 23 | foreach($trs as $tr) 24 | { 25 | $params = array(); 26 | 27 | $tds = $xpath->query('td', $tr); 28 | $i = 0; 29 | foreach($tds as $td) 30 | { 31 | $text = trim($td->textContent); 32 | $text = utf8_decode($text); 33 | // ENTER-ak kendu 34 | $text = preg_replace("/\r\n/", '', $text); 35 | 36 | switch($i) 37 | { 38 | case 1: // data 39 | if(preg_match("/([0-9]{2})\/([0-9]{2})\/([0-9]{4})\s+([0-9]{2})\:([0-9]{2})h./", $text, $m)) 40 | $text = "{$m[3]}/{$m[2]}/{$m[1]} {$m[4]}:{$m[5]}"; 41 | elseif(preg_match("/([0-9]{2})\/([0-9]{2})\/([0-9]{4})/", $text, $m)) 42 | $text = "{$m[3]}/{$m[2]}/{$m[1]}"; 43 | 44 | $params['data'] = $text; 45 | break; 46 | case 2: // partidu mota 47 | if(preg_match("/Liga 1ª DivisiónJornada ([0-9]+)/", $text, $m)) 48 | { 49 | if(intval($m[1])<10) 50 | $m[1] = "0{$m[1]}"; 51 | 52 | $text = "Ligie {$m[1]}"; 53 | } 54 | elseif(preg_match("/UEFA/", $text)) 55 | $text = 'UEFA'; 56 | elseif(preg_match("/Amistoso/", $text)) 57 | $text = 'Amistosue'; 58 | elseif(preg_match("/copa/i", $text)) 59 | $text = 'Kopie'; 60 | 61 | $params['mota'] = $text; 62 | break; 63 | case 3: // etxeko taldea 64 | $params['etxekoa'] = $text; 65 | break; 66 | case 4: // emaitza 67 | $params['emaitza'] = $text; 68 | break; 69 | case 5: // kanpoko taldea 70 | $params['kanpokoa'] = $text; 71 | break; 72 | default: 73 | break; 74 | } 75 | $i++; 76 | } 77 | $result[] = $params; 78 | } 79 | $txt = ""; 80 | foreach($result as $r) 81 | { 82 | $txt.="{$r['mota']}: {$r['etxekoa']} - {$r['kanpokoa']}"; 83 | if($r['emaitza']!='-') 84 | $txt.= " ({$r['emaitza']})"; 85 | $txt.=" {$r['data']}\n"; 86 | } 87 | return $txt; 88 | } 89 | } -------------------------------------------------------------------------------- /lib/curl.php: -------------------------------------------------------------------------------- 1 | init(); 15 | } 16 | 17 | private function init() 18 | { 19 | $this->curl_obj = curl_init(); 20 | } 21 | 22 | public function request($url, $method = 'GET', $params = array(), $opts = array()) 23 | { 24 | $method = trim(strtoupper($method)); 25 | 26 | // default opts 27 | $opts[CURLOPT_FOLLOWLOCATION] = true; 28 | $opts[CURLOPT_RETURNTRANSFER] = 1; 29 | //$opts[CURLOPT_FRESH_CONNECT] = true; 30 | 31 | if($method==='GET' && count($params)>0) 32 | { 33 | $params = http_build_query($params); 34 | $url .= "?".$params; 35 | } 36 | elseif($method==='POST') 37 | { 38 | $opts[CURLOPT_POST] = 1; 39 | $opts[CURLOPT_POSTFIELDS] = $params; 40 | } 41 | 42 | $opts[CURLOPT_URL] = $url; 43 | 44 | curl_setopt_array($this->curl_obj, $opts); 45 | 46 | $content = curl_exec($this->curl_obj); 47 | return $content; 48 | } 49 | 50 | public function close() 51 | { 52 | if(gettype($this->curl_obj) === 'resource') 53 | curl_close($this->curl_obj); 54 | } 55 | 56 | public function __destruct() 57 | { 58 | $this->close(); 59 | } 60 | } -------------------------------------------------------------------------------- /lib/estropadak.php: -------------------------------------------------------------------------------- 1 | request($url, 'GET'); 16 | $r = json_decode($r, true); 17 | 18 | $result = array 19 | ( 20 | 'calendar' => array(), 21 | 'clasif' => array() 22 | ); 23 | 24 | foreach($r['items'] as $data) 25 | { 26 | $data['iz'] = html_entity_decode($data['iz']); 27 | $data['le'] = html_entity_decode($data['le']); 28 | $data['da'] = preg_replace("/^([0-9]{2})\-([0-9]{2})\-([0-9]{4})$/", '$3-$2-$1', $data['da']); 29 | $data['or'] = preg_replace("/\./", ':', $data['or']); 30 | 31 | $date = trim($data['da']." ".$data['or']); 32 | preg_match("/^([0-9]{4})\-([0-9]{2})\-([0-9]{2})\ ([0-9]{2}):([0-9]{2})$/", $date, $m); 33 | 34 | $date = array 35 | ( 36 | 'Y' => $m[1], 37 | 'm' => $m[2], 38 | 'd' => $m[3], 39 | 'H' => $m[4], 40 | 'i' => $m[5], 41 | 's' => '00' 42 | ); 43 | 44 | $date = mktime($date['H'], $date['i'], $date['s'], $date['m'], $date['d'], $date['Y']); 45 | $now = time(); 46 | 47 | if($date < $now) 48 | $data['ta'] = $this->getWinner($data['id']); 49 | else 50 | $data['ta'] = ''; 51 | 52 | $result['calendar'][] = $data; 53 | } 54 | 55 | $result['clasif'] = $this->getClasif($year); 56 | 57 | return $result; 58 | } 59 | 60 | private function getWinner($id) 61 | { 62 | $url = "http://www.euskolabelliga.com/json/emaitzakLSM.php?e={$id}"; 63 | 64 | $c = new curl(); 65 | $r = $c->request($url, 'GET'); 66 | $r = json_decode($r, true); 67 | 68 | foreach($r['items'] as $data) 69 | { 70 | if($data['po']===1) 71 | return $data['ta']; 72 | } 73 | return ''; 74 | } 75 | 76 | private function getClasif($year) 77 | { 78 | $url = "http://www.euskolabelliga.com/json/sailkapenaLSM.php?d={$year}"; 79 | 80 | $c = new curl(); 81 | $r = $c->request($url, 'GET'); 82 | $r = json_decode($r, true); 83 | 84 | $result = array(); 85 | 86 | foreach($r['items'] as $data) 87 | { 88 | $data['iz'] = html_entity_decode($data['iz']); 89 | $result[] = $data; 90 | } 91 | return $result; 92 | } 93 | } -------------------------------------------------------------------------------- /lib/imgur.php: -------------------------------------------------------------------------------- 1 | url = preg_replace("/#tag#/", $tag, $this->url); 16 | $this->folder = PATH.preg_replace("/#tag#/", $tag, $this->folder); 17 | 18 | if(!is_dir($this->folder)) 19 | mkdir($this->folder); 20 | 21 | $this->folder .= $chat_id."/"; 22 | 23 | if(!is_dir($this->folder)) 24 | mkdir($this->folder); 25 | 26 | $this->curl_obj = new curl(); 27 | } 28 | 29 | public function getResource() 30 | { 31 | $found = false; 32 | $i = 0; 33 | do 34 | { 35 | $url = preg_replace("/#index#/", $i, $this->url); 36 | 37 | $opts = $this->getCurlOpts(); 38 | 39 | $r = $this->curl_obj->request($url, 'GET', array(), $opts); 40 | 41 | $dom = $this->createHtmlDom($r); 42 | 43 | $links = $this->getLinks($dom); 44 | 45 | $multimedias = $this->getMultimedias($links); 46 | 47 | $file = $this->getFile($multimedias, true); 48 | if($file!==false) 49 | return $file; 50 | } 51 | while($found===false); 52 | } 53 | 54 | private function getCurlOpts() 55 | { 56 | $opts = array 57 | ( 58 | CURLOPT_HTTPHEADER => array 59 | ( 60 | 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 61 | 'Accept-Encoding' => 'gzip, deflate, sdch', 62 | 'Accept-Language' => 'en-US,en;q=0.8,es;q=0.6', 63 | 'Connection' => 'keep-alive', 64 | 'Host' => 'imgur.com', 65 | 'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/43.0.2357.130 Chrome/43.0.2357.130 Safari/537.36' 66 | ) 67 | ); 68 | 69 | return $opts; 70 | } 71 | 72 | private function createHtmlDom($html) 73 | { 74 | $html = " 75 | 76 | a 77 | 78 | ".$html." 79 | 80 | "; 81 | 82 | $dom = new DOMDocument(); 83 | $dom->loadHTML($html); 84 | 85 | return $dom; 86 | } 87 | 88 | private function getLinks($dom) 89 | { 90 | $xpath = new DOMXPath($dom); 91 | 92 | $links = array(); 93 | $as = $xpath->query("//a[@class=\"image-list-link\"]"); 94 | foreach($as as $a) 95 | { 96 | $href = $a->getAttribute('href'); 97 | $href = preg_replace("/^\/\//", '', $href); 98 | $href = "https://imgur.com{$href}"; 99 | $links[] = $href; 100 | } 101 | return $links; 102 | } 103 | 104 | private function getMultimedias($links) 105 | { 106 | $images = array(); 107 | foreach($links as $link) 108 | { 109 | $image = ''; 110 | 111 | $r = $this->curl_obj->request($link); 112 | 113 | $dom = new DOMDocument(); 114 | @$dom->loadHTML($r); 115 | 116 | $xpath = new DOMXPath($dom); 117 | 118 | $imgs = $xpath->query("//link[@rel=\"image_src\"]"); 119 | if($imgs->length>0) 120 | { 121 | $image = $imgs->item(0); 122 | $image = $image->getAttribute('href'); 123 | } 124 | if($image!=='') 125 | $images[] = $image; 126 | } 127 | return $images; 128 | } 129 | 130 | private function getFile($multimedias, $shuffle = false) 131 | { 132 | if($shuffle) 133 | shuffle($multimedias); 134 | 135 | foreach($multimedias as $image) 136 | { 137 | $basename = basename($image); 138 | $filename = $this->folder.$basename; 139 | if(!is_file(realpath($filename))) 140 | { 141 | $content = $this->curl_obj->request($image); 142 | file_put_contents($filename, $content); 143 | 144 | return $filename; 145 | } 146 | } 147 | return false; 148 | } 149 | } -------------------------------------------------------------------------------- /lib/telegram.php: -------------------------------------------------------------------------------- 1 | api_key = $api_key; 12 | } 13 | 14 | public function request($method, $params = array()) 15 | { 16 | $c = new curl(); 17 | $r = $c->request($this->url.$this->api_key."/".$method, 'POST', $params); 18 | 19 | $j = json_decode($r, true); 20 | if($j) 21 | return $j; 22 | else 23 | return $r; 24 | } 25 | 26 | public function parse($item) 27 | { 28 | $text = $item['message']['text']; 29 | $chat_id = $item['message']['chat']['id']; 30 | $reply_to_message_id = $item['message']['message_id']; 31 | 32 | $text = preg_replace("/@[^$]+$/", '', $text); 33 | 34 | // txupintzako tranpie 35 | if(preg_match("/^\/[a-z]+arrote$/", $text) && $text!=='/garrote') 36 | $text = '/qarrote'; 37 | 38 | switch($text) 39 | { 40 | case '/papeo': 41 | { 42 | $response = "Gai Sai: 946884998 43 | Itxas Gane: 946880946 44 | Izaro: 946881112 45 | Izokin: 946884891 46 | Kebab: 946477895 47 | Zokoa: 946028395 48 | Napolis: 946186385 49 | Txurrerue: 946882073 50 | Txinue: 946028392"; 51 | 52 | $params = array 53 | ( 54 | 'chat_id' => $chat_id, 55 | 'text' => $response, 56 | 'disable_web_page_preview' => null, 57 | 'reply_to_message_id' => $reply_to_message_id 58 | ); 59 | $this->request('sendMessage', $params); 60 | break; 61 | } 62 | 63 | case '/estropadak': 64 | { 65 | include('estropadak.php'); 66 | $a = new estropadak(); 67 | $response = $a->get(date('Y')); 68 | 69 | $text = ""; 70 | foreach($response['calendar'] as $calendar) 71 | { 72 | $text.= $calendar['iz']."\n"; 73 | 74 | for($i=0; $i0) 89 | $text.= " - banderak: ".$clasif['ba']; 90 | $text.= "\n"; 91 | } 92 | 93 | $params = array 94 | ( 95 | 'chat_id' => $chat_id, 96 | 'text' => $text, 97 | 'disable_web_page_preview' => null, 98 | 'reply_to_message_id' => $reply_to_message_id 99 | ); 100 | $this->request('sendMessage', $params); 101 | break; 102 | } 103 | 104 | case '/athletic': 105 | { 106 | include('athletic.php'); 107 | $response = athletic::get(); 108 | 109 | 110 | $partiduek = preg_split("/\n/", $response); 111 | 112 | $now = mktime(0, 0, 0, date('m'), date('d'), date('Y')); 113 | 114 | $tmp = ""; 115 | $i = 0; 116 | foreach($partiduek as $partidue) 117 | { 118 | preg_match("/([0-9]{4})\/([0-9]{2})\/([0-9]{2})/", $partidue, $m); 119 | 120 | $p_time = mktime(0, 0, 0, $m[2], $m[3], $m[1]); 121 | if($p_time>=$now) 122 | { 123 | if($i===0) 124 | $h_partidue = $partidue; 125 | $tmp.=$partidue."\n\n"; 126 | $i++; 127 | } 128 | } 129 | 130 | if(isset($h_partidue)) 131 | $tmp.="\nHurrengo partidue:\n".$h_partidue; 132 | 133 | $response = $tmp; 134 | 135 | $params = array 136 | ( 137 | 'chat_id' => $chat_id, 138 | 'text' => $response, 139 | 'disable_web_page_preview' => null, 140 | 'reply_to_message_id' => $reply_to_message_id 141 | ); 142 | $this->request('sendMessage', $params); 143 | break; 144 | } 145 | case '/garrote': 146 | { 147 | include('imgur.php'); 148 | 149 | //$imgur = new imgur('spaceporn', $chat_id); 150 | $imgur = new imgur('nsfw', $chat_id); 151 | $filename = $imgur->getResource(); 152 | 153 | if(class_exists('CURLFile')) 154 | $cfile = new CURLFile($filename); 155 | else 156 | $cfile = "@".$filename; 157 | 158 | $params = array 159 | ( 160 | 'chat_id' => $chat_id, 161 | 'photo' => $cfile, 162 | 'reply_to_message_id' => $reply_to_message_id, 163 | 'reply_markup' => null 164 | ); 165 | 166 | $method = 'sendPhoto'; 167 | if(preg_match("/\.mp4$/", $filename)) 168 | { 169 | unset($params['photo']); 170 | $params['video'] = $cfile; 171 | $method = 'sendVideo'; 172 | } 173 | 174 | $this->request($method, $params); 175 | /* 176 | 177 | $params = array 178 | ( 179 | 'chat_id' => $chat_id, 180 | 'text' => "debug: ".$filename, 181 | 'disable_web_page_preview' => null, 182 | 'reply_to_message_id' => $reply_to_message_id 183 | ); 184 | $this->request('sendMessage', $params); 185 | */ 186 | 187 | file_put_contents($filename, ''); 188 | break; 189 | } 190 | 191 | case '/andobuek': 192 | { 193 | include('imgur.php'); 194 | 195 | //$imgur = new imgur('spaceporn', $chat_id); 196 | $imgur = new imgur('malemodels', $chat_id); 197 | $filename = $imgur->getResource(); 198 | 199 | if(class_exists('CURLFile')) 200 | $cfile = new CURLFile($filename); 201 | else 202 | $cfile = "@".$filename; 203 | 204 | $params = array 205 | ( 206 | 'chat_id' => $chat_id, 207 | 'photo' => $cfile, 208 | 'reply_to_message_id' => $reply_to_message_id, 209 | 'reply_markup' => null 210 | ); 211 | 212 | $method = 'sendPhoto'; 213 | if(preg_match("/\.mp4$/", $filename)) 214 | { 215 | unset($params['photo']); 216 | $params['video'] = $cfile; 217 | $method = 'sendVideo'; 218 | } 219 | 220 | $this->request($method, $params); 221 | /* 222 | 223 | $params = array 224 | ( 225 | 'chat_id' => $chat_id, 226 | 'text' => "debug: ".$filename, 227 | 'disable_web_page_preview' => null, 228 | 'reply_to_message_id' => $reply_to_message_id 229 | ); 230 | $this->request('sendMessage', $params); 231 | */ 232 | 233 | file_put_contents($filename, ''); 234 | break; 235 | } 236 | 237 | // txupintzako tranpie 238 | case '/qarrote': 239 | { 240 | $response = "MARIKA"; 241 | 242 | $params = array 243 | ( 244 | 'chat_id' => $chat_id, 245 | 'text' => $response, 246 | 'disable_web_page_preview' => null, 247 | 'reply_to_message_id' => $reply_to_message_id 248 | ); 249 | $this->request('sendMessage', $params); 250 | break; 251 | } 252 | 253 | case '/getInfo': 254 | $params = array 255 | ( 256 | 'chat_id' => $chat_id, 257 | 'text' => print_r($item, true), 258 | 'disable_web_page_preview' => null, 259 | 'reply_to_message_id' => $reply_to_message_id 260 | ); 261 | $this->request('sendMessage', $params); 262 | default: 263 | break; 264 | } 265 | } 266 | } 267 | -------------------------------------------------------------------------------- /tmp/.htaccess: -------------------------------------------------------------------------------- 1 | deny from all 2 | -------------------------------------------------------------------------------- /tmp/imgur/.htaccess: -------------------------------------------------------------------------------- 1 | deny from all 2 | -------------------------------------------------------------------------------- /view/.htaccess: -------------------------------------------------------------------------------- 1 | deny from all 2 | -------------------------------------------------------------------------------- /view/default.tpl: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | php-telebot-heroku 6 | 7 | 8 |

9 | PHP Telegram BOT for Heroku 10 | https://github.com/ZiTAL/php-telebot-heroku 11 |

12 | 13 | 14 | --------------------------------------------------------------------------------