Angel \”Java\” Lopez on Blog

February 6, 2015

Sending messages to Slack using JavaScript/Node.js

Filed under: JavaScript, NodeJs, Slack — ajlopez @ 12:42 pm

A year ago, Slack entered in my radar:

https://slack.com/

I read “Slack is a platform for team communication; everything in one place, instantly searcheable, available wherever you go”.

In general, I am somewhat skeptical to online tools like this, I prefer a simpler combination. For the above statement, I simply use gmail and mailing lists per project or team. The interesting story behind Slack founder:

http://www.wired.com/2014/08/the-most-fascinating-profile-youll-ever-read-about-a-guy-and-his-boring-startup/

My links about Slack at:

https://delicious.com/ajlopez/slack

Example:

https://medium.com/@slackhq/11-useful-tips-for-getting-the-most-of-slack-5dfb3d1af77

Past wednesday, at afternoon, I started to explore how to send programmatically a message to Slack, using their exposed API, using Node.js (I think Node is the simplest technology to use in this kind of experiments). More general info about the API at:

https://api.slack.com/

My current experiment code:

https://github.com/ajlopez/NodeSamples/tree/master/Slack

I searched in Google for some Node.js module to access Slack, my first result:

https://github.com/xoxco/node-slack

Initially I installed it using npm, in a working directory. My first try:

var domain = process.env.SLACK_DOMAIN;
var token = process.env.SLACK_TOKEN;

var Slack = require('node-slack');
var slack = new Slack(domain, token);

var channel = process.argv[2];
var message = process.argv[3];

console.log('Sending message', message, 'to channel', channel);

slack.send({
    text: message,
    channel: '#' + channel,
    username: username
});

You must set environment variables: SLACK_DOMAIN and SLACK_TOKEN. When you enter to a Slack group, the group has an associated domain. For example, I’m a member of https://squeakros.slack.com the group for Smalltalk Squek Rosario. Other group is https://nodejsar.slack.com, Node.js Argentina. The provided token correspond to a user AND an applicaction, it can be obtained from:

https://api.slack.com/web

The above program run, without errors, being invoked via

node run.js general “Hello, world”

(on Windows). The first argument after run.js is the target channel, without the initial #, and the second argument is the message text. All OK, but the message didn’t arrive to the channel. After a bit of research, I found the Pull Request:

https://github.com/xoxco/node-slack/pull/8

that it was not merged with the original source, yet. Some months ago, the Slack API changed. The suggested pull requests uses another access point, a web hook URL provided from:

https://your-domain.slack.com/services/new/incoming-webhook

I applied the suggestion I found in one comment, and I put directly the link to the new code, in my package.json:

{
  "name": "slack-demo",
  "private": true,
  "version": "0.0.1",
  "dependencies": {
    "node-slack": "git://github.com/Dahlgren/node-slack.git#hook-url"
  }
}

An now, my program use the web hook URL, provided via a variable environment:

var hookUrl = process.env.SLACK_WEBHOOK;
var username = process.env.SLACK_USER;

var Slack = require('node-slack');
//var slack = new Slack(domain, token);
var slack = new Slack(hookUrl);

var channel = process.argv[2];
var message = process.argv[3];

console.log('Sending message', message, 'to channel', channel);

slack.send({
    text: message,
    channel: '#' + channel,
    username: username
});

Now, it works! Topics to research: any way to use domain/token again? other modules?.

Stay tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

Blog at WordPress.com.