-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server configured to receive wolfram alpha api data
- Loading branch information
0 parents
commit 70da572
Showing
7 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Slack with Wolfram|Alpha integration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
let express = require('express'); | ||
let fs = require('fs'); | ||
let config = require('./config'); | ||
let bodyParser = require('body-parser'); | ||
let morgan = require('morgan'); | ||
let wolframBot = require('./wolframBot'); | ||
|
||
module.exports = function(){ | ||
let app = express(); | ||
let port = process.env.PORT || config.get('port'); | ||
|
||
// body parser middleware | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
app.use(morgan('dev')); | ||
|
||
app.use('/',router()); | ||
|
||
let server = app.listen(port, function () { | ||
console.log('Slack-Wolfram|Alpha listening on port ' + port); | ||
}); | ||
|
||
process.on('SIGINT', function() { | ||
server.close(); | ||
process.exit(); | ||
}); | ||
|
||
function router(){ | ||
var expressRouter = express.Router(); | ||
expressRouter.post('/wolframBot', wolframBot); | ||
return expressRouter; | ||
|
||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require('babel/register')({ | ||
optional: ['runtime', 'es7.asyncFunctions'] | ||
}); | ||
var apiServer = require('./apiServer'); | ||
apiServer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
let nconf = require('nconf'); | ||
nconf.argv() | ||
.env() | ||
.file({ file: './config.json' }); | ||
module.exports = nconf; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "slack-wolfram-alpha", | ||
"version": "1.0.0", | ||
"description": "Slack with Wolfram|Alpha", | ||
"main": "app.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "Jasper Mishra <[email protected]> (https://www.linkedin.com/profile/view?id=168344449)", | ||
"license": "ISC", | ||
"dependencies": { | ||
"babel": "^5.6.14", | ||
"babel-runtime": "^5.6.15", | ||
"bluebird": "^2.9.30", | ||
"body-parser": "^1.13.1", | ||
"express": "^4.13.0", | ||
"lodash": "^3.10.0", | ||
"morgan": "^1.6.0", | ||
"nconf": "^0.7.1", | ||
"xml2js": "^0.4.9" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
let config = require('./config'); | ||
let _ = require('lodash'); | ||
let Promise = require('bluebird'); | ||
let http = require('http'); | ||
let qs = require('querystring'); | ||
let parseString = require('xml2js').parseString; | ||
let wolframParser = require('./wolframParser'); | ||
|
||
module.exports = function(req, res, next){ | ||
let triggerWord = req.body.trigger_word; | ||
|
||
if(config.get('username') !== req.body.user_name){ | ||
return res.status(400).send('bad username').end(); | ||
} | ||
console.log('config triggerWords', config.get('triggerWords').length); | ||
if(_.indexOf(config.get('triggerWords'), triggerWord) === -1){ | ||
return res.status(400).send('bad triggerWord').end(); | ||
} | ||
|
||
let text = req.body.text; | ||
let equation = text.substr(text.indexOf(' ')+1); | ||
let queryString = qs.stringify({ | ||
input:equation, | ||
appid:config.get('appid') | ||
}); | ||
|
||
getWolframRest(queryString).then(response => { | ||
parseString(response, (err, parsedJson) => { | ||
res.status(200).send(parsedJson); | ||
}); | ||
}); | ||
|
||
function getWolframRest(queryString){ | ||
return new Promise((resolve, reject) => { | ||
let options = { | ||
host:config.get('host'), | ||
path:config.get('path')+queryString, | ||
method:'GET' | ||
}; | ||
let wolframReq = http.request(options, wolframRes => { | ||
let str = ''; | ||
wolframRes.on('data', function (chunk) { | ||
str += chunk; | ||
}); | ||
wolframRes.on('error', wolframError => { | ||
reject(error); | ||
}); | ||
|
||
wolframRes.on('end', () => { | ||
resolve(str); | ||
}); | ||
|
||
}); | ||
wolframReq.on('error', function (e) { | ||
reject(e); | ||
}); | ||
wolframReq.end(); | ||
|
||
}); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = function(req, res){ | ||
// let json = req.apiResponse; | ||
// console.log('json -->', json); | ||
// res.status(200).send({a:'a'}); | ||
// res.status(200).send({b:'b'}); | ||
// res.status(200).send({c:'c'}); | ||
// res.end(); | ||
} |