Last active
December 22, 2015 09:41
-
-
Save yamafaktory/c6424f37ce5406cf2442 to your computer and use it in GitHub Desktop.
Slack message module using the incoming webhook API.
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
;(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 () {} | |
}()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Basic usage: