Created
December 19, 2016 16:05
-
-
Save sunnygleason/e27def6856b7bb0c5bc58f762acd6060 to your computer and use it in GitHub Desktop.
PubNub Q&A BLOCK w/ Wolfram Alpha API
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
const xhr = require('xhr'); | |
const query = require('codec/query_string'); | |
export default (request) => { | |
const appId = 'YOUR_WOLFRAM_ALPHA_APP_ID'; | |
const spoken = 'http://www.wolframalpha.com/api/v1/spoken-json.jsp'; | |
const queryParams = { | |
appid: appId, | |
input: request.message.text | |
}; | |
const apiUrl = spoken + '?' + query.stringify(queryParams); | |
return xhr.fetch(apiUrl) | |
.then((r) => { | |
const body = JSON.parse(r.body || r); | |
request.message.answer = body.result || body.error; | |
return request.ok(); | |
}) | |
.catch((e) => { | |
console.error(e); | |
return request.ok(); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment