Automating Humour on Slack with “Memebot”

Matt identified a significant improvement to the experience of office humour - and set about writing a bespoke software solution.

Like many other technology companies, we use Slack for informal company-wide instant messaging. From serious discussion about work and events, to sharing light-hearted jokes and funny pictures of coworkers (in designated channels, of course), Slack is one of the most popular methods of communication in the company.

The latter use case was recently boosted by a huge collection of photos which were taken for our brand launch as Ghyston. Fortunately for us, all the photos taken for this have been kept, both the good (which you'll find all around our website) and the bad (which you definitely won't!).

Some of the particularly enterprising amongst us created memes from the images by opening the photos up in their photo editing software of choice, and adding some text, before sharing it on Slack for a humorous and topical interjection into the conversation.

Conor explains what Slack is for

Unfortunately creating a meme like this requires a not insignificant amount of time and effort. Not only do we have our actual work to do, but also spending time painstakingly creating memes in a photo editor could ruin that all important comedic timing. This was severely limiting the amount of fun we could have by adding some text to a photograph.

This was the problem I set out to solve one "Pizza and Programming" evening. Every fortnight we have an optional “Type of food and alliterating word about software development” evening after work, where we're encouraged to work on fun projects to improve our skills, learn new languages or technologies, or just to work on fun ideas. It is all - of course - with food provided, which inspires the ingenious naming system. Most importantly though, it's left up to us to decide what we want to learn or build.

So on this particular "Mexican and Making" evening, I started trying to create a Slack bot to automatically create memes when requested. For speed of development and the strong ecosystem surrounding it I chose Python for this, and fortunately there's good documentation available on how to use Python to build a Slack bot which can respond to messages. Great, pretty quickly into the evening my bot could respond to any messages it was tagged in (after fixing a slight bug where it wouldn't stop replying to itself over and over, endlessly spamming the Slack channel in an infinite loop!).

This was a good start, but the next problem was how to programmatically generate a meme. For this I used Python's popular image processing library PILLOW, which made adding text on top of an image trivial. With a little bit of time spent working out the program logic to size the text to fit into a specified rectangle on the image, and then quite a bit of time working out the logic to ensure the text still fits within the area if the text is rotated, the meme generation was complete.

What do you mean it's not pronounced 'Jy-ston'?

Next was the simple task of combining the Slack bot and the meme generator together. I made it so that all you had to do was tag “@memebot” in a message, followed by the name of the photo you wanted to add text to, followed by a message to write on top of the photo. After a couple of seconds memebot would reply with the resulting meme. It worked! By now it was starting to get late into the evening, but all that was left to do was to deploy memebot to a server so it could still run when my laptop was off.

Deploying the memebot application was made simple by our internal Docker swarm infrastructure. We have a custom tool which automatically generates the necessary Dockerfile and deploys apps to run in a container on our internal servers with a single command. We use this for all kinds of internal apps, but it is particularly useful to be able to instantly get something running with no hassle on a "Curry and Coding" evening where the priority is getting something done quickly.

After it was deployed, the only thing to do was to wait until the next morning, add it to our main channels, and watch everyone have some fun with it (and inevitably try to break it, which fortunately no one did!). I'd made sure to write it so that all you needed to add to the code base was an image, and rectangle coordinates for the text to be placed in (and some optional parameters like rotation, font colour etc.), and you could add a whole new meme template to memebot. Soon pull requests were coming in as people added their own favourite photos to be automatically memed.

Since then, memebot has had GIF support added, and the ability to respond to certain phrases, and continues to be used throughout the company. If you want to check it out and maybe even try it on your Slack channels we've made it available on Github here.

It only took a couple of months before I found myself added to memebot’s library of photos after a colleague spotted a photo opportunity with a perfectly positioned ray of sunlight.

Matt (author) features in his own memebot (That smile when everyone loves Memebot)

Matt March

By Matt March

Share this article: