Created
January 30, 2015 09:34
-
-
Save symonny/b3541632b1bfbdad2e71 to your computer and use it in GitHub Desktop.
slack.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*based on https://github.com/xoxco/node-slack/blob/master/slack.js*/ | |
/* add file in /lib | |
init in server.js | |
var configs = { | |
token:"XXX", | |
channel:"#studio-binder", | |
username:"studibinder", | |
domain:"leanometry", | |
icon_url: 'http://studiobinder.com/static/img/email-bubble.png', | |
notify:true | |
} | |
var Slack = require('./lib/slack'); | |
var slack = new Slack(configs); | |
slack.sendNotification('test'); | |
*/ | |
"use strict"; | |
var request = require('request'); | |
var deferred = require('deferred'); | |
function Slack(configs, http_proxy_options) { | |
this.domain = configs.domain; | |
this.token = configs.token; | |
this.channel = configs.channel; | |
this.notify = configs.notify; | |
this.username = configs.username; | |
this.icon_url = configs.icon_url; | |
this.http_proxy_options = http_proxy_options; | |
} | |
Slack.prototype.sendNotification = function(message, cb) { | |
if(this.notify) { | |
this.send({text: message}, cb) ; | |
} | |
} | |
Slack.prototype.send = function(message, cb) { | |
if (!message.text) { | |
if (cb) cb.call(null,{message:'No text specified'},null); | |
return; | |
} | |
if (!message.channel) { message.channel = this.channel ? this.channel : '#general'; } | |
if (!message.username) { message.username = this.username ; } | |
if (!message.icon_url) { message.icon_url = this.icon_url ; } | |
var url = 'https://slack.com/api/chat.postMessage'; | |
var qs = { | |
token :this.token, | |
channel: encodeURIComponent(message.channel), | |
text: encodeURIComponent(message.text ) , | |
username: encodeURIComponent( message.username) , | |
pretty:1 | |
}; | |
if (message.icon_url) { qs.icon_url = message.icon_url; } | |
if (message.icon_emoji) { qs.icon_emoji = message.icon_emoji; } | |
if (message.attachments) { qs.attachments = message.attachments; } | |
if (message.unfurl_links) { qs.unfurl_links = message.unfurl_links; } | |
if (message.link_names) { qs.link_names = message.link_names; } | |
var queryString = function(query) { | |
var query_string = ''; | |
for (var key in query) { | |
query_string += ('&' + key + '=' + query[key]); | |
} | |
return query_string; | |
}; | |
url += '?' + queryString(qs); | |
if(!cb) var d = deferred(); | |
console.log(url); | |
var req = request.get(url, {}, function(err, res, body) { | |
if (!err && body!='ok') { | |
err = {message: body}; | |
body = null; | |
} | |
if (d) return err ? d.reject(err) : d.resolve({res: res, body: body}); | |
if (cb) return cb.call(null, err, body); | |
return null; | |
}); | |
return d ? d.promise : req; | |
}; | |
Slack.prototype.respond = function(query,cb) { | |
var obj = {}; | |
obj.token = query.token; | |
obj.team_id = query.team_id; | |
obj.channel_id = query.channel_id; | |
obj.channel_name = query.channel_name; | |
obj.timestamp = new Date(query.timestamp); | |
obj.user_id = query.user_id; | |
obj.user_name = query.user_name; | |
obj.text = query.text; | |
if (!cb) { | |
return {text:''}; | |
} else { | |
return cb.call(null,obj); | |
} | |
}; | |
module.exports = Slack; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment