Compute Services

Build a serverless Slack app and bot with IBM Bluemix OpenWhisk and API Connect

Share this post:

This post gives you a starting point for building a serverless Slack app with IBM Bluemix OpenWhisk actions exposed via API Connect.

Slack has become the digital hub for collaboration in many companies and teams. Initially, I considered Slack only as a kind of super-duper IRC. But I quickly discovered that Slack apps can add features to channels with commands, using notifications from external systems (e.g., a new commit in GitHub, a build failure). And let’s not forget the chat bots at the heart of a new type of conversational apps these days:

Slack in the API economy

The team at Slack has worked hard to make it easy to build apps. Whether you want to build a webhook, a bot, or a custom command, there are simple APIs to help you make it happen — and you can try them all for free.

Let’s say you’re building a bot. It could be a low-traffic bot — perhaps something useful, but you won’t be using it extensively; or it could be something very critical and visible, a bot used by all your team members, or even by other external Slack teams (if you plan to publish your integration to the Slack app directory). In either case, it will make your work easier if you can focus on your actual bot code rather than on the plumbing around it. And if your bot becomes popular, you’ll want to scale the bot resources so that it can handle more messages and more load.

Typically, building a Slack app has required you to create a new bot user in Slack, have some back-end code join the Real Time Messaging API, start listening for events, and then do something with them. But things have changed a bit in the recent weeks. Slack recently released its Events API, whose motto is “Don’t call us, we’ll call you”. What that means is you no longer need to have a back end always running to listen to Slack messages with a websocket, or to poll for Slack updates. Instead, you can provide Slack with endpoints, and they will call the endpoints whenever something interesting happens.

I don’t like this — I love it! Why? Because this, coupled with the previous requirements around scalability and focusing on the bot code rather than operations, makes a serverless platform like OpenWhisk a great fit for kickstarting the implementation of a Slack application.

How to build a serverless Slack app

So let’s give this a try! I’ll show you how to build a serverless Slack app, or at least the foundation for a great app. (I’ll save showing you how to build the killer Slack app for another post.)

This is how the app will look like from a high-level perspective:

The user should be able to:

  • Install the app in his Slack team (the app is named myserverlessapp in the sample)
  • Chat with a bot (the bot user is named @myserverlessapp)
  • Run a custom command (the custom command is… wait for it… /myserverlessapp)

During the installation of the app, Slack calls an endpoint that you specify and expects you to reply to its request with your app credentials. In return, Slack provides you with the necessary authorization tokens to interact with the Slack team where your app was just installed.

To implement the chat bot, you use the Slack Events API listening to the direct messages to your bot user. Slack requires you to provide an endpoint that it will call with every new event.

For the custom command, you need to provide another endpoint, which will receive the custom command parameters. It’s up to you to return the command output immediately or asynchronously.

In our app, the three endpoints described above map to three OpenWhisk actions. In order to expose these actions through a REST API, the app relies on API Connect. API Connect allows you to create an API using the OpenAPI specification, and then to assemble this API:


Click to view full-sized image

I won’t go through all of the installation and configuration steps in this post. Instead, I encourage you to proceed to the GitHub repository where you’ll find all of the source code for this Slack app, along with a instructions for configuring Slack, OpenWhisk, and API Connect.

Detailed instructions on how to implement this solution with Bluemix are available in the IBM-Bluemix/openwhisk-slackapp project on GitHub.

This project provides the basics for building a Slack app. Obviously, there is more work required to build a real app that people will want to use, but the event-driven nature of Slack shows how a serverless platform is a great way to support the implementation and scalability of apps.

From the perspective of the Slack app developer, there is no server involved; only OpenWhisk actions and an API in API Connect. In addition, the code will not run if no user interacts with the app, and if the app gets popular it will benefit from OpenWhisk scalability.

If you have feedback, suggestions, or questions about this post, please reach out to me on Twitter: @L2FProd.

References

Offering Manager - IBM Cloud

More stories
May 3, 2019

Kubernetes Tutorials: 5 Ways to Get You Building Fast

Ready to start working with Kubernetes? Want to build your Kubernetes skills? The five tutorials in this post will teach you everything you need to know about how to manage your containerized apps with Kubernetes.

Continue reading

May 3, 2019

Using Portworx to Deploy and Manage an HA MySQL Cluster on IBM Cloud Kubernetes Service

This tutorial is a walkthrough of the steps involved in deploying and managing a highly available MySQL cluster on IBM Cloud Kubernetes Service.

Continue reading

May 2, 2019

Kubernetes v1.14.1 Now Available in IBM Cloud Kubernetes Service

We are excited to announce the availability of Kubernetes v1.14.1 for your clusters that are running in IBM Cloud Kubernetes Service. IBM Cloud Kubernetes Service continues to be the first public managed Kubernetes service to support the latest upstream versions from the community.

Continue reading