23/02/2016 Ben Bridts

Generating memes in Slack with AWS Lambda

At Cloudar we use Slack for a lot of our internal communication. One of the things we love is how easy it is to extend the functionality of this messaging tool. One integration that we sometimes missed was a meme generator, because there a moments when a picture meme is worth a thousand words.

If you search for a meme generator for slack you’ll find this great ‘slash command’ by Nicole White. The only thing we liked to change was the environment that it runs in. Running the Flask application that Nicole wrote in an Elastic Beanstalk environment is fairly trivial, but porting the code to run in a Lambda function is a lot cooler. On top of this you’ll get all the benefits of Lambda, making the solution cheap and easy to scale.

If you want to jump right in, you can grab the code from GitHub and run it in your own AWS account. In the remaining part of this blogpost I’ll describe the changes we made to make it run in AWS Lambda.

What we did

  • We kept the complete models.py, which is the real logic of the function. It is responsable for using memegen and memeifier to generate the memes. Having support for mulitple languages in AWS Lambda saves us a lot of work here.
  • We wrote a lambda_function.py as a replacement for views.py. It contains the lambda_handler function that parses the data send by Slack, uses the models to get an image url and returns a json response. To create this file we merged an AWS Lambda Blueprint with the code that Nicole wrote, another place where the current ecosystem allows for fast integration.
  • Nicole used a webhook to post the memes, we decided to just return json directly. This looks a little bit different in Slack, but is more in line with how other slash commands work.
  • We also changed the way previews work, by returning the image directly in Slack instead of a url.

How this looks

Using a build in meme: /meme success; we have; a meme bot;

Screenshot 2016-02-23 11.55.51

Using a meme with a custom image: /meme https://pbs.twimg.com/profile_images/657199367556866048/EBEIl2ol.jpg; Hello; It's me

Screenshot 2016-02-23 13.33.24

Previewing a meme: /meme preview awesome-awkward; makes a meme; doesn't show anyone;

Screenshot 2016-02-23 13.39.05

Showing this to your coworkers:

Screenshot 2016-02-23 13.41.40

How to set this up

You can find complete instructions in the README. But the big steps are:

1. Set up a new slash command in Slack
2. Encrypt the Slack token with KMS
3. Update lambda_function.py to use this token.
4. Package the code and upload it to AWS Lambda
5. Configure API gateway. to transform the form encoded data to json

Make it your own

All the source code is available on Github and we love to see how you integrate this in your own Slack team. Leave a comment here or send us a pull request on github if you find even more ways to build on the code that Nicole put online.

Leave a Reply

Your email address will not be published. Required fields are marked *