hubot 설치, 스크립팅, 외부 명령 실행

17115 단어 Hubot
RaspberryPi1 B+로 동작 확인

참고


  • Hubot을 설치하고 HipChat과 협력하기 - Qiita

  • 설치


    # sudo apt-get install -y redis-server
    sudo npm install -g npm nodejs 
    sudo npm install -g coffee-script yo generator-hubot
    mkdir k
    cd k
    yo hubot --defaults
    

    external-scripts.json
     [
     ...
    -   "hubot-heroku-keepalive",
    -   "hubot-redis-brain",
     ...
     ]
    

    비어 있으므로 삭제
    rm hubot-scripts.json
    

    시작



    시작
    bin/hubot
    

    콘솔
    k> k ping
    k> PONG
    
    k> k help
    k> Shell: k adapter - Reply with the adapter
    k animate me <query> - The same thing as `image me`, except adds a few parameters to try to return an animated GIF instead.
    k echo <text> - Reply back with <text>
    k help - Displays all of the help commands that Hubot knows about.
    k help <query> - Displays all help commands that match <query>.
    k image me <query> - The Original. Queries Google Images for <query> and returns a random top result.
    k map me <query> - Returns a map view of the area returned by `query`.
    k mustache me <url|query> - Adds a mustache to the specified URL or query result.
    k ping - Reply with pong
    k pug bomb N - get N pugs
    k pug me - Receive a pug
    k the rules - Make sure hubot still knows the rules.
    k time - Reply with current time
    k translate me <phrase> - Searches for a translation for the <phrase> and then prints that bad boy out.
    k translate me from <source> into <target> <phrase> - Translates <phrase> from <source> into <target>. Both <source> and <target> are optional
    ship it - Display a motivation squirrel
    

    PING 응답을 변경해 봅니다.



    ~.coffee is using deprecated documentation syntax
  • Hubot 시작 시 "INFO [script 경로] is using depercated documentation syntax"가 출력되는 이유와 대응 - Qiita

  • Description은 필요하다고.

    scripts/ping.coffee
    # Description:
    #   Example scripts for you to examine and try out.
    
    module.exports = (robot) ->
      robot.respond /PING$/i, (res) ->
        res.reply "何ですか?"
    
    bin/hubot
    
    k> k ping
    k> Shell: 何ですか?
    PONG
    

    외부 명령 실행




  • hubot-scripts/shell-command.coffee at master · sapg/hubot-scripts · GitHub
  • # Description:
    # Commands:
    #  hubot ping <hostname>
    
    module.exports = (robot) ->
     robot.respond /ping (.*)$/i, (msg) ->
        hostname = msg.match[1]
        @exec = require('child_process').exec
        command = "ping -c5 #{hostname}"
        msg.send command
    
        @exec command, (error, stdout, stderr) ->
          #msg.send error
          msg.send stdout
          #msg.send stderr
    
    k> k ping 8.8.8.8
    
    ping -c5 8.8.8.8
    PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
    64 bytes from 8.8.8.8: icmp_seq=1 ttl=54 time=5.12 ms
    64 bytes from 8.8.8.8: icmp_seq=2 ttl=54 time=4.83 ms
    64 bytes from 8.8.8.8: icmp_seq=3 ttl=54 time=3.07 ms
    64 bytes from 8.8.8.8: icmp_seq=4 ttl=54 time=4.05 ms
    64 bytes from 8.8.8.8: icmp_seq=5 ttl=54 time=5.26 ms
    
    --- 8.8.8.8 ping statistics ---
    5 packets transmitted, 5 received, 0% packet loss, time 4005ms
    rtt min/avg/max/mdev = 3.074/4.472/5.263/0.814 ms
    

    날씨(curl)


  • 영어와 일본 영어 번역과 오늘 내일 명후일의 날씨를 터미널에서 조사하는 편리 커맨드 소개 - Qiita



  • 英和/和英(trans)


  • Translate Shell
  • sudo apt-get install -y gawk
    wget git.io/trans
    chmod +x ./trans
    sudo mv trans /usr/local/bin/
    

    trans 명령의 동작 확인
    $ trans :ja binary
    $ trans :en 変える
    

    scripts/trans.coffee
    # Description:
    # Commands:
    #  hubot trans :(ja|en) <word>
    
    module.exports = (robot) ->
     robot.respond /trans :(ja|en) (.*)$/i, (msg) ->
        lang = msg.match[1]
        word = msg.match[2]
        @exec = require('child_process').exec
        command = "trans :#{lang} #{word}"
        msg.send command
    
        @exec command, (error, stdout, stderr) ->
          #msg.send error
          msg.send stdout
          #msg.send stderr
    

    trans 명령의 동작 확인
    k> @k trans :ja binary
    

    wget



    scripts/wget.coffee
    # Description:
    # Commands:
    #  hubot wget <url>
    
    module.exports = (robot) ->
     robot.respond /wget (.*)$/i, (msg) ->
        url = msg.match[1]
        @exec = require('child_process').exec
        command = "wget -P ./Downloads/ #{url}"
        msg.send command
    
        @exec command, (error, stdout, stderr) ->
          msg.send stdout
    

    준비
    mkdir Downloads
    

    동작 확인
    k wget https://pbs.twimg.com/media/Civ07I7UgAAXLOC.jpg
    

    ls



    scripts/ls.coffee
    # Description:
    # Commands:
    #  hubot ls
    
    module.exports = (robot) ->
     robot.respond /ls$/i, (msg) ->
        @exec = require('child_process').exec
        command = "ls -lh ./Downloads/"
        msg.send command
    
        @exec command, (error, stdout, stderr) ->
          #msg.send error
          msg.send stdout
          #msg.send stderr
    

    youtube-dl



    scripts/
    scripts/youtube-dl.coffee 
    # Description:
    # Commands:
    #  hubot youtube-dl <url>
    
    module.exports = (robot) ->
     robot.respond /youtube-dl (.*)$/i, (msg) ->
        url = msg.match[1]
        @exec = require('child_process').exec
        command = "(cd ./Downloads/ && youtube-dl #{url})"
        msg.send command
    
        @exec command, (error, stdout, stderr) ->
          msg.send stdout
    

    hubot의 이름을 바꾸고 싶다면


  • myhubot을 k로 변경 예
  • sed -i -e 's/myhubot/k/g' package.json 
    sed -i -e 's/myhubot/k/g' README.md   
    sed -i -e 's/myhubot/k/g' bin/hubot
    sed -i -e 's/myhubot/k/g' bin/hubot.cmd 
    

    좋은 웹페이지 즐겨찾기