Hubotで外部コマンドを実行する
Node.jsで外部コマンドを実行するために、Child Processを利用する。
Child Process | Node.js v6.5.0 Documentation
const exec = require('child_process').exec;
exec('cat *.js bad_file | wc -l', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});Hubotの例
child_process = require 'child_process'
module.exports = (robot) ->
robot.hear /ruby/i, (res) ->
child_process.exec "ruby -v", (error, stdout, stderr) ->
if !error
output = stdout+''
res.send output
else
res.send 'error'