LoginSignup
12
13

More than 5 years have passed since last update.

hubotからcliを叩く方法

Last updated at Posted at 2014-02-18

↓こんな感じのscriptをhubotに叩かせて結果を表示する方法を示します。

php cli/talk.php -a hello
hello

node.jsのspawnを用いて

scripts/cli.coffee
spawn = require('child_process').spawn

module.exports = (robot) ->
  robot.respond /do-talk$/i, (msg) ->
    cli_talk = spawn('php', ['cli/talk.php', '-a', 'hello'])
    cli_talk.stdout.on 'data', (data) ->
      msg.send data
    cli_talk.stderr.on 'data', (data) ->
      console.log('cli_talk stderr: ' + data)

こんな感じで定義してあげれば

Hubot> hubot do-talk
Hubot> hello

このように結果を表示してくれます。
同様にして外部のscriptを叩けるので複雑な処理はcliにしてしまえば「coffeeはちょっと…」という方にもオススメです。

参考
Node.jsのspawnをつかってcoffescriptでps ax | grep sshを実行させるサンプル
Node.js Child Processes

12
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
13