Skip to content

Instantly share code, notes, and snippets.

@moinism
Last active June 11, 2017 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moinism/3114960a39b76c2f96de745706e41476 to your computer and use it in GitHub Desktop.
Save moinism/3114960a39b76c2f96de745706e41476 to your computer and use it in GitHub Desktop.
Tiny Slack Web API wrapper for Nodejs

Usage

var Slack = require('./SlackAPI.js')

Use bot or user token for authentication.

var WebAPI = Slack(<TOKEN>)

Call the API methods.

WebAPI.call('chat.postMessage', {
  channel: 'general',
  text: 'API is simple(rr)',
  attachments: JSON.stringify([
    {
      fields: [{
        'title': 'Status',
        'value': '👍'
      }]
    }
  ])
}).then(response => {
  // it went smoothly!
}).catch(error => {
  // something shucks..
})
/*jshint esversion: 6 */
module.exports = function (token) {
const request = require('request');
const BASE_API = 'https://slack.com/api/';
return {
call: function (name, qs) { // name could be 'im.replies', etc.
qs = qs || {};
qs.token = token;
return new Promise((resolve, reject) => {
request({
qs: qs,
method: 'GET',
url: BASE_API + name
}, function (error, response, body) {
var b = JSON.parse(body);
if(error || !b.ok) {
reject(error || b);
} else {
resolve(b);
}
});
});
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment