# Description: # accede a wolfram alpha # # Configuration: # WOLFRAM_API_KEY - self explanatory # # Commands # hubot wolfram/wfa/quanto fa/calcola/compute ... - pone la domanda a Wolfram Alpha # # Author: # Enrico Fasoli (fazo96) # cheerio = require 'cheerio' module.exports = (robot) -> robot.wquery = (input, key, callback) -> q = encodeURIComponent input url = 'http://api.wolframalpha.com/v2/query?input=' + q + '&appid=' + key robot.http(url).get() (err, res, body) -> if err then return callback err, {} $ = cheerio.load body, xmlMode: true if $('queryresult').attr('error') == 'true' return callback $('error').find('msg').text(), {} pods = ($('pod').map (_, pod) -> title: $(pod).attr 'title' primary: $(pod).attr('primary')? subpods: ($('subpod', $ pod).map (_, subpod) -> title: $(subpod).attr 'title' value: $('plaintext', $ subpod).text() image: $('img', $ subpod).attr 'src' ).get() ).get() if pods.length callback null, pods else callback 'la risposta da wolfram non è valida', [] robot.respond /(?:calcola|quanto fa|compute|wfa|wolfram) (.+)/i, (res) -> wait = ['chiedo a wolfy', 'dammi un attimo', 'sto pensando', 'fammi fare due conti', 'chiedo a wolfram'] key = process.env.WOLFRAM_API_KEY if not key return res.send 'non ho le chiavi per Wolfram Alpha :(' res.send (res.random wait) + '...' robot.wquery res.match[1], key, (err, result) -> if err then return res.send "c'è qualche problema: " + err parseSubPod = (subpod) -> subpod.value or subpod.image parsePod = (pod) -> '\n=== ' + pod.title + '\n' + pod.subpods.map(parseSubPod).join('\n') res.send (parsePod(pod) for pod in result).join('').trim()