asjon/scripts/wolfram.coffee

33 lines
1013 B
CoffeeScript
Raw Normal View History

2015-04-27 20:34:36 +02:00
# Description:
# accede a wolfram alpha
#
# Dependencies:
# "wolfram":"0.3.1"
#
2015-04-27 20:56:20 +02:00
# Configuration:
# WOLFRAM_API_KEY - self explanatory
#
# Commands
# hubot wolfram/wfa/quanto fa/compute ... - pone la domanda a Wolfram Alpha
#
2015-04-27 20:34:36 +02:00
# Author:
# Enrico Fasoli (fazo96)
#
2015-04-29 15:18:03 +02:00
Wolfram = require 'wolfram'
2015-04-27 20:34:36 +02:00
module.exports = (robot) ->
robot.respond /(?:quanto fa|compute|wfa|wolfram) (.+)/i, (res) ->
2015-04-29 15:18:03 +02:00
unless process.env.WOLFRAM_API_KEY
return res.send 'non ho le chiavi per Wolfram Alpha :('
wolfram = Wolfram.createClient process.env.WOLFRAM_API_KEY
res.send 'Contattando Wolfram Alpha...'
2015-04-27 20:34:36 +02:00
wolfram.query res.match[1], (err, result) ->
if err then return res.send err
2015-04-29 15:18:03 +02:00
unless result?.slice?
return res.send 'invalid response from Wolfram Alpha'
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()