WordPress integration to Slack deployed on Heroku

Deploying WordPress integration to Slack toy application on Heroku

A few weeks ago, I posted a note about a tinkering project with the purpose of building a small web API with Flask to get notifications on Slack from my WordPress blog.

At the time, the application was deployed on a Raspberry Pi and I wanted to try and get the application to run on Heroku.

In the process, I adopted some of Heroku best practices on the management of the environment setup. I cleaned the code so that it can be shared on GitHub and I played around with Slack message formatting capabilities.

What did I learn?

At some point during the migration of the application from the Raspberry Pi, I spent a lot of time being stuck with an “unauthorized access” error. I assumed that the issue was coming from my poor understanding of the port routing logic of Heroku. I was wrong, as the issue was linked to the definition of the Slack API Key.

In the process, I learned that the official documentation is often the best place to find reliable information on a service, as the Heroku documentation show me the correct path to the solution.

I also learned that you need to get as much insights as possible from logs and error messages before starting to mess with your code.

Resources

I used two tutorials from Heroku documentation:
1. Getting Started on Heroku with Python
2. Deploying Python and Django Apps on Heroku

I also used Slack documentation for message formatting.

Shared repo

The code of this project has been shared on Github

WordPress and Slack integration using a Raspberry Pi

Purpose of the project

zapier is a awesome tool that allows you to create a form of collaboration between the services that you are using on the web.

It gives you an easy way to build workflows where actions on a given website will trigger events on another of your preferred websites. One of the recipes (zap) that you can find on zapier catalog creates a notification in a Slack discussion channel when a comment is posted on your WordPress website.

screenshotzapier
Zap description

The idea of this project is to use a bunch of Python tools (the Flask framework and the requests library mostly) to build the same functionality in a small web server running on a Raspberry Pi.

Pourquoi faire simple quand on peut faire compliqué.

Les Shadoks

Description of the workflow

When a new comment is posted on an article, WordPress sends a POST requests containing all the characteristics of the comment to a predefined URI.

Running on the Raspberry Pi, a Flask webserver receives the POST request that is sent by WordPress and automatically translates it in the format that is expected by the Slack incoming webhook API.

The webserver will use the requests Python library to send a POST request to the Slack incoming web hook API containing the message to be published in the discussion channel.

Final state of the tool

As you can see in the screencast below when a comment is added to the blog, a message containing all the characteristics of the comment is added in the target Slack channel. A notification is fired by Slack (Upper right side of the screen) and the message appears on the left part of the screen in the Slack conversation.

wptoslackfinalstate
Demo of the tool

And of course, the message can be viewed in all the different Slack clients.

slackscreenshotonandroid
Same conversation on an Android phone

What did I learn?

This project was my first project with my Raspberry Pi. I learned a lot on how to set up a web server on this small machine and how to forward ports from the router to the Pi.

It was also my first project with Flask. I learned some basic knowledge of how a REST API can be built using this framework.

I also learned that you can be really happy with a functioning application that is made of only 20 lines of code!

Resources

I started with the official Slack and WordPress API documentation websites.

Most of the resources on Python Flask have been found on Miguel Grinberg’s Blog.
In particular, I used his post on how to build a REST API.

For the installation of Flask on the Raspberry Pi, I started with Matt Richardson’s blog and I then used the Raspberry Pi official website

To teach myself how to use the requests python library, I used their official website.

Save