Super Easy Serverless Slack Bots

Slack has a great API for building integrations and one of those types of integrations is called a “bot”. Bots are useful tools that can respond to messages the chat users type into a chatroom. For instance you could type in “what is the weather” and the bot would respond with today’s weather.

Bots are simply software programs that run on a server somewhere and when someone types in a special sequence of characters, in Slack, these usually start with a ‘/’, the message is sent to the bot. The bot then responds with whatever answer it wants to give and that answer is posted back to the chatroom.

Way cool. Buuuut… you have to run the bots on a server somewhere that Slack can communicate with and it always has to be running whether it’s being used or not. True PITA.


IronWorker is an event driven processing (EDP) system and responding to commands/messages is what it does best. So why not respond to chat events? Whenever a keyword or slash command is typed into Slack, an IronWorker will execute to respond to the request. No servers required! No waste either as the worker bot will only run when it’s called and will stop as soon as it’s finished responding. Perfect.

Hello World Example

Here I’ll show you how to make the simplest slack bot in the world. When you type /hello it will post “Hello World!” to the room.

1) Write our bot

Here’s the code for hellobot:

 

The code above should be pretty straight forward, we get a message from Slack (the payload), then we send “Hello World!” back to Slack on the right channel. It’s in Ruby, but it could be in any language.

Now let’s tie everything together and get it working.

2) Get Incoming Webhook URL from Slack

In Slack, go to Integrations, then Incoming Webhooks, then click Add. Choose a channel, then click Add again. Slack will provide you with a webhook URL. Create a filed called config.json with the following content:

Replace the webhook_url string in config.json with the one that Slack provided.

3) Test the Bot / Worker

Since this is Ruby, we need a Gemfile to define our dependencies.

Install the gems to current directory so we can run them in Docker and for uploading to IronWorker.

Here’s a sample of the POST body Slack will send to the bot.

Copy and paste this into a file named slack.payload.

Now run it to test it with the example payload.

You should see Hello World! in #random now!

Ok, we’re all good, let’s upload it to IronWorker.

4) Upload to IronWorker

Now it’s time to upload it to IronWorker so Slack can send messages to it and the IronWorker platform will take care of the rest.

Grab the URL it prints to the console and go to it in your browser, it will look something like this:

https://hud.iron.io/tq/projects/4fd2729368/code/50fd7a3051df9225ba

On that page, you’ll see a Webhook URL, it will look something like this:

https://worker-aws-us-east-1.iron.io/2/projects/4fd2729368/tasks/webhook?code_name=hello&oauth=abc

Copy that URL, we’ll use it in the next step.

5) Create a Slash Command in Slack

In Slack, go to Integrations, find Slash Commands, click Add, type in /hello as the command then click Add again. On the next page, take the IronWorker’s webhook URL you got in the step above and paste it into the URL field then click Save Integration.

6) Try it out! Type /hello into a Slack channel

Alright, now it’s time to try the whole thing out. Go to a slack room and type /hello.

You should see this:

Boom!

This bot isn’t really that useful, but it’s a good one to get you started and a good template to build more useful bots from. I’ve got a few more example bots I’ll post in the weeks to come in the GitHub repo below and we’d love to hear about any IronBots that you make. If they’re good, we’ll share them too.

 

blank

You can find the full source for this example here: https://github.com/iron-io/slackbots.

Leave a Comment





This site uses Akismet to reduce spam. Learn how your comment data is processed.