2015-04-27 20:34:36 +02:00
|
|
|
# Description:
|
|
|
|
# accede a wolfram alpha
|
|
|
|
#
|
2015-04-27 20:56:20 +02:00
|
|
|
# Configuration:
|
|
|
|
# WOLFRAM_API_KEY - self explanatory
|
|
|
|
#
|
|
|
|
# Commands
|
2015-05-06 20:41:25 +02:00
|
|
|
# hubot wolfram/wfa/quanto fa/calcola/compute ... - pone la domanda a Wolfram Alpha
|
2015-04-27 20:56:20 +02:00
|
|
|
#
|
2015-04-27 20:34:36 +02:00
|
|
|
# Author:
|
|
|
|
# Enrico Fasoli (fazo96)
|
|
|
|
#
|
|
|
|
|
2015-05-06 20:41:25 +02:00
|
|
|
cheerio = require 'cheerio'
|
2015-04-27 20:34:36 +02:00
|
|
|
|
|
|
|
module.exports = (robot) ->
|
2015-05-06 20:41:25 +02:00
|
|
|
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
|
2015-04-29 15:18:03 +02:00
|
|
|
return res.send 'non ho le chiavi per Wolfram Alpha :('
|
2015-05-06 20:41:25 +02:00
|
|
|
|
|
|
|
res.send (res.random wait) + '...'
|
|
|
|
robot.wquery res.match[1], key, (err, result) ->
|
|
|
|
if err then return res.send "c'è qualche problema: " + err
|
|
|
|
|
2015-04-27 20:56:20 +02:00
|
|
|
parseSubPod = (subpod) -> subpod.value or subpod.image
|
2015-04-27 20:34:36 +02:00
|
|
|
parsePod = (pod) ->
|
|
|
|
'\n=== ' + pod.title + '\n' + pod.subpods.map(parseSubPod).join('\n')
|
2015-04-29 15:18:03 +02:00
|
|
|
res.send (parsePod(pod) for pod in result).join('').trim()
|