grunt-slack-api
Advanced tools
Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "grunt-slack-api", | ||
"description": "A more comprehensive slack api integration using grunt.", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"homepage": "https://github.com/nerrad/grunt-slack-api", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -32,34 +32,37 @@ # grunt-slack-api | ||
your_target: { | ||
type : 'message', | ||
channel : '#general', | ||
text : 'Your message', | ||
attachments: [ | ||
{ | ||
"fallback": "Required plain-text summary of the attachment.", | ||
"color": "#36a64f", | ||
"pretext": "Optional text that appears above the attachment block", | ||
"author_name": "Bobby Tables", | ||
"author_link": "http://flickr.com/bobby/", | ||
"author_icon": "http://flickr.com/icons/bobby.jpg", | ||
"title": "Slack API Documentation", | ||
"title_link": "https://api.slack.com/", | ||
"text": "Optional text that appears within the attachment", | ||
"fields": [ | ||
{ | ||
"title": "Priority", | ||
"value": "High", | ||
"short": false | ||
} | ||
], | ||
"image_url": "http://my-website.com/path/to/image.jpg" | ||
} | ||
], | ||
as_user : false, | ||
username : 'GruntSlackBot', | ||
parse : 'full', | ||
link_names : 1, | ||
unfurl_links : true, | ||
unfurl_media : false, | ||
icon_url : '', | ||
icon_emoji : ':chart_with_upwards_trend:' | ||
options: { | ||
type : 'message', | ||
channel : '#general', | ||
text : 'Your message', | ||
attachments: [ | ||
{ | ||
"fallback": "Required plain-text summary of the attachment.", | ||
"color": "#36a64f", | ||
"pretext": "Optional text that appears above the attachment block", | ||
"author_name": "Bobby Tables", | ||
"author_link": "http://flickr.com/bobby/", | ||
"author_icon": "http://flickr.com/icons/bobby.jpg", | ||
"title": "Slack API Documentation", | ||
"title_link": "https://api.slack.com/", | ||
"text": "Optional text that appears within the attachment", | ||
"fields": [ | ||
{ | ||
"title": "Priority", | ||
"value": "High", | ||
"short": false | ||
} | ||
], | ||
"image_url": "http://my-website.com/path/to/image.jpg", | ||
"markdwn_in" : ["text","pretext"] | ||
} | ||
], | ||
as_user : false, | ||
username : 'GruntSlackBot', | ||
parse : 'full', | ||
link_names : 1, | ||
unfurl_links : true, | ||
unfurl_media : false, | ||
icon_url : '', | ||
icon_emoji : ':chart_with_upwards_trend:' | ||
} | ||
}, | ||
@@ -88,3 +91,3 @@ }, | ||
#### your_target.type | ||
#### options.type | ||
Type: `String` | ||
@@ -98,3 +101,3 @@ Default value: `message` | ||
#### your_target.channel | ||
#### options.channel | ||
Type: `String` | ||
@@ -105,9 +108,9 @@ Default value: `#general` | ||
#### your_target.text | ||
#### options.text | ||
Type: `String` | ||
Default value: '' | ||
When "topic" is the `your_target.type`, the content of this string is used for the topic. Otherwise, this is the content posted to the channel. Follow the formatting guidelines here: https://api.slack.com/docs/formatting | ||
When "topic" is the `options.type`, the content of this string is used for the topic. Otherwise, this is the content posted to the channel. Follow the formatting guidelines here: https://api.slack.com/docs/formatting | ||
#### your_target.attachments | ||
#### options.attachments | ||
Type: `Array` | ||
@@ -120,11 +123,11 @@ Default value: null | ||
For all of the below keys, see https://api.slack.com/methods/chat.postMessage, these are only used when `your_target.type` is set to "message". | ||
For all of the below keys, see https://api.slack.com/methods/chat.postMessage, these are only used when `options.type` is set to "message". | ||
- your_target.as_user | ||
- your_target.username | ||
- your_target.parse | ||
- your_target.link_names | ||
- your_target.unfurl_links | ||
- your_target.unfurl_media | ||
- your_target.icon_url | ||
- your_target.icon_emoji | ||
- options.as_user | ||
- options.username | ||
- options.parse | ||
- options.link_names | ||
- options.unfurl_links | ||
- options.unfurl_media | ||
- options.icon_url | ||
- options.icon_emoji |
@@ -12,2 +12,3 @@ /* | ||
var request = require( 'superagent' ); | ||
var querystring = require('querystring'); | ||
@@ -24,4 +25,4 @@ module.exports = function(grunt) { | ||
var data = { | ||
type : ! this.data.type ? 'message' : this.data.type, | ||
text : this.data.text | ||
type : ! options.type ? 'message' : options.type, | ||
text : options.text | ||
} | ||
@@ -33,7 +34,10 @@ | ||
return; | ||
} else { | ||
data.token = options.token; | ||
} | ||
/** channel? **/ | ||
if ( this.data.channel) { | ||
data.channel = this.data.channel; | ||
if ( options.channel) { | ||
data.channel = options.channel; | ||
} else { | ||
@@ -51,2 +55,9 @@ data.channel = '#general'; | ||
switch ( data.type ) { | ||
case 'getChannelInfo' : | ||
options.endpoint += 'channels.info'; | ||
if ( ! options.callback ) { | ||
grunt.log.error( 'Callback is required for handling the response of a channel info call.' ); | ||
return; | ||
} | ||
break; | ||
case 'topic' : | ||
@@ -62,38 +73,47 @@ options.endpoint += 'channels.setTopic'; | ||
**/ | ||
if ( this.data.attachments ) { | ||
data.attachments = this.data.attachments; | ||
if ( options.attachments ) { | ||
data.attachments = JSON.stringify(options.attachments); | ||
} | ||
if ( typeof this.data.as_user !== 'undefined' ) { | ||
data.as_user = this.data.as_user; | ||
if ( typeof options.as_user !== 'undefined' ) { | ||
data.as_user = options.as_user; | ||
} | ||
if ( this.data.username ) { | ||
data.username = this.data.username; | ||
if ( options.username ) { | ||
data.username = options.username; | ||
} else { | ||
data.as_user = true; | ||
} | ||
if ( this.data.parse ) { | ||
data.parse = this.data.parse; | ||
if ( options.parse ) { | ||
data.parse = options.parse; | ||
} | ||
if ( typeof this.data.link_names !== 'undefined' ) { | ||
data.link_names = this.data.link_names; | ||
if ( typeof options.link_names !== 'undefined' ) { | ||
data.link_names = options.link_names; | ||
} | ||
if ( typeof this.data.unfurl_links !== 'undefined' ) { | ||
data.unfurl_links = this.data.unfurl_links; | ||
if ( typeof options.unfurl_links !== 'undefined' ) { | ||
data.unfurl_links = options.unfurl_links; | ||
} | ||
if ( typeof this.data.unfurl_media !== 'undefined' ) { | ||
data.unfurl_media = this.data.unfurl_media; | ||
if ( typeof options.unfurl_media !== 'undefined' ) { | ||
data.unfurl_media = options.unfurl_media; | ||
} | ||
if ( this.data.icon_url ) { | ||
data.icon_url = this.data.icon_url; | ||
if ( options.icon_url ) { | ||
data.icon_url = options.icon_url; | ||
} | ||
if ( this.data.icon_emoji ) { | ||
data.icon_emoji = this.data.icon_emoji | ||
if ( options.icon_emoji ) { | ||
data.icon_emoji = options.icon_emoji | ||
} | ||
if ( options.fields ) { | ||
data.fields = JSON.stringify( options.fields ); | ||
} | ||
break; | ||
} | ||
console.log(data); | ||
var stringified = querystring.stringify(data); | ||
request.post( options.endpoint ) | ||
.type( 'form' ) | ||
.send( 'payload=' + JSON.stringify(data) ) | ||
.type('form') | ||
.send( stringified ) | ||
.end( function(res ) { | ||
console.log(res.text); | ||
if ( ! res.ok ) { | ||
@@ -104,2 +124,5 @@ grunt.log.error( 'Error with slack api: ', res.text ); | ||
grunt.log.writeln( "Finished communicating with slack." ); | ||
if ( options.callback ) { | ||
options.callback | ||
} | ||
done(); | ||
@@ -106,0 +129,0 @@ }) |
15086
216
129