How to Write a Simple Slack Bot to Monitor Your Brand on Twitter

Bob, Tue 25 April 2017, Tools

Automation, brand, configparser, logging, monitoring, Slack, slacker, triggers, Twitter, twython, TwythonStreamer

In this article I show you how to monitor Twitter and post alerts to a Slack channel. We built a nice tool to monitor whenever our domain gets mentioned on Twitter. The slacker and twython modules made this pretty easy. We also use configparser and logging.

This was another script that came out of our 100DaysOfCode challenge.

The funny thing is that we started out using mediatrigger.io using a free trial, but soon we thought:

How difficult would it be to build this ourselves?

Not that much, in this article we show you how.

By the way, if at some point you lack inspiration what to build next, apart from joining our code challenges, think about how you can scratch your own itch. When we do this we tend to stumble upon interesting projects. We think it's the best way to learn.

With that said let's get coding!

Requirements and Setup

We use slacker and twython so pip install them or pip install -r requirements.txt (after creating a virtual env).

You will need Twitter API tokens and a Slack token. We wrote about the Twitter API and Slack before.

We use configparser to read these tokens in from a config file. Note we only store the template config file on GH, the real one is ignored to not reveal any secret info.

For Slack you need to create a bot first and add the bot to your designated channel.

The code

You can get the full project here (as said it's part of our 100DaysOfCode repo). Here I go over the script bit by bit:

Deployment

I run this script on my server. Of course it might die and we want it to work 7x24, so I included a small shell script to respawn the process if it dies, a technique I learned from my previous Slack bot.

Result

Whenever our domain is mentioned we get an instant notification on Slack:

pybites mentions channel

What would you Slackify?

For us this is a great form of monitoring (automation). I hope you've enjoyed this tour of how to interact with 2 APIs. It's not that difficult, yet the options are endless and you can build some really cool stuff.

Go wild with APIs

On that note, 'Now is better than never' (Python Zen), because this week's challenge is all about fiddling with Web APIs! Comment below if this inspired you to build something yourself, or submit your code to our challenges community branch via a PR.


Keep Calm and Code in Python!

-- Bob

PyBites Python Tips

Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.

Get our Python Tips Book

"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H

"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B

"This is now my favourite first Python go-to reference." - Anthony L

"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S

Get the book