Slack notifications on Raspberry Pi

For those of you who haven’t heard of or used Slack, it is a great communication tool that is used in many modern workplaces. It is a chat tool that reduces the dependence on emails, and allows for quicker more efficient communication in the office.

The beauty of Slack is that it is free to use (there is a paid-for subscription for enterprise level usage) but can be integrated with a wide variety of third-party applications, as well as being able to utilise incoming and outgoing webhooks.

Slack controlled On-Air sign

This is an example of how I use Slack every day at work in my job. As I often speak to important enterprise customers, often from other countries, I found that it often became noisy whenever the guys upstairs needed to grab another cup of coffee. Being a fan of all things IoT, I decided to build an On-Air sign that could be controlled via Slack, so that I could quickly turn the sign on whenever I was on an important call. I’ll be writing up a separate post on this particular project, but being able to control it via slash commands in Slack was really useful and we now use it every time we have a conference call with a customer.

This (pretty badly shot) video, should give you an idea of what is to come when I get the project on here:

Raspberry Pi Twitter Follower Bot

I tend to check how many Twitter Followers I have every now and then, as I’m always looking to spread the word about Raspberry Pi and Raspberry Coulis where possible. I then thought it would be pretty nice if I could automate this process using a Raspberry Pi and some Python code.

To save making this post unnecessarily long, I have included detailed instructions in the README.md file on my GitHub repository, which will guide you through the necessary parts, but if you are after the code then here it is:

#!/usr/bin/python import tweepy import urllib2 import time # For Twitter: Add the relevant keys, tokens and secrets from your Twitter app made here: https://apps.twitter.com/ consumer_key = '' consumer_secret = '' access_token = '' access_token_secret = '' # Variables - configure the bits below to get your script working. wait = 12600 # Time (in seconds) between checks. Default is 12600 seconds (210 minutes / 3.5 hours). style = "#1da1f2" # Colour for the message - default is Twitter Bird blue userid = '' # The Twitter user you want to track the followers of (without the @) handle = '' # Tweak this to display the userid in a nicer format - i.e. "Raspberry Coulis" instead of "raspberrycoulis" # Slack incoming webhook URL webhook = '' # Tweepy API - do not change auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) follows = api.get_user(userid) # The function that does the magic - checks your Twitter user for the number of followers then sends this data to Slack to notify you. def postToSlack(): # while True: fans = str(follows.followers_count) data = '{"attachments":[{"fallback":"'+handle+' has '+fans+' followers.","pretext":"'+handle+' has '+fans+' followers.","color":"'+style+'","fields":[{"title":"Twitter Fans","value":"'+handle+' has '+fans+' followers.","short":false}]}]}' slack = urllib2.Request(webhook, data, {'Content-Type': 'application/json'}) f = urllib2.urlopen(slack) f.close() # time.sleep(wait) postToSlack() exit()

If all goes well, you should receive a Slack notification telling you how many followers your account has!

As I’m still learning Python and all things Raspberry Pi related, I’d welcome any feedback on this code via my Gist.

Raspberry Pi Facebook Fan Bot

In similar fashion to the Twitter Follower Bot above, the Facebook Fan Bot works in a very similar way. The great thing about this script is that you only need to add a few access keys, secrets and ID’s to get it working.

Again, to save repeating myself in here, take a look at the README.md on my GitHub repository that tells you how to get and add the necessary keys and IDs to your script. This is what it looks like:

#!/usr/bin/python import urllib2 from urllib import quote import json # Variables - configure the bits below to get your script working. style = '#3b5998' # Colour for the message - default is Facebook blue fb_page = '' # Facebook Page name # Slack incoming webhook URL webhook = '' # The functions that creates the magic - checks your Facebook Company Page for the number of fans then sends this data to Slack to notify you. def fanCount(): fb_id, app_id, secret = '', '', '' # Add your Page ID, App ID and Secret between each quotation mark fb_url = 'https://graph.facebook.com/{}?access_token={}|{}&fields=fan_count'.format(quote(fb_id), quote(app_id, quote(secret)) fanCount = urllib2.urlopen(fb_url).read() jsonResponse = json.loads(fanCount) return jsonResponse['fan_count'] def postToSlack(): fb_fans = str(fanCount()) data = '{"attachments":[{"fallback":"'+fb_page+' has '+fb_fans+' followers.","pretext":"'+fb_page+' has '+fb_fans+' followers.","color":"'+style+'","fields":[{"title":"Facebook Fans","value":"'+fb_page+' has '+fb_fans+' followers.","short":false}]}]}' slack = urllib2.Request(webhook, data, {'Content-Type': 'application/json'}) f = urllib2.urlopen(slack) f.close() postToSlack() exit()

And if successful, you should get another Slack notification that looks like this (assuming you have added the custom icons included in my repository):

If you would like to provide your feedback, please feel free to do so via my Gist.

Automating Slack Notifications on Raspberry Pi

Rather than have these scripts run constantly, bombarding your with Slack notifications, it may be better to schedule them via cron to run at specific times of the day. Unless you are uber-popular, like the official Raspberry Pi Twitter account, then checking on your followers once or twice a day ought to be enough.

Cron can appear confusing at first, but if you use a crontab generator then this whole process is a lot easier to manage. In my setup on my Raspberry Pi, I have my Twitter Follower Bot and Facebook Fan Bot checking in the morning and in the evening, which is perfectly adequate for me.

Like this: Like Loading...