Slash commands are special messages that begin with a slash (/) and behave differently from regular chat messages. There are many slash commands available by default, and we can also create our own custom ones to trigger special actions or to retrieve information from external sources without leaving Slack.
"URL shortener" slash command will allow us to generate personalised short urls with a versatile syntax. I have used Rebrandly as a Short URL service. I have also passed a sample request bin URL as Request URL in order to inspect the payload that gets sent by Slack before implementing the custom logic.
Our goal is to implement a little web server app that receives url shortening commands, calls the rebrandly APIs to do so and returns the shortened URLs back to the Slack server. We can break down our app into some well-defined components:
-
The web server deals with all the HTTP nuances, receives and decodes requests from the Slack server and forwards it to the underlying components. Collects the result from them and returns it as an HTTP response.
-
The command parser parses the text (arguments) of the slash commands and extracts URLS, slashtags and domains.
-
The url shortener uses the result of the command parser to generate the short URLs by invoking the Rebrandly APIs.
Step 1: Clone the code
Step 2: Replace slackToken and Rebrandly API key in server.js
Step 3: Type ngrok http 3000
in the terminal (Make sure you have already installed and configured ngrok
on your system)
Step 4: Move to your slack channel and type the command (Don't forget to mention URL(s), domain and slashtag(s))
Thanks to Luciano Mammino, whose amazing tutorial helped me build this integration.