Build an RSS to Slack bot

Build an RSS to Slack bot

Building a Slackbot is surprisingly easy and I wanted one that would send RSS feeds to a special room. This, I discovered, was a bit hard which is why I wanted to share some of my work here.

The first trick is to get your Slack API key. This is found here and requires you create a Bot user and then grab the key. It is a fairly long string and starts with "xoxp."

The instructions for API key generation are here. It is quite simple.

Once you have your token, simply place it in the code below where it says "YOUR API TOKEN." You can then select a channel to send the notifications to.

channel="#newsroom"
simple_feed("litrelease.txt","https://www.sec.gov/rss/litigation/litreleases.xml",channel,"SEC Litigation")

I've made this system as simple as possible and all of the necessary data is stored in text files. This could be improved considerably but it works for now. I've also tried to manage for different types of RSS feeds but some of them are very messy and so I created a few small error-detection routines to keep things moving.

Again, this is a very rudimentary system that sends new news to the selected channel. Code is below and email us at [email protected] if you have any questions.

from googletrans import Translator
import feedparser

from dateutil import parser
import dateparser
import slack
from datetime import datetime
import pytz
from html.parser import HTMLParser


def strip_html(text):
parts = []
parser = HTMLParser()
parser.handle_data = parts.append
parser.feed(text)
return ''.join(parts)



def quick_slack(channel, text):
token="YOUR API TOKEN"
log(text)
slacker = slack.WebClient(token)
slacker.chat_postMessage(channel=channel, text=text)








def get_simple_feed(date_file,feed, channel,heading):
d = feedparser.parse(feed)
i=0
max=20


try:
this_file=open(date_file, 'r')
stored_update = this_file.readline()
except:
stored_update=""



try
:
last_update = d.updated
except:
try:
last_update = d.entries[0].published
except:
last_update = d.entries[0].updated

if (stored_update==""):
stored_update = last_update


log(stored_update +" "+last_update)
if (stored_update != last_update ):
for post in d.entries:
i = i + 1

try:
pubbed = dateparser.parse(post.published, settings={'TIMEZONE': 'US/Eastern'})
except:
pubbed = dateparser.parse(post.updated, settings={'TIMEZONE': 'US/Eastern'})


update = dateparser.parse(last_update, settings={'TIMEZONE': 'US/Eastern'})


if (pubbed > update):
single_line = ""
single_line = ""+ post.title + ": " + post.link + "\n"
quick_slack(channel, "News from " + heading + ":star::star::star:")
try:
single_line=single_line+"\n"+strip_tags(post.description)
except:
single_line=single_line+"\n "


quick_slack(channel, single_line)

if i == max:
break

this_file = open(date_file, "w")
this_file.write(last_update)
this_file.close()



#grab and deliver the feeds
print("Running...")
print(datetime.now())
channel="#newsroom"
simple_feed("litrelease.txt","https://www.sec.gov/rss/litigation/litreleases.xml",channel,"SEC Litigation")
simple_feed("lastftc.txt","https://www.cftc.gov/RSS/RSSGP/rssgp.xml",channel, "CFTC General")
simple_feed("googlebtc.txt","https://news.google.com/rss/search?q=bitcoin",channel,"Google Bitcoin")

Photo by Roman Kraft on Unsplash