diff --git a/slack.js b/slack.js index 9282665..780e8b6 100644 --- a/slack.js +++ b/slack.js @@ -3,9 +3,10 @@ var request = require('request'); var deferred = require('deferred'); -function Slack(domain, token, http_proxy_options) { +function Slack(domain, token, http_proxy_options, token2) { this.domain = domain; this.token = token; + this.token2 = token2 || token; this.http_proxy_options = http_proxy_options; } @@ -70,4 +71,55 @@ Slack.prototype.respond = function(query,cb) { } }; +Slack.prototype.message = function(message, cb) { + if (!message.text) { + if (cb) cb.call(null,{message:'No text specified'},null); + return; + } + if (!message.channel) { message.channel = '#general'; } + + var command = 'https://' + this.domain + '.slack.com/api/chat.postMessage?token=' + this.token2 + '&channel='+encodeURI(message.channel)+'&text='+encodeURI(message.text)+'&username='+encodeURI(message.username); + if (message.icon_url) { command.concat("&icon_url=",encodeURI(message.icon_url)) } + if (message.icon_emoji) { command.concat("&icon_emoji=",encodeURI(message.icon_emoji)); } + var option = { + proxy: (this.http_proxy_options && this.http_proxy_options.proxy) || process.env.https_proxy || process.env.http_proxy, + url: command + }; + + if(!cb) var d = deferred(); + + var req = request.get(option, 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.channelList = function(cb) { + var command = "https://slack.com/api/channels.list?token="+this.token2+"&exclude_archived=1"; + var option = { + proxy: (this.http_proxy_options && this.http_proxy_options.proxy) || process.env.https_proxy || process.env.http_proxy, + url: command, + json: true + }; + if (!cb) var d = deferred(); + + var req = request.get(option, 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; +}; + module.exports = Slack;