Creating serverless Slack commands in minutes with Go & Up

This post walks through the creation of a serverless Slack command written in Golang, and deployed to AWS Lambda in seconds with Up.

You’ll be creating a `/time <url>` command used to check how long a website takes to respond. Up uses your own AWS account. You can host a large number of custom apps for free while still utilizing the AWS free tier (1 million requests/m).

Check out the installation instructions as well if you’re new to Up.

Registering the Slack command

The first step is to create a Slack app, allowing you to register commands, among other things.

Once created, click “Slash commands” in the menu on the left, and register the /time command. You’ll need to keep this page open for a minute since we need a Request URL so Slack knows where to send requests.

Creating the Slack command

In your project’s directory create a file named up.json. Make sure to replace PROFILE with your AWS profile name (read more).

{
"name": "slack-cmd-test",
"profile": "PROFILE"
}

Now we need a little HTTP server to process the Slack command POST request. Create a main.go file with the following net/http server.

Deploy it with up .

NOTE : The first deploy may take roughly 60s to set up resources.

Now you need to grab the URL and paste it into the Slack command page so Slack knows where to send requests. Copy the command’s URL to the clipboard using:

$ up url -c
Copied to the clipboard!

Paste it in the Request URL field, then you’re good to give it a test run:

With any luck, you’ll see a Hello World response!

Performing the request

Slack sends a POST request with form inputs, otherwise known as application/x-www-form-urlencoded (a tragically named mime type, turned standard-ish).

To access the form values, parse the form with the ParseForm() method. In this case all we need is the “text” field from r.Form, the parsed form.

Now that the request is portion is complete, import the time package and wrap the request with time.Now() and time.Since() to record the request duration.

Deploy again with up and immediately after the deploy you’re ready to test out the real version:

Two files and a few commands later, you’re done! Repeat for as many Slack commands as you need.

Testing locally

One of Up’s strengths is deploying traditional “vanilla” HTTP servers. This means there is nothing new to learn when testing on your machine, develop the app as you always would.

Here’s an example of this application tested via curl :

$ PORT=3000 go run main.go
$ curl -d 'text=https://apex.sh' http://localhost:3000/
https://apex.sh took 19.33542m

Hope that was helpful! Check out the documentation for more help, and follow on Twitter for updates and various software rants.

--

--

--

We’ve moved to https://freecodecamp.org/news and publish tons of tutorials each week. See you there.

Recommended from Medium

How To Fix Meizu U20 Not Charging [Troubleshooting Guide]

Ansible Playbook to configure reverse proxy..

Speech to text - Python

50 best resources when learning to code

Partner tutorial: How to automate construction document flow from Box to PlanGrid

Personaliza la ventana de configuración del documento “Form Settings” | SAP Business One

Download CloudHub Application Log Files Using Anypoint CLI

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
TJ Holowaychuk

TJ Holowaychuk

Code. Photography. Art.

More from Medium

101 AWS Lambda tutorial for Go developers — API Gateway (Part 2)

How to Automate AWS CDK Deployments using Github Actions

Dev Blog: Control your iOS device from your browser with a Raspberry Pi (and without Xcode)

How to Create a Web Service Using Render and Go