├── .gitignore ├── .npmignore ├── History.md ├── Makefile ├── Readme.md ├── example.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | support 2 | test 3 | examples 4 | *.sock 5 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 1.0.1 / 2015-10-25 3 | ================== 4 | 5 | * remove console.log 6 | 7 | 1.0.0 / 2010-01-03 8 | ================== 9 | 10 | * Initial release 11 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | test: 3 | @./node_modules/.bin/mocha \ 4 | --require should \ 5 | --reporter spec 6 | 7 | .PHONY: test -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | # send-to-slack 3 | 4 | Simple way to send messages to slack. Works on both the client and server. 5 | 6 | ## Installation 7 | 8 | ``` 9 | npm install send-to-slack 10 | ``` 11 | 12 | ### Usage 13 | 14 | ```js 15 | var Slack = require('send-to-slack')(webhook_url) 16 | var send = Slack({ 17 | channel: '#general', 18 | username: 'ava' 19 | }) 20 | 21 | send('hi i am ava!', { 22 | field: 'value' 23 | }) 24 | ``` 25 | 26 | Take a look at the [example.js](example.js) for more ways to use `send-to-slack`. 27 | 28 | ## Things to Note about the Slack API 29 | 30 | If you want to make your links clickable, you'll need to wrap them in open and close angle brackets. Example: `http://google.com => `. 31 | 32 | ## License 33 | 34 | (The MIT License) 35 | 36 | Copyright (c) 2015 Matthew Mueller <matt@lapwinglabs.com> 37 | 38 | Permission is hereby granted, free of charge, to any person obtaining 39 | a copy of this software and associated documentation files (the 40 | 'Software'), to deal in the Software without restriction, including 41 | without limitation the rights to use, copy, modify, merge, publish, 42 | distribute, sublicense, and/or sell copies of the Software, and to 43 | permit persons to whom the Software is furnished to do so, subject to 44 | the following conditions: 45 | 46 | The above copyright notice and this permission notice shall be 47 | included in all copies or substantial portions of the Software. 48 | 49 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 50 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 51 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 52 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 53 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 54 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 55 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 56 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | 2 | var assert = require('assert') 3 | 4 | // slack message 5 | assert.ok(process.env.SLACK_URL, ` 6 | Must include SLACK_URL as an env to run this test. 7 | 8 | You can generate one here: https://TEAM_NAME.slack.com/services/new/incoming-webhook 9 | `) 10 | 11 | var slack = require('./')(process.env.SLACK_URL) 12 | 13 | var send = slack({ 14 | channel: '@mnm' 15 | }) 16 | 17 | send('hi @mnm!', { 18 | test: 'value this is another value', 19 | woohoo: 'value this is another value' 20 | }) 21 | 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Getting the URL for slack 3 | * 4 | * - https://TEAM_NAME.slack.com/services/new/incoming-webhook 5 | */ 6 | 7 | /** 8 | * Module Dependencies 9 | */ 10 | 11 | var debug = require('debug')('send-to-slack') 12 | var superagent = require('superagent') 13 | var assign = require('object-assign') 14 | var is_array = Array.isArray 15 | 16 | /** 17 | * Export `Slack` 18 | */ 19 | 20 | module.exports = Slack 21 | 22 | /** 23 | * Pass token into slack 24 | */ 25 | 26 | function Slack (url) { 27 | return function slack (options) { 28 | options = options || {} 29 | 30 | // defaults 31 | options.username = options.username || 'send-to-slack' 32 | if (!options.icon_url && !options.icon_emoji) { 33 | options.icon_emoji = ':bell:' 34 | } 35 | 36 | return function send (message, fields) { 37 | var data = assign({}, options) 38 | data.text = message 39 | 40 | if (fields) { 41 | if (!data.attachments) data.attachments = [] 42 | 43 | if (is_array(fields)) { 44 | data.attachments = data.attachments.concat(fields) 45 | } else { 46 | fields = Object.keys(fields).map(function (title) { 47 | return { 48 | title: title, 49 | value: fields[title], 50 | short: (fields[title] + '').length < 35 51 | } 52 | }) 53 | 54 | data.attachments.push({ 55 | fallback: message, 56 | fields: fields 57 | }) 58 | } 59 | } 60 | 61 | debug('sending to slack: %j', data) 62 | superagent.post(url) 63 | .type('form') 64 | .send({ payload: JSON.stringify(data) }) 65 | .end(function (err, res) { 66 | if (err) debug('could not send to slack: %s', err.stack) 67 | else debug('sent to slack: %j', data) 68 | }) 69 | } 70 | } 71 | } 72 | 73 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "send-to-slack", 3 | "version": "1.0.1", 4 | "description": "Simple way to send messages to slack. Works on both the client and server.", 5 | "keywords": [ 6 | "slack", 7 | "send", 8 | "simple" 9 | ], 10 | "author": "Matthew Mueller ", 11 | "repository": { 12 | "type": "git", 13 | "url": "git://github.com/LapwingLabs/send-to-slack.git" 14 | }, 15 | "dependencies": { 16 | "debug": "^2.2.0", 17 | "object-assign": "^4.0.1", 18 | "superagent": "^1.4.0" 19 | }, 20 | "devDependencies": {}, 21 | "main": "index" 22 | } --------------------------------------------------------------------------------