How to Build a Serverless Slack Command in minutes

May 12, 2020 | Commander


Commander is a Slack app that provides a framework for developers to create custom serverless slash commands and serverless Slack apps without leaving the Slack environment. Commander is an app that simplifies your workflow to build custom apps. The commands which are created using Commander are serverless commands which run on Nimbella’s Serverless Cloud. There's nothing to install, no servers to manage, and no services you need to provision. Simplifying the process of creating Apps and slash commands allows you to quickly create or install functionality for your team to more efficiently communicate with each other.

Why use Serverless Slack Apps?

Serverless computing with functions fundamentally reduces the amount of code you develop and deploy for cloud applications, eschewing the “server” parts, and allowing you to focus on just your functions. For a cloud-native application, where functions are APIs, an integrated API gateway handles the routing of events and REST requests to your functions.

The advantage of Commander is minimal configuration setup, easier development & deployment, and sharing across your team. Not only that but with Commander you also get access control, built-in security, and logging and all within Slack itself. These are unique and important features for small and large Slack teams alike, and we will describe them in our next article in the series.

What are slash commands?

According to Slack, “Slash Commands allow users to invoke your app by typing a string into the message composer box. A submitted Slash Command will cause a payload of data to be sent from Slack to the associated app. The app can then respond in whatever way it wants using the context provided by that payload. These commands are the entry points for complex workflows, integrations with external services, or even just simple message responses. They're the knock at your app's front door that could be the start of a great conversation”.

How to Build a Currency Converter slash command?

Without further ado, let’s get started. The slash command we will be writing today is a simple currency converter.

/nc convert <from> <to> 

The above command will convert 1 unit of from the from parameter to the to parameter. For example, running /nc convert usd eur  will convert 1 USD to Euro.

Before we could build our own custom command in Commander, we need to install it. In 2 simple steps, we will install Commander and create a basic slash command that users can run in Slack.

  1. First, install the Commander Slack app from the Slack App Directory or Nimbella’s Website.

Slack app added to Slack

After Installation, you should see the following message.

Slack app welcome message with Commander

  1. We have extensive documentation for Nimbella Commander and the slash commands it provides. Commander comes with the “/nc” slash command (short for Nimbella Commander). This command is used to create, manage, and run your custom commands. This is also the primary command to interact with the Commander.

Try it and type /nc command_create demo  to create a demo command as shown below. The command is created with a stub function which you can immediately run with the command /nc demo . The stub function outputs a placeholder response which you can edit by clicking the shown “edit the code” link.

Slack app commander creating demo

The default language for slash commands is Javascript. You can change the language of your command to Python or Go as well. For this tutorial, we will use JavaScript.

Slack app code for Commander

  1. Now let’s create a currency converter command. The command should take 2 mandatory parameters, “from” and “to”. from is the currency we want to convert and to is the currency we want to convert it to. We will again use command_create for this.

Type /nc command_create convert <from> <to>  into Slack. Once you run the command, click the “edit the code” button to pull up the code for your newly created command.

commander Slack app convert command

  1. Now once you create a command, let's write the logic for the currency converter. We will use the exchange-rates-api npm package to convert the currency. You can find the source of the command here. Copy the code from the link and paste it over all the code found in the page that pulls up when you click “edit the code”.

commander code

  1. Once this is done, press “Save”, go back to your Slack prompt and run the command /nc convert usd eur . As shown below, the command converts 1 USD to Euro.

slack app commander convert use to eru

Why use Nimbella Slack Commander?

This article demonstrated how easy it is to create a serverless Slack command using Nimbella Commander. I showed you how to install Commander, create and run your first command, and how to create a more interesting example.

I’ll conclude by summarizing the benefits of using Nimbella Commander for building Slack apps.

  1. You don’t have to download any external tools other than Slack.
  2. There is no infrastructure for you to manage or operate.
  3. As a developer, you get to focus on writing just the business logic of your app, with Nimbella Commander doing the rest of the heavy lifting for you.
  4. There is no complicated Slack setup, and you can use and share the slash commands with everyone in your Slack team.

For our next blog, we will cover more advanced Nimbella Commander features, which include the following:

  1. Command-Sets: a way for creating and sharing Slack commands with others.
  2. Learn about Command-Sets that we have open-sourced and how to install them in your Slack with a single command.
  3. How to use Commander’s built-in access-control, logging, and monitoring so that you can control who can create, edit, or run your commands, and audit who’s using them.

Written by Nikhil Prakash (Twitter)

Recent Posts