diff --git a/index.js b/index.js index e6ac0be..a3457fc 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ var http = require('http'); var https = require('https'); -var querystring = require('querystring'); +var Q = require('q'); +var cognitiveServices = require('./scripts/cognitive-services'); var allContactTypes = ['direct_message', 'direct_mention', 'mention']; @@ -21,43 +22,12 @@ var bot = controller.spawn({ token: process.env.token }).startRTM(); -controller.hears('cognitive test', ['ambient'], function(bot, message) { - bot.reply(message, 'on it, boss'); - - var responseData; - var postData = querystring.stringify({ - 'language' : 'en', - 'analyzerIds' : ['08ea174b-bfdb-4e64-987e-602f85da7f72'], - 'text' : 'Hi, Tom! How are you today?' - }); - - var options = { - host: 'api.projectoxford.ai', - path: '/linguistics/v1.0/analyze', - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Cache-Control': 'no-cache', - 'Ocp-Apim-Subscription-Key': '02829fac842a458d9fa90242754c6721', //replace with process.env.MSCSToken - } - }; - - var req = https.request(options, (res) => { - res.setEncoding('utf8'); - res.on('data', (chunk) => { - responseData += chunk; - }); - res.on('end', () => { - bot.reply(message, responseData); - }) - }); - - req.on('error', (e) => { - console.log(`problem with request: ${e.message}`); - }); - - req.write(postData); - req.end(); +controller.hears('(.*)', allContactTypes, function(bot, message) { + bot.reply(message, 'I am at your service'); + + cognitiveServices.analyzeText(message).then(function (analyzedText) { + bot.reply(message, analyzedText); + }); }); controller.hears(['that conference'], ['ambient'], function(bot, message) { diff --git a/package.json b/package.json index 1c6f9d9..874a867 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ }, "homepage": "https://github.com/ThatConference/that-bot#readme", "dependencies": { - "botkit": "^0.1.1" + "botkit": "^0.1.1", + "q": "^1.4.1" } } diff --git a/scripts/cognitive-services.js b/scripts/cognitive-services.js new file mode 100644 index 0000000..fa79d98 --- /dev/null +++ b/scripts/cognitive-services.js @@ -0,0 +1,47 @@ +var https = require('https'); +var Q = require('q'); + +module.exports = { + analyzeText: function(message) { + var responseData = ''; + var deferred = Q.defer(); + + var postData = JSON.stringify({ + 'language' : 'en', + 'analyzerIds' : ['08ea174b-bfdb-4e64-987e-602f85da7f72'], + 'text' : message.text + }); + + var options = { + host: 'api.projectoxford.ai', + path: '/linguistics/v1.0/analyze', + port: 443, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Content-Length': postData.length, + 'Cache-Control': 'no-cache', + 'Ocp-Apim-Subscription-Key': '02829fac842a458d9fa90242754c6721', //replace with process.env.MSCSToken + } + }; + + var req = https.request(options, (res) => { + res.setEncoding('utf8'); + res.on('data', (chunk) => { + responseData += chunk; + }); + res.on('end', () => { + deferred.resolve(responseData); + }) + }); + + req.on('error', (e) => { + deferred.reject(new Error("problem with request: " + e.message)); + }); + + req.write(postData); + req.end(); + + return deferred.promise; + } +}; \ No newline at end of file