Rewrite wolfram script to use no deps

This commit is contained in:
rnhmjoj 2015-05-06 20:41:25 +02:00
parent d80a53089e
commit b4b0feb203

View File

@ -1,31 +1,55 @@
# Description: # Description:
# accede a wolfram alpha # accede a wolfram alpha
# #
# Dependencies:
# "wolfram":"0.3.1"
#
# Configuration: # Configuration:
# WOLFRAM_API_KEY - self explanatory # WOLFRAM_API_KEY - self explanatory
# #
# Commands # Commands
# hubot wolfram/wfa/quanto fa/compute ... - pone la domanda a Wolfram Alpha # hubot wolfram/wfa/quanto fa/calcola/compute ... - pone la domanda a Wolfram Alpha
# #
# Author: # Author:
# Enrico Fasoli (fazo96) # Enrico Fasoli (fazo96)
# #
Wolfram = require 'wolfram' cheerio = require 'cheerio'
module.exports = (robot) -> module.exports = (robot) ->
robot.respond /(?:quanto fa|compute|wfa|wolfram) (.+)/i, (res) -> robot.wquery = (input, key, callback) ->
unless process.env.WOLFRAM_API_KEY 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 :(' return res.send 'non ho le chiavi per Wolfram Alpha :('
wolfram = Wolfram.createClient process.env.WOLFRAM_API_KEY
res.send 'Contattando Wolfram Alpha...' res.send (res.random wait) + '...'
wolfram.query res.match[1], (err, result) -> robot.wquery res.match[1], key, (err, result) ->
if err then return res.send err if err then return res.send "c'è qualche problema: " + err
unless result?.slice?
return res.send 'invalid response from Wolfram Alpha'
parseSubPod = (subpod) -> subpod.value or subpod.image parseSubPod = (subpod) -> subpod.value or subpod.image
parsePod = (pod) -> parsePod = (pod) ->
'\n=== ' + pod.title + '\n' + pod.subpods.map(parseSubPod).join('\n') '\n=== ' + pod.title + '\n' + pod.subpods.map(parseSubPod).join('\n')