message)) { if (isset($row->message->from->username)) { if (DEBUG) botlog('[II] ' . $row->message->from->username . ' has written «' . $row->message->text . '»' . "\n"); if (! isCommand($row->message)) { if (in_array($row->message->from->username, BOT_ALLOWED_USERS)) { if (DEBUG) botlog( ' Telegram: forwarding to channel... '); $r = forwardChannel($row->message); if (DEBUG) botlog(($r ? 'done' : 'error') . "\n"); if (DEBUG) botlog(' Mail: mailing to address... '); $r = sendEmail($row->message); if (DEBUG) botlog(($r ? 'done' : 'not done') . "\n"); } else if (DEBUG) botlog('[EE] User ' . $row->message->from->username . ' not authorized!' . "\n"); } else if (DEBUG) botlog('[WW] Command not allowed' . "\n"); } else if (DEBUG) botlog('[EE] from->username not set!' . "\n"); } echo '{}' . "\n"; botlog('[II] End at ' . date('Y-m-d H:i:s') . ' ====================' . "\n"); /* ===== END of MAIN PROGRAM ===== */ /* ===== FUNCTIONS ===== */ function forwardChannel($message) { $query = API_URL . API_TOKEN . '/forwardMessage?' . 'chat_id=' . urlencode(API_CHANNEL_ID) . '&from_chat_id=' . urlencode($message->chat->id) . '&message_id=' . urlencode($message->message_id); if (DEBUG) botlog($query); return file_get_contents($query); } function sendEmail($message) { $link = isLink($message); if ($link) { $title = getHtmlTitle($link); if ($title) { $subject = $title; $text = mb_convert_encoding($message->text, 'UTF-8'); $headers = 'From: ' . MAIL_FROM_ADDR . "\n" . 'Content-Type: text/plain; charset=UTF-8'; if (mail(MAIL_TO_ADDR, $subject, $text, $headers)) return true; } } return false; } /* * Retrieves link content, then, if it is HTML, returns the title (if any), else returns false * http://stackoverflow.com/questions/399332/fastest-way-to-retrieve-a-title-in-php */ function getHtmlTitle($link) { $html = file_get_contents($link); if ($html) { if (! preg_match('/(.*)<\/title>/siU', $html, $titleMatches)) { if (DEBUG) botlog('[II] Not HTML compliant link'); return false; } $title = trim(preg_replace('/\s+/', ' ', $titleMatches[1])); return $title; } return false; } /* * Checks wheter message has a link (and returns it) or not (and returns false) * Only the first link is checked, bicos unn'aveo voglia di fanne altri - in inglisc un rend */ function isLink($message) { if (isset($message->entities)) { foreach($message->entities as $entity) { if ($entity->type == 'url') { return substr(utf8_decode($message->text), $entity->offset, $entity->length); } } } return false; } /* * Checks wheter message contains commands * Sì, il codice fa schifo e non è ottimizzato, * ma del resto è stato fatto in fretta e furia e, come detto, * è più per farsi un'idea e capire un po' come funziona 'sta cosa * Verrà abbellito durante qualche ora del GOLEM */ function isCommand($message) { if (isset($message->entities)) { foreach($message->entities as $entity) { if ($entity->type == 'bot_command') return true; } } return false; } ?>