golem-telegram-bot/feed-2-channel-email.php

44 lines
1.5 KiB
PHP

<?php
require_once('config.php');
/* ===== BEGIN of MAIN PROGRAM ===== */
if (! $latestGetTime = file_get_contents('latestFeedTimestamp.conf')) $latestGetTime = 0;
$content = file_get_contents('http://golem.linux.it/wp/feed/');
$newLatestGetTime = time();
$content = str_replace('<![CDATA[', '', $content);
$content = str_replace(']]>', '', $content);
$feed = new SimpleXMLElement($content); /* KABOOM */
foreach($feed->channel->item as $item)
{
$timestamp = strtotime($item->pubDate);
if ($timestamp > $latestGetTime)
{
echo 'Now publishing: ' . $item->title . "\n";
/* Send to Telegram Channel */
if (DEBUG) echo 'Publishing on Telegram channel...';
$message = $item->title . "\n\n" . $item->description . "\n" . $item->link;
$r = file_get_contents(API_URL . API_TOKEN . '/sendMessage?chat_id=' . urlencode(API_CHANNEL_ID) . '&text=' . urlencode($message));
echo ($r ? 'done' : 'error') . "\n";
/* Send to Email/Mailing List */
if (DEBUG) echo 'Sending to email address...';
$title = mb_convert_encoding($item->title, 'ASCII');
$text = mb_convert_encoding($item->description, 'UTF-8') . "\n" . $item->link;
$r = mail(MAIL_TO_ADDR, $title, $text, 'From: ' . MAIL_FROM_ADDR . "\n" . 'Content-Type: text/plain; charset=UTF-8');
echo ($r ? 'done' : 'error') . "\n";
echo "\n";
}
}
file_put_contents('latestFeedTimestamp.conf', $newLatestGetTime );
/* ===== END of MAIN PROGRAM ===== */
?>