|
| 1 | +# Description: |
| 2 | +# Teach Hubot to play nice with Ruby via rhubot (https://github.com/minton/rhubot) |
| 3 | +# |
| 4 | +# Dependencies: |
| 5 | +# None |
| 6 | +# |
| 7 | +# Configuration: |
| 8 | +# RHUBOT_PATH - Path to Rhubot |
| 9 | +# HUBOT_URL - URL to Hubot HTTP router - http://192.168.0.0:8080 |
| 10 | +# |
| 11 | +# Commands: |
| 12 | +# rb <cmd> <args> - Runs a Ruby script named <cmd> sending the arguments <args>. |
| 13 | +# |
| 14 | +# Author: |
| 15 | +# @mcminton |
| 16 | + |
| 17 | +sh = require('sh') |
| 18 | +scriptPath = process.env.RHUBOT_SCRIPT_PATH |
| 19 | +routerPost = "/hubot/rhubot" |
| 20 | +httpUrl = process.env.HUBOT_URL + routerPost |
| 21 | + |
| 22 | +module.exports = (robot) -> |
| 23 | + robot.hear /rb\s{1}([^\s]+)\s{1}(.+)/i, (msg) -> |
| 24 | + script = msg.match[1] |
| 25 | + scriptArgs = msg.match[2] |
| 26 | + console.log "Running #{script} #{scriptArgs}" |
| 27 | + data = '' |
| 28 | + spawn = require('child_process').spawn |
| 29 | + proc = spawn "ruby", ["#{scriptPath}/rhubot.rb", httpUrl, msg.message.user.room, script, scriptArgs] |
| 30 | + me = this |
| 31 | + proc.stdout.on 'data', (chunk) -> |
| 32 | + data += chunk.toString() |
| 33 | + proc.stderr.on 'data', (chunk) -> |
| 34 | + msg.send chunk.toString() |
| 35 | + proc.stdout.on 'end', () -> |
| 36 | + msg.send data.toString() |
| 37 | + |
| 38 | + |
| 39 | + robot.router.post routerPost, (req, res) -> |
| 40 | + user = robot.userForId 'broadcast' |
| 41 | + user.room = req.body.room |
| 42 | + user.type = 'groupchat' |
| 43 | + result = req.body.result |
| 44 | + robot.send user, "#{result}" |
| 45 | + res.end "O.o" |
| 46 | + |
0 commit comments