Build an RSS to Slack bot09 . 04 . 2020

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 slackfrom 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 + 1try:
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

Other News

41WUtS5-R2L._SX328_BO1204203200_

John new book, Get Funded!, is available now (and here is an excerpt)

John’s new book, Get Funded!, is out and we’d love for you to check it out. Get Funded! is a compendium of advice and stories about how John and his friend Eric Villines got funded. It’s a book for beginning entrepreneurs and the goal was to give you the tools you need to raise funds

60108169560__185F9C77-D4AB-4E1F-A581-D0C1B2C07EAD-1

The best book on coding… ever

If you’re anything like the guys at VisibleMagic, you do two things obsessively: visit StackOverflow and buy coding books. I personally have about a dozen under my desk right now where they are getting various levels of use. Older coders, however, remember when these thick tomes were integral to their jobs. “Learn Web Programming In 24

SHOW ALL NEWS

Want to start a conversation?