1
1
var request = require ( 'request' ) ;
2
2
3
+ /**
4
+ * Does nothing. Takes no params, returns nothing. It's a no-op!
5
+ */
3
6
function noop ( ) { }
4
7
8
+ /**
9
+ *
10
+ * @param {Object } bot The botkit bot object
11
+ * @param {Object } config A config containing auth credentials.
12
+ * @returns {Object } A callback-based Slack API interface.
13
+ */
5
14
module . exports = function ( bot , config ) {
6
15
var slack_api = {
7
16
api_url : 'https://slack.com/api/'
@@ -79,28 +88,40 @@ module.exports = function(bot, config) {
79
88
'users.setActive'
80
89
] ;
81
90
82
- slack_api . callAPI = function ( command , options , cb ) {
83
- if ( ! options . token ) {
84
- options . token = config . token ;
91
+ /**
92
+ * Calls Slack using a Token for authentication/authorization
93
+ * @param {string } command The Slack API command to call
94
+ * @param {Object } data The data to pass to the API call
95
+ * @param {function } cb A NodeJS-style callback
96
+ */
97
+ slack_api . callAPI = function ( command , data , cb ) {
98
+ if ( ! data . token ) {
99
+ data . token = config . token ;
85
100
}
86
101
87
- bot . debug ( command , options ) ;
88
- postForm ( slack_api . api_url + command , options , cb ) ;
102
+ bot . debug ( command , data ) ;
103
+ postForm ( slack_api . api_url + command , data , cb ) ;
89
104
} ;
90
105
91
- slack_api . callAPIWithoutToken = function ( command , options , cb ) {
92
- if ( ! options . client_id ) {
93
- options . client_id = bot . config . clientId ;
106
+ /**
107
+ * Calls Slack using OAuth for authentication/authorization
108
+ * @param {string } command The Slack API command to call
109
+ * @param {Object } data The data to pass to the API call
110
+ * @param {function } cb A NodeJS-style callback
111
+ */
112
+ slack_api . callAPIWithoutToken = function ( command , data , cb ) {
113
+ if ( ! data . client_id ) {
114
+ data . client_id = bot . config . clientId ;
94
115
}
95
- if ( ! options . client_secret ) {
96
- options . client_secret = bot . config . clientSecret ;
116
+ if ( ! data . client_secret ) {
117
+ data . client_secret = bot . config . clientSecret ;
97
118
}
98
- if ( ! options . redirect_uri ) {
99
- options . redirect_uri = bot . config . redirectUri ;
119
+ if ( ! data . redirect_uri ) {
120
+ data . redirect_uri = bot . config . redirectUri ;
100
121
}
101
122
102
123
// DON'T log options: that could expose the client secret!
103
- postForm ( slack_api . api_url + command , options , cb ) ;
124
+ postForm ( slack_api . api_url + command , data , cb ) ;
104
125
} ;
105
126
106
127
@@ -141,11 +162,11 @@ module.exports = function(bot, config) {
141
162
142
163
/**
143
164
* Makes a POST request as a form to the given url with the options as data
144
- * @param url
145
- * @param options
146
- * @param cb
165
+ * @param { string } url The URL to POST to
166
+ * @param { Object } formData The data to POST as a form
167
+ * @param { function= } cb An optional NodeJS style callback when the POST completes or errors out.
147
168
*/
148
- function postForm ( url , options , cb ) {
169
+ function postForm ( url , formData , cb ) {
149
170
cb = cb || noop ;
150
171
151
172
bot . log ( '** API CALL: ' + url ) ;
@@ -165,6 +186,6 @@ module.exports = function(bot, config) {
165
186
return cb ( json . error , json ) ;
166
187
}
167
188
return cb ( error || new Error ( 'Invalid response' ) ) ;
168
- } ) . form ( options ) ;
189
+ } ) . form ( formData ) ;
169
190
}
170
191
} ;
0 commit comments