Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to new Slack webhook auth (version 0.1.0 of node-slackr) #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ var session = new Session({
token: token,
devChannel: '#bottest',
webhookClient: {
token: 'xxxxxxxxxxxxxxxxxxxx',
webhookUrl: 'xxxxxxxxxxxxxxxxxxxx',
team: 'tumblr',
username: 'ghbot',
icon_url: 'http://michaelsharman.com/static/images/github_metro.png'
}
});
```

All the data in the webhook client can be used but the only required ones are token and team.
All the data in the webhook client can be used but the only required one is webhookUrl. The webhookUrl is the value specified on Incoming Webhooks page of your Slack team.

The other option seen in this session instantiation is the dev channel. With this the bot will ignore anything in
that channel unless `NODE_ENV=development` is set. In which case it will only listen to that channel.
Expand Down
30 changes: 26 additions & 4 deletions lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var _ = require('lodash');
var Slack = require('node-slackr');
var request = require('request');
var WebSocket = require('ws');
var Q = require('q');

var Channel = require('./channel');
var Event = require('./event');
Expand Down Expand Up @@ -43,14 +44,19 @@ function Session(token, options) {

this.messageHandlers = [];

this._connectPromise = Q.defer();
request.get(url, function(err, response, body) {
if (err) {
this._connectPromise.reject(err);
throw('error:', err);
}

var data = JSON.parse(body);

this.data = data;

// Note that we could resolve here, but we don't, since
// we want the websocket to connect.
}.bind(this));

this._channelMessagePings = {};
Expand Down Expand Up @@ -226,6 +232,14 @@ Session.prototype = {
sendMessage: function(msg) {
msg = formatMessage(msg);

if (msg.channel) {
// slack now expects an id for the channel, rather than a name
var known = this.channelData(msg.channel);
if (known) {
msg.channel = known.id;
}
}

if (msg.attachments && this.webhookClient) {
this.webhookClient.notify(msg);

Expand Down Expand Up @@ -268,6 +282,10 @@ Session.prototype = {
this.ws = new WebSocket(data.url);

this.ws.on('message', this.gotMessage);

this.ws.on('open', function () {
this._connectPromise.resolve(this);
}.bind(this));
},

get webhookClient() {
Expand All @@ -279,11 +297,15 @@ Session.prototype = {
this._webhookClient = client;
} else {
var options = _.clone(client);
delete options.team;
delete options.token;

this._webhookClient = new Slack(client.team, client.token, options);
var hook = options.webhookUrl;
delete options.webhookUrl;
this._webhookClient = new Slack(hook, options);
}
},

get connected() {
return this._connectPromise.promise;
}
};

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "slackr-bot",
"version": "0.0.2",
"version": "0.0.3",
"description": "API for using the slack RTM interface",
"main": "index.js",
"scripts": {
Expand All @@ -17,9 +17,10 @@
"author": "Tal Atlas <[email protected]> (http://tal.by/)",
"license": "MIT",
"dependencies": {
"node-slackr": "0.0.4",
"node-slackr": "0.1.0",
"lodash": "^2.4.1",
"request": "^2.47.0",
"ws": "^0.6.3"
"ws": "^0.6.3",
"q": "1.4.1"
}
}