Running Slackalytics on Google Compute Engine

Slack is an excellent messaging medium for teams and I have been part of teams that have been using it successfully for a while now. My interest in Slack is more along its API and Bots that we can write to customize it to our needs.

Nico Miceli, Google Developer Expert for Analytics, recently wrote a blog post titled Slackalytics, that was of particular interest to me. In the words of the author, Slackalytics is a Bot that is configured in your Slack Channel that can analyze the text messages and send this data over to Google Analytics, all in real time. How cool is that.

To summarize the blog post, you can add an Outgoing Hook to your Slack Team that will monitor each text message and it will currently analyze the text message for number of words, emoticons, question marks and more. This data will be pushed into Google Analytics (Custom Dimensions and Metrics).

I advise you to first read his blog post and understand what the capabilities of this bot in conjunction with Google Analytics can be.

Nico’s Slackalytics App is a Node.js app and the instructions were provided for Heroku, which is a fine PaaS in my opinion. What this blog post will demonstrate is to deploy that same application on the Google Cloud Platform, specifically the Compute Engine Service (IaaS).

slackalytics6

Follow these steps to get going on deploying Slackalytics on Google Compute Engine:

Create Google Cloud Project

The first step is to create a project. Follow these steps:

  • Visit Google Developers Console and login with your account.
  • Click on Create Project. This will bring up the New Project dialog. Enter a name for the project and ensure that you have selected the correct Billing Account. An example screenshot is shown below:slackalytics1
  • Click Create to begin the process of creating the project.

Be patient while the project is being created and initialized.

Provision a Compute Engine VM

We need to create an instance on which we can run Slackalytics. This means we need to provision a VM (Linux based) from the Compute Engine service. Follow these steps:

  • Click on Compute –> Compute Engine –> VM Instances.
  • Click on New Instance.
  • This will bring up a form for entering the details about your new instance. Pay attention to the following fields
    • Give your instance a name. For example, slackalytics-1
    • Select a zone closest to you. For example, us-central1-a
    • Select a machine type. I went with the lowest option f1-micro
    • Keep the boot disk image as the default one selected i.e. Debian
    • Leave all the other options at their default.
    • Click on the Create button.

Setup Node.js on your VM

To setup Node.js on our VM instance, we will need to SSH into the instance. Follow these steps:

  • Go to Compute –> Compute Engine –> VM Instances
  • You will see your VM instance (For example, slackalytics-1) listed.
  • Look out for the SSH button at the end of the row.
  • Click that and wait for the SSH session to get initialized and setup for you. If all is well, you should see another browser window that will transport you to the VM instance as shown below:slackalytics2
  • To install Node.js run the follow commands in the terminal above:
    curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash -
    

 Then run the following command:

sudo apt-get install -y nodejs

You can verify the version of node, by executing the following command:



node --version


Do not close your  SSH instance, we need it to setup Slackalytics.

Setup Slackalytics on your VM

All we need to do now is to get the Slackalytics project from Github. Before that, we will need to install git on this machine, so let us do that first:

Execute the following command to install it:


sudo apt-get install -y git

Now, let us clone the Slackalytics project from Github as shown below:


git clone https://github.com/NicoMiceli/slackalytics.git

You should see output similar to the following:


romin_irani@slackalytics-1:~$ git clone https://github.com/NicoMiceli/slackalytics.git
Cloning into 'slackalytics'...
remote: Counting objects: 89, done.
remote: Total 89 (delta 0), reused 0 (delta 0), pack-reused 89
Unpacking objects: 100% (89/89), done.
romin_irani@slackalytics-1:~$

The next step is to install the node packages that Slackalytics needs. This is specified by the package.json file that is present in the slackalytics folder. Follow these steps:

  • Go to the slackalytics directory. If you are in the home directory, just do a cd slackalytics.
  • Execute the following command : npm install

Be patient. It will install all the dependencies and you will be back at the prompt.

Changes made to Slackalytics app.js

I made some minor changes to the app.js file:

  • You will need to key in your Google Analytics UAID. Please look out for the following lines of code in app.js:
    var env_var = {
    ga_key: process.env.GOOGLE_ANALYTICS_UAID
    };
    
  • Replace the process.env.GOOGLE_ANALYTICS_UAID with your Property UAID.
  • Added a res.send(“OK”) just at the end of the loop for app.post(/collect‘…) handler. This will ensure that the response stream is closed out.
  • Changed the variable port to a value of 8080, since I want to run it on that port.

Save the app.js file before running the application.

Run Slackalytics

You can run Slackalytics anytime now via the following command:


node app.js

This will launch the node server and it will start listening on port 8080.

Note: You might want to start the node server in the background. A recommended option for now could be nohup node app.js &

Setup Firewall Rule for your VM

Note that Slackalytics is now up and running on port 8080. This will not be accessible from outside and hence we need to create a firewall that will allow this. To do that, do the following:

  1. Go to Google Developers Console for your Slackalytics project.
  2. Click on Networks –> Firewall rules.
  3. Click on Create a firewall rule.
  4. This will bring up a form to create a Firewall rule. Take a look at the expected values given below:slackalytics3
  5. Note that you can name the firewall rule anything, but make sure that the source IP range i.e. from where should this be accessible opened up for anyone. And the port on which traffic is to be allowed is 8080. Leave the network as default.
  6. Click on Create button to create the rule.

Setup the Outgoing Webhook in your Slack Channel

We are all set now and we only need to setup the Outgoing Webhook in your Slack Channel. Follow these steps:

  1. First get the External IP Address of your Compute Engine instance. To do that, from your Google Developers Console for the Slackalytics project, click on
    Compute –> Compute Engine –> VM Instances
  2. You will see your Slackalytics VM instance listed. You will also find a column named “External IP” . Note down this address. Let us call it <slackalytics-external-ip>
  3. Assuming that you have Administrator rights for your Slack Channel, login and go to Integrations.
  4. Go to All Integrations and type “outgoing” in the filter as shown below:slackalytics4
  5. This will show you the option for Outgoing Webhooks. Click on View and in the next page, click on Add Outgoing Webhooks Integration.
  6. This will bring up the Outgoing Webhook integration details that you need to fill up. Scroll down to a section title Integration Settings and provide the channel and the URL value as given below:slackalytics5
  7. Note that all we are instructing Slack is that if any message appears in the #general channel, send the payload with the parameters to the URL that we have configured below. Notice that the IP address will have to be the External IP address from your Google Compute Engine VM instance in which you installed Slackalytics. Click on Save Settings. You are all set now.

That’s it. Enjoy Slackalytics! Thanks Nico for this fine project.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s