1 | ![travis-ci](https://travis-ci.org/chenka/node-slackr.svg)
|
2 |
|
3 | Summary
|
4 | =======
|
5 | A simple node.js library for send notifications to [Slack](https://slack.com/) via Incoming WebHooks.
|
6 |
|
7 |
|
8 | Installation
|
9 | =======
|
10 | You can also install via npm:
|
11 | ```sh
|
12 | npm install node-slackr
|
13 | ```
|
14 |
|
15 | Initialize client:
|
16 |
|
17 | ```js
|
18 | Slack = require('node-slackr');
|
19 | slack = new Slack('https://<incoming-hook-url>');
|
20 | ```
|
21 |
|
22 | Initialize with options:
|
23 | ```js
|
24 | slack = new Slack('https://<incoming-hook-url>',{
|
25 | channel: "#development",
|
26 | username: "slack-bot",
|
27 | icon_url: "http://domain.com/image.png",
|
28 | icon_emoji: ":ghost:"
|
29 | });
|
30 | ```
|
31 |
|
32 | ###Send message:
|
33 |
|
34 | If channel is not set default channel is *#general*
|
35 | ```js
|
36 | slack.notify("Message"); //without callback
|
37 | slack.notify("Message", function(err, result){
|
38 | console.log(err,result);
|
39 | });
|
40 |
|
41 | ```
|
42 |
|
43 | ###Customized Appearance:
|
44 |
|
45 | You can customize the name and icon of your Incoming Webhook.
|
46 |
|
47 | ```js
|
48 | messages = {
|
49 | text: "Message",
|
50 | channel: "#random",
|
51 | username: "new-bot-name",
|
52 | icon_url: "https://slack.com/img/icons/app-57.png"
|
53 | }
|
54 |
|
55 | slack.notify(messages);
|
56 | ```
|
57 |
|
58 | Send multiple channels:
|
59 | ```js
|
60 | messages = {
|
61 | text: "Message",
|
62 | channel: ["#channel1","#channel2","#channel3"]
|
63 | }
|
64 |
|
65 | slack.notify(messages);
|
66 | ```
|
67 |
|
68 |
|
69 | ###Message Attachments:
|
70 | To display a richly-formatted message attachment in Slack, you can use the same JSON payload as above, but add in an attachments array. Each element of this array is a hash containing the following parameters:
|
71 |
|
72 | ```js
|
73 | messages = {
|
74 | text: "Server Down",
|
75 | channel: "#alert"
|
76 | attachments: [
|
77 | {
|
78 | fallback: "Detected server down",
|
79 | color: "#36a64f", // Can either be one of 'good', 'warning', 'danger'
|
80 | fields: [
|
81 | {
|
82 | title: "Uptime",
|
83 | value: "30 Hours",
|
84 | short: false
|
85 | },
|
86 | {
|
87 | title: "Downtime",
|
88 | value: "20 Minutes",
|
89 | short: false
|
90 | }
|
91 | ]
|
92 | }
|
93 | ]
|
94 | };
|
95 |
|
96 | slack.notify(messages, function(err, result) {
|
97 | console.log(err, result);
|
98 | });
|
99 |
|
100 | ```
|
101 |
|
102 | ###Documentation
|
103 |
|
104 | For more information such as send URL link, Message Formatting, @mention and Parsing modes, please follow the link below
|
105 |
|
106 | [Formatting](https://api.slack.com/docs/formatting)
|
107 |
|
108 | [Incomg Webook](https://my.slack.com/services/new/incoming-webhook)
|
109 |
|
110 |
|