Skip to content

Instantly share code, notes, and snippets.

@piroyon
Last active April 17, 2020 04:21
Show Gist options
  • Save piroyon/f4e43b6e246ccd1e4ec89912b50b9ed0 to your computer and use it in GitHub Desktop.
Save piroyon/f4e43b6e246ccd1e4ec89912b50b9ed0 to your computer and use it in GitHub Desktop.
Check RSS feed and post to slack, if there is a new article
<?php
// Usage: /usr/bin/php rss_slackpost.php
// Require: php-xmlrpc, php-cli
// Make slack app. and get slackapikey, then invite bot
// $ cat feedlog.txt --> The URL of the newest article in the last check
// https://example.org/wp/archives/178
ini_set("auto_detect_line_endings", true);
$slackchannel = 'channel_name';
$mess = '';
$fg = 0;
$feedurl = 'https://example.org/wp/feed';
$slackApiKey = 'xoxb-1234567890000-1234567890000-xxxxxxxxxxxxxxxxxxxxxxxx';
$log = './feedlog.txt';
$feed = file_get_contents($feedurl);
$invalid_characters = '/[^\x9\xa\x20-\xD7FF\xE000-\xFFFD]/';
$feed = preg_replace($invalid_characters, '', $feed);
$rss = simplexml_load_string($feed);
//var_dump($rss);
$fp = fopen($log, 'r+');
$oldurl = rtrim(fgets($fp, 4096));
fclose($fp);
foreach($rss->channel->item as $item){
$link = $item->link;
if (rtrim($link) == $oldurl) {
break;
}
else {
$mess = 'Newly posted'.PHP_EOL;
$mess .= '----------------------------------------'.PHP_EOL;
$title = $item->title;
$date = $item->pubDate;
//$description = strip_tags($item->description);
$mess .= $title.PHP_EOL;
$mess .= $date.PHP_EOL;
$mess .= $link.PHP_EOL;
if ($fg == 0) {
file_put_contents($log, $link);
$fg = 1;
}
$mess = urlencode($mess);
$url = "https://slack.com/api/chat.postMessage?token=${slackApiKey}&channel=%23${slackchannel}&text=${mess}&as_user=true";
$res = file_get_contents($url);
echo $res;
//break;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment