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 team@visiblemagic.com 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

SHARE THIS POST

author

James Donovan

Share a little biographical information to fill out your profile. This may be shown publicly.

Other News

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

09 . 04 . 2020

Programming Mobile Apps

k4EBH-1

Why is ‘ls’ ‘ls?’

You probably type it every day: ls. It’s a Unix command that shows something like this: {22:51}~/Downloads/BeOS 5.0 Professional ➭ lsBeOS_5.0_Professional.ccd BeOS_5.0_Professional.img winworldpc.com.txtBeOS_5.0_Professional.cue BeOS_5.0_Professional.sub A simple list of files, quickly presented. But this simple command has its roots in 40-year-old technology. The first “ls”-like command appeared in 1961 on the Massachusetts Institute of Technology’s Compatible

09 . 04 . 2020

Programming Mobile Apps

SHOW ALL NEWS

Some posts

Maecenas nunc erat, faucibus id velit sed, pellentesque efficitur dui.

12 . 03 . 2020

Suspendisse vehicula nunc id metus mattis pretium.

12 . 03 . 2020

Maecenas nunc erat, faucibus id velit sed, pellentesque efficitur dui.

12 . 03 . 2020

Suspendisse vehicula nunc id metus mattis pretium.

12 . 03 . 2020

Maecenas nunc erat, faucibus id velit sed, pellentesque efficitur dui.

12 . 03 . 2020

news img

Maecenas nunc erat, faucibus id velit sed.

news img

Praesent arcu nibh, dignissim et justo at.

news img

Maecenas nunc erat, faucibus id velit sed.

news img

Praesent arcu nibh, dignissim et justo at.

news img

Maecenas nunc erat, faucibus id velit sed.

Ut porttitor nisi metus, sed ultricies velit vehicula sed.

Mauris urna lacus, elementum et faucibus ut, porttitor in orci. Ut porttitor nisi metus, sed ultricies velit vehicula sed.

Want to start a conversation?

Subscribe To Our Newsletter

Talking chamber as shewing an it minutes. Trees fully of blind do. Exquisite favourite at do extensive listening. Improve up musical welcome he.

VISIBLEMAGIC