0

Query Wolfram Alpha engine

Function JavaScript 1 revisions 111 a year ago a year ago
async function wolframAlpha(query) {
	const parser = new DomParser();

	return (fetch('https://api.wolframalpha.com/v2/query?input=' 
			+ encodeURIComponent(query) 
			+ '&appid='
			+ encodeURIComponent(process.env.WOLFRAM_API_KEY))
		.then(response => response.text())
		.then(html => {
			const parsed = parser.parseFromString(html);
			return parsed.getElementsByTagName('plaintext')
						.map(e => e.innerHTML);
		}));
}

This function queries Wolfram Alpha API and returns an array of strings returned by the API

Note that this function ignores any returned images and other non-textual stuff.

It assumes that you have registered your application, got its token and set it in process.env.WOLFRAM_API_KEY. The function also depends on dom-parser:

const DomParser = require('dom-parser');

It also depends on the Fetch API, which is readily available in current browsers, you might however need to switch to some other fetching library if you're using it on NodeJS. You can also just use node-fetch