Skip to content

Instantly share code, notes, and snippets.

@sunnygleason
Created December 19, 2016 16:05
Show Gist options
  • Save sunnygleason/e27def6856b7bb0c5bc58f762acd6060 to your computer and use it in GitHub Desktop.
Save sunnygleason/e27def6856b7bb0c5bc58f762acd6060 to your computer and use it in GitHub Desktop.
PubNub Q&A BLOCK w/ Wolfram Alpha API
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