Created
September 4, 2018 13:01
-
-
Save macdonst/d3d1eaa7413bec4a1488e3efeb7de330 to your computer and use it in GitHub Desktop.
MS Azure Function to ask Wolfram Alpha a question
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 fetch = require('node-fetch'); | |
const credentials = require('./credentials'); | |
module.exports = async function(context, req) { | |
if (req.query.searchTerm || (req.body && req.body.searchTerm)) { | |
const searchTerm = req.query.searchTerm || | |
req.body.searchTerm; | |
let url = `http://api.wolframalpha.com/v2/query?input=${searchTerm}&appid=${ | |
credentials.wolfram | |
}`; | |
await fetch(url) | |
.then(res => res.text()) | |
.then(text => { | |
context.res = { | |
status: 200, | |
headers: { 'Content-Type': 'text/plain' }, | |
body: text | |
}; | |
context.done(); | |
}); | |
} else { | |
context.res = { | |
status: 400, | |
body: 'Please pass a name on the query string or in the request body' | |
}; | |
} | |
context.done(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment