Skip to content

Instantly share code, notes, and snippets.

@yamafaktory
Last active December 22, 2015 09:41
Show Gist options
  • Save yamafaktory/c6424f37ce5406cf2442 to your computer and use it in GitHub Desktop.
Save yamafaktory/c6424f37ce5406cf2442 to your computer and use it in GitHub Desktop.
Slack message module using the incoming webhook API.
;(function () {
'use strict'
/*
Slack message module using the incoming webhook API.
https://api.slack.com/incoming-webhooks
https://api.slack.com/docs/formatting
*/
module.exports = Slack
const https = require('https')
const querystring = require('querystring')
Slack.prototype.post = m => {
return new Promise((res, rej) => {
const payload = JSON.stringify({
'channel': '#some-channel',
'username': 'some-user',
'text': `@channel: ${m}`,
'icon_emoji': ':robot_face:',
'link_names': 1
})
const data = querystring.stringify({ payload: payload })
const post = https.request({
hostname: 'hooks.slack.com',
path: '/services/XXXXXXXXX/YYYYYYYYY/ZZZZZZZZZZZZZZZZZZZZZZZZ',
port: 443,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data)
}
}, r => res())
post.on('error', e => rej())
post.write(data)
post.end()
})
}
function Slack () {}
}())
@yamafaktory
Copy link
Author

Basic usage:

const Slack = require('./../slack')
const slack = Object.create(Slack.prototype)

yield slack.post('Hello world!')
// Do other stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment