Golearn!

Or learning a new language – Golang is my latest endeavor to keep my programming skills sharp. At the last Reno Developer meetup Spot presented on Golang and that inspired me to use Go to implement a Slack custom integration for the devreno Slack team. The Reno Developer meetup recently received sponsorship from JetBrains and one of the perks is we are provided with one license key per month to giveaway to members. Armed with newly acquired knowledge I set out to build a Slack integration in Go that allow any team member to run a slash command and see if they have won a prize.

Slash Commands

The name of the command is “raffl” and typing /raffl in any Slack channel:
Screenshot from 2016-04-30 09:45:08

will display the command, press enter one to focus the command, press enter again to run the command. One of three things will happen:

  1. You win a prize
    Screenshot from 2016-04-30 09:58:49
  2. You don’t win a prize
    Screenshot from 2016-04-30 09:54:03
  3. You’ve already won a prize and thus cannot win another
    Screenshot from 2016-04-30 10:01:26

What are the odds? Currently there are 2 prizes, in the form of JetBrains license keys, and there are 100 members of the devreno Slack team. So your chances are 1 in 100 every time you run /raffl in any Slack channel.

Technical Details

Programming in Go

Overall Go is easy to pick up assuming:

  • You’ve programmed in a C like language
  • Are familiar with a managed language
  • Enjoy using the command line
  • Willing to learn several helper utilities to augment your Go experience

I opted to also use the Go plugin for IntelliJ (both are free!) which provides an integration in a familiar IDE.  I found the move and rename refactorting support invaluable as I started to spread my wings with this project.

I also selected the Go BoltDB to persist the app data over the lifetime of use in a deployment.

Slack & Go

The Slack integration consists of a Slash Command and an Outgoing Webhook. This mainly because Slack expects a Slash command to run in less than 3000ms and will report a warning message back to the user if it takes longer. I forked this Slackbot framework for use as a starting point. Plenty of details in the docs for the framework and very well written. The basic flow is:

  • User types “/raffl” in any Slack channel
  • A call is made from Slack to a service running on a remote server (in this case hosted on Heroku)
  • The Slash command is processed immediately and if necessary a deferred command is invoke in Go to run additional logic
  • Deferred commands are invoked via a Goroutine and the response is continued in via Direct Message from @slackbot.

I choose Direct Message for the response target to keep the details of a prize win/loss secret to the requester.

The two main Go files for running this logic are:

  • Controller: Raffle.go main Slack integration
  • Model: Prize.go. contains logic to interact with BoltDB to manage the Prize data store

Be gentle if you review the code, I’m sure there is room for improvement in my use of Go.

Running Go in Slack

I selected Heroku to run the Go service that use invoked from Slack when “/raffle” is run by a user. I found Getting Started With Go on Heroku and the Go tutorial from Heroku (somewhat) useful.

Resources

 

 

Advertisements