|
| 1 | +/** |
| 2 | + * Direct Line API |
| 3 | + * https://docs.botframework.com/en-us/restapi/directline/ |
| 4 | + */ |
| 5 | +const baseUrl = "https://directline.botframework.com/api"; |
| 6 | +var request = require('request'); |
| 7 | +var Q = require('q'); |
| 8 | + |
| 9 | + |
| 10 | +function DirectLineClient() {} |
| 11 | + |
| 12 | +/** |
| 13 | + * Get token |
| 14 | + * @param {[type]} secret [description] |
| 15 | + * @return {[type]} [description] |
| 16 | + */ |
| 17 | +DirectLineClient.prototype.getToken = function(secret) { |
| 18 | + var defer = Q.defer(); |
| 19 | + request({ |
| 20 | + method: 'GET', |
| 21 | + uri: baseUrl + '/tokens', |
| 22 | + headers: { |
| 23 | + 'Authorization': 'BotConnector ' + secret |
| 24 | + } |
| 25 | + }, function(err, response, body) { |
| 26 | + if (err) return defer.reject({ |
| 27 | + rc: 1, |
| 28 | + error: err |
| 29 | + }); |
| 30 | + if (response.statusCode === 200) { |
| 31 | + return defer.resolve(body); |
| 32 | + } else { |
| 33 | + return defer.reject({ |
| 34 | + rc: 2, |
| 35 | + error: 'wrong status code.' |
| 36 | + }); |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + return defer.promise; |
| 41 | +}; |
| 42 | + |
| 43 | +/** |
| 44 | + * Create a conversation |
| 45 | + * @param {[type]} token [description] |
| 46 | + * @return {[type]} [description] |
| 47 | + */ |
| 48 | +DirectLineClient.prototype.createConversation = function(token) { |
| 49 | + var defer = Q.defer(); |
| 50 | + request({ |
| 51 | + method: 'POST', |
| 52 | + uri: baseUrl + '/conversations', |
| 53 | + headers: { |
| 54 | + 'Authorization': 'BotConnector ' + token, |
| 55 | + 'Accept': 'application/json' |
| 56 | + } |
| 57 | + }, function(err, response, body) { |
| 58 | + if (err) return defer.reject({ |
| 59 | + rc: 1, |
| 60 | + error: err |
| 61 | + }); |
| 62 | + if (response.statusCode === 200) { |
| 63 | + return defer.resolve(JSON.parse(body)); |
| 64 | + } else { |
| 65 | + return defer.reject({ |
| 66 | + rc: 2, |
| 67 | + error: 'wrong status code.' |
| 68 | + }); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + return defer.promise; |
| 73 | +}; |
| 74 | + |
| 75 | +/** |
| 76 | + * Post a message for a conversation |
| 77 | + * @param {[type]} token [description] |
| 78 | + * @param {[type]} conversationId [description] |
| 79 | + * @param {[type]} message [description] |
| 80 | + * @return {[type]} [description] |
| 81 | + */ |
| 82 | +DirectLineClient.prototype.postMessage = function(token, conversationId, message) { |
| 83 | + var defer = Q.defer(); |
| 84 | + request({ |
| 85 | + method: 'POST', |
| 86 | + uri: baseUrl + '/conversations/' + conversationId + '/messages', |
| 87 | + headers: { |
| 88 | + 'Authorization': 'BotConnector ' + token, |
| 89 | + 'Content-Type': 'application/json' |
| 90 | + }, |
| 91 | + json: true, |
| 92 | + body: JSON.stringify({ |
| 93 | + text: message |
| 94 | + }) |
| 95 | + }, function(err, response, body) { |
| 96 | + if (err) return defer.reject({ |
| 97 | + rc: 1, |
| 98 | + error: err |
| 99 | + }); |
| 100 | + if (response.statusCode === 204) { |
| 101 | + return defer.resolve(); |
| 102 | + } else { |
| 103 | + return defer.reject({ |
| 104 | + rc: 2, |
| 105 | + error: 'wrong status code.' |
| 106 | + }); |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + return defer.promise; |
| 111 | +}; |
| 112 | + |
| 113 | +/** |
| 114 | + * upload file |
| 115 | + * @param {[type]} token [description] |
| 116 | + * @param {[type]} conversationId [description] |
| 117 | + * @param {[type]} file [description] |
| 118 | + * @return {[type]} [description] |
| 119 | + */ |
| 120 | +DirectLineClient.prototype.postFileMessage = function(token, conversationId, formData) { |
| 121 | + var defer = Q.defer(); |
| 122 | + request({ |
| 123 | + method: 'POST', |
| 124 | + uri: baseUrl + '/conversations/' + conversationId + '/upload', |
| 125 | + headers: { |
| 126 | + 'Authorization': 'BotConnector ' + token, |
| 127 | + 'Content-Type': 'multipart/form-data' |
| 128 | + }, |
| 129 | + formData: formData |
| 130 | + }, function(err, response, body) { |
| 131 | + console.log(response); |
| 132 | + if (err) return defer.reject({ |
| 133 | + rc: 1, |
| 134 | + error: err |
| 135 | + }); |
| 136 | + if (response.statusCode === 204) { |
| 137 | + return defer.resolve(); |
| 138 | + } else { |
| 139 | + return defer.reject({ |
| 140 | + rc: 2, |
| 141 | + error: 'wrong status code.' |
| 142 | + }); |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + return defer.promise; |
| 147 | +}; |
| 148 | + |
| 149 | + |
| 150 | +/** |
| 151 | + * Get Messages for a conversation |
| 152 | + * @param {[type]} token [description] |
| 153 | + * @param {[type]} conversationId [description] |
| 154 | + * @param {[type]} watermark [description] |
| 155 | + * @return {[type]} [description] |
| 156 | + */ |
| 157 | +DirectLineClient.prototype.getMessages = function(token, conversationId, watermark) { |
| 158 | + var defer = Q.defer(); |
| 159 | + var watermarkStr = watermark ? 'watermark=' + watermark : ''; |
| 160 | + request({ |
| 161 | + method: 'GET', |
| 162 | + uri: baseUrl + '/conversations/' + conversationId + '/messages/?' + watermarkStr, |
| 163 | + headers: { |
| 164 | + 'Authorization': 'BotConnector ' + token, |
| 165 | + 'Accept': 'application/json' |
| 166 | + } |
| 167 | + }, function(err, response, body) { |
| 168 | + if (err) return defer.reject({ |
| 169 | + rc: 1, |
| 170 | + error: err |
| 171 | + }); |
| 172 | + if (response.statusCode === 200) { |
| 173 | + return defer.resolve(JSON.parse(body)); |
| 174 | + } else { |
| 175 | + return defer.reject({ |
| 176 | + rc: 2, |
| 177 | + error: 'wrong status code.' |
| 178 | + }); |
| 179 | + } |
| 180 | + }); |
| 181 | + |
| 182 | + return defer.promise; |
| 183 | +}; |
| 184 | + |
| 185 | +/** |
| 186 | + * generate a token for a new conversation. |
| 187 | + * #TODO what difference with GET /tokens ? |
| 188 | + * @param {[type]} token [description] |
| 189 | + * @param {[type]} conversationId [description] |
| 190 | + * @param {[type]} message [description] |
| 191 | + * @return {[type]} [description] |
| 192 | + */ |
| 193 | +DirectLineClient.prototype.generateConversationAndToken = function(secret) { |
| 194 | + var defer = Q.defer(); |
| 195 | + request({ |
| 196 | + method: 'POST', |
| 197 | + uri: baseUrl + '/tokens/conversation', |
| 198 | + headers: { |
| 199 | + 'Authorization': 'BotConnector ' + token |
| 200 | + } |
| 201 | + }, function(err, response, body) { |
| 202 | + if (err) return defer.reject({ |
| 203 | + rc: 1, |
| 204 | + error: err |
| 205 | + }); |
| 206 | + if (response.statusCode === 200) { |
| 207 | + return defer.resolve(body); |
| 208 | + } else { |
| 209 | + return defer.reject({ |
| 210 | + rc: 2, |
| 211 | + error: 'wrong status code.' |
| 212 | + }); |
| 213 | + } |
| 214 | + }); |
| 215 | + |
| 216 | + return defer.promise; |
| 217 | +}; |
| 218 | + |
| 219 | +/** |
| 220 | + * renew token by conversationId |
| 221 | + * @param {[type]} secret [description] |
| 222 | + * @param {[type]} conversationId [description] |
| 223 | + * @return {[type]} [description] |
| 224 | + */ |
| 225 | +DirectLineClient.prototype.renewConversationToken = function(token, conversationId) { |
| 226 | + var defer = Q.defer(); |
| 227 | + |
| 228 | + request({ |
| 229 | + method: 'GET', |
| 230 | + uri: baseUrl + '/tokens/' + conversationId + '/renew', |
| 231 | + headers: { |
| 232 | + 'Authorization': 'BotConnector ' + token |
| 233 | + } |
| 234 | + }, function(err, response, body) { |
| 235 | + if (err) return defer.reject({ |
| 236 | + rc: 1, |
| 237 | + error: err |
| 238 | + }); |
| 239 | + if (response.statusCode === 200) { |
| 240 | + return defer.resolve(body); |
| 241 | + } else { |
| 242 | + return defer.reject({ |
| 243 | + rc: 2, |
| 244 | + error: 'wrong status code.' |
| 245 | + }); |
| 246 | + } |
| 247 | + }); |
| 248 | + |
| 249 | + return defer.promise; |
| 250 | +}; |
| 251 | + |
| 252 | + |
| 253 | +exports = module.exports = new DirectLineClient(); |
0 commit comments