A Simple Slack Bot in Go - The Hosting
Tags: go, systemd, slackbots, slack
Published: 2018-11-28 03:13:15

---
title: A Simple Slack Bot in Go - The Hosting
published: true
description: "Let's write a simple Slack bot in Go part 2 - the part that gets us 'free' hosting!"
tags: go, systemd, slackbots, slack
cover_image: https://thepracticaldev.s3.amazonaws.com/i/hzmxezsk0l361idae9qr.png
series: "Golang Slack Bot"
---

# Praise the Sun

## The Real *Dark Souls* Slack Bot Continues Here

[Last time](https://dev.to/shindakun/a-simple-slack-bot-in-go---the-bot-4olg) around we put together a very simple Slack bot that would respond with "Praise the Sun" anytime it saw the test "*Dark Souls*". This time around we're going to go over how we're going to host it. Note that there may be steps I kind of gloss over - this is because certain steps differ between operating systems. If you are running into any issues a quick Google should get you back on track.

We have a bunch of different choices for hosting, some of those include [Digital Ocean](https://m.do.co/c/fa6899c60e6c) (affiliate link), [AWS](https://aws.amazon.com/), [Azure](https://azure.microsoft.com/), [Google Compute Engine](https://cloud.google.com/compute/) or whatever else you may choose. For this article, we'll be using GCE - not quite as straightforward as some other choices but using a small instance should result in free hosting for our bot. There is really no wrong choice for hosting, use whatever works for you - I'm using GCE just because I happen to be using it a lot lately.

### Google Cloud Hosting

First things first - signup for Google cloud if you don't already have an account. New accounts will get a $300 credit that lasts up to a year but you have to have a credit card to activate the account so keep that in mind. We'll do a bit of setup in the UI to get started.

Let's create a new project at https://console.cloud.google.com/projectcreate. I've named mine `golang-slackbot`.

![Google Cloud Console](https://thepracticaldev.s3.amazonaws.com/i/1v1o153ubnu01blc7gz9.png)

Now let's create a new VM instance. Once you have created and selected your new project you can visit https://console.cloud.google.com/compute/instances to create a new virtual machine. It may take a few minutes for GCE to be set up on your account - if you have to wait go pour yourself a pint or have some tea.

![GCE](https://thepracticaldev.s3.amazonaws.com/i/nao5enqnn7hn7nyzvyet.png)

As you can see in my screenshot above I have chosen to use a `micro` instance running `Debian GNU/Linux 9 (stretch)` hosted in Region `us-west1` and Zone `us-west1-a`. You will probably want to choose a region and zone closer to you, make a note of the zone and project name they will be needed later on. I recommend sticking to the micro instance as you can run the equivalent of one instance for free each month after your $300 credit runs out. I've only selected Debian as it's the default choice. Leave everything else the same and click "Create" at the bottom.

### Google Cloud SDK

Once again it's going to take a moment or two to create the VM. Now would be a good time to install the [Google Cloud SDK](https://cloud.google.com/sdk/install). The install docs are pretty good so I'll leave the installation as an exercise for the reader.

### Building

We're getting close! By now our VM should be up and running. Now you'll need to open a command prompt wherever our Slack bot source code lives on the local computer. Once there we'll build a Linux executable. I'll usually use `-ldflags="-s -w"` just to get a slightly smaller executable. I'm not going to be debugging this one anyway so [stripping](https://en.wikipedia.org/wiki/Strip_(Unix)) it isn't really a big deal, leave `ldflags` out if you wish. Here you can see we want the resulting file to be named `dsbot`.

```bash
$ GOOS=linux go build -ldflags="-s -w" -o dsbot main.go
```

### Deploying

I am calling this stage deploying but it's not really - well not in the DevOps sense at least. All of this would be at least somewhat automated at some point. I find it's better to first wrap your head around the more manual steps to have a good understanding of what is happening. Anyway, now we'll turn back to the Google Cloud SDK. It can do a lot of stuff but we should only need to focus on a few commands. First, we'll want to make sure our compute zone is set - I'm using `us-west1-a` so I set that in the config, use the zone you chose when making the VM.

```bash
$ gcloud config set compute/zone us-west1-a
```

We'll also set the project, again use the project name you chose above.

```bash
$ gcloud config set project golang-slackbot
```

To move or Linux executable over to the VM we'll need to SCP (secure copy) the file. The command breaks down like this `scp <local filename> <remote instance>:<remote filename>`. We should still be in the directory the bot was compiled in so we run the following.

```bash
$ gcloud compute scp ./dsbot slackbot:dsbot
```

Whew! The end is in sight! Now lets SSH into our VM.

```bash
$ gcloud compute ssh slackbot
```

You could just run the executable similar to how we did in the last post with `SLACKTOKEN=slacktoken dsbot` however, that won't do for us. We're going to set it up as a service! So if Google does maintenance on the cluster we're hosted on once the machine has restarted the bot will auto-restart. Once connected to the remote server verify dsbot made it.

```bash
steve@slackbot:~$ ls -al
total 5240
drwxr-xr-x 3 steve steve    4096 Nov 26 17:56 .
drwxr-xr-x 4 root  root     4096 Nov 26 05:23 ..
-rw------- 1 steve steve     110 Nov 26 18:21 .bash_history
-rw-r--r-- 1 steve steve     220 May 15  2017 .bash_logout
-rw-r--r-- 1 steve steve    3526 May 15  2017 .bashrc
-rwxr-xr-x 1 steve steve 5334816 Nov 26 17:55 dsbot
-rw-r--r-- 1 steve steve     675 May 15  2017 .profile
drwx------ 2 steve steve    4096 Nov 26 17:55 .ssh
```

Perfect!

### Systemd

Our VM, as long as you set it up the same way I did, should be running `systemd`. We'll use this to create a system service to manage our bot. There is a good primer on systemd over on [Digital Ocean](https://www.digitalocean.com/community/tutorials/systemd-essentials-working-with-services-units-and-the-journal) if you are not familiar with it. We'll use the trusty old `vim` to set up our service file. 

```bash
$ sudo vim /etc/systemd/system/dsbot.service
```

Once in `vim` enter Insert mode by pressing `i` then type out or paste in the following. You will need to update the home directory location to where ever you copied the dsbot executable to. It doesn't need to stay in the home directory you could also move it over to say `/opt/dsbot` or similar. You will also need to get your Slack token from your Slack instance at https://YOURINSTANCENAME.slack.com/apps/A0F7YS25R-bots, update the environment variable accordingly.

```service
[Unit]
Description=dsbot for slack
After=network.target

[Service]
Type=simple
User=steve
WorkingDirectory=/home/steve
ExecStart=/home/steve/dsbot
Restart=on-abort

Environment=SLACKTOKEN=xoxb-slacktoken

[Install]
WantedBy=multi-user.target
```

Now Google how to save and exit vim (`ESC:wq`). Once back at the prompt we can enable the service.

```bash
$ sudo systemctl enable dsbot.service
```

Finally, start it up!

```bash
$ sudo systemctl start dsbot
```

And that's it! You should now have your very own bot running on a VM in the cloud - and with free hosting at that. Next time we'll scrap the VM and see what we can do with Docker and Kubernetes.

![Praise the Sun](https://thepracticaldev.s3.amazonaws.com/i/jcuy55klegvgufizake2.png)

---

| Enjoy this post? |
|:----:|
| [How about buying me a coffee?](https://ko-fi.com/shindakun) |

---