bearychat+hubot搭建chatops机器人

一、nodejs安装

这里使用的是官方二进制包进行的安装,因为yum源(epel源)里带的nodejs版本太老了。官方下载地址:https://nodejs.org/en/download/ 

[root@361way src]# xz -d node-v8.10.0-linux-x64.tar.xz
[root@361way src]# tar xvf node-v8.10.0-linux-x64.tar
[root@361way src]# mv node-v8.10.0-linux-x64 /usr/local/nodejs

然后打开/etc/profile 文件,增加如下内容:

export NODE_HOME=/usr/local/nodejs
export PATH=$NODE_HOME/bin:$PATH

source  /etc/profile ,使当前环境变量生效。可以通过如下命令进行验证:

[root@361way src]# npm -version
5.6.0
[root@361way src]# node  -v
v8.10.0

二、安装hubot

[root@361way home]# useradd www
[root@361way home]# su - www
[www@361way ~]$ mkdir myhubot && cd myhubot
[www@361way myhubot]$ yo hubot
? ==========================================================================
We're constantly looking for ways to make yo better!
May we anonymously report usage statistics to improve the tool over time?
More info: https://github.com/yeoman/insight & http://yeoman.io
========================================================================== Yes
                     _____________________________
                    /                             \
   //\              |      Extracting input for    |
  ////\    _____    |   self-replication process   |
 //////\  /_____\   \                             /
 ======= |[^_/\_]|   /----------------------------
  |   | _|___@@__|__
  +===+/  ///     \_\
   | |_\ /// HUBOT/\\
   |___/\//      /  \\
         \      /   +---+
          \____/    |   |
           | //|    +===+
            \//      |xx|
? Owner admin@361way.com
? Bot name myhubot
? Description A simple helpful robot for your Company
? Bot adapter campfire
   create bin/hubot
   create bin/hubot.cmd
   create Procfile
   create README.md
   create external-scripts.json
   create hubot-scripts.json
   create .gitignore
   create package.json
   create scripts/example.coffee
   create .editorconfig
                     _____________________________
 _____              /                             \
 \    \             |   Self-replication process   |
 |    |    _____    |          complete...         |
 |__\\|   /_____\   \     Good luck with that.    /
   |//+  |[^_/\_]|   /----------------------------
  |   | _|___@@__|__
  +===+/  ///     \_\
   | |_\ /// HUBOT/\\
   |___/\//      /  \\
         \      /   +---+
          \____/    |   |
           | //|    +===+
            \//      |xx|
npm WARN deprecated coffee-script@1.6.3: CoffeeScript on NPM has moved to "coffeescript" (no hyphen)
npm WARN deprecated connect@2.30.2: connect 2.x series is deprecated
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN hubot-help@0.2.2 requires a peer of coffee-script@^1.12.6 but none is installed. You must install peer dependencies yourself.
+ hubot-google-translate@0.2.1
+ hubot-diagnostics@0.0.2
+ hubot-google-images@0.2.7
+ hubot-heroku-keepalive@1.0.3
+ hubot@2.19.0
+ hubot-maps@0.0.3
+ hubot-scripts@2.17.2
+ hubot-pugme@0.1.1
+ hubot-rules@0.1.2
+ hubot-shipit@0.2.1
+ hubot-help@0.2.2
+ hubot-redis-brain@0.0.4
added 151 packages in 16.318s

注:此处建议切换为普通用户安装hubot,因为使用root用户,会有Error: EACCES: permission denied, mkdir '/root/.config'这样的提示,所以上面的操作中,我新建了一个www用户。如果一定要使用root用户,还需要给.config目录修改权限才能搞定。

最后通过安装adapter  hubot-bearychat完成安装,命令如下:

[www@361way myhubot]$ npm install hubot-bearychat --save

三、hubot shell模块的安装使用

[www@361way myhubot]$ npm install hubot-script-shellcmd
+ hubot-script-shellcmd@0.0.17
added 1 package in 3.152s
[www@361way myhubot]$ cp -R node_modules/hubot-script-shellcmd/bash ./

修改一下external-scripts.json,添加模块:hubot-script-shellcmd

[root@361way myhubot]# cat external-scripts.json
[
  "hubot-diagnostics",
  "hubot-help",
  "hubot-heroku-keepalive",
  "hubot-google-images",
  "hubot-google-translate",
  "hubot-pugme",
  "hubot-maps",
  "hubot-redis-brain",
  "hubot-rules",
  "hubot-shipit",
  "hubot-script-shellcmd"  //新增部分
]
[root@361way myhubot]# cd bash/handlers/
[root@361way handlers]# ls
helloworld  update
新增脚本
[root@361way handlers]# cat disk
#!/bin/bash
ansible $1 -m shell -a 'df -hP'
[root@361way handlers]# chmod +x disk 

四、添加bearychat机器人

打开https://www.bearychat.com 并注册一个帐号。找到机器人管理 ----  添加自定义机器人 ----- 并找到hubot,如下:

hubot

点开hubot项后,会进入如下的配置界面:

bearychat-hubot

上面的hubot  url里的端口号是可以随意定义的。

五、启动hubot

复制Hubot Token,执行下面的命令启动hubot机器人:

[www@361way myhubot]# export HUBOT_BEARYCHAT_TOKENS=18ff191266f7bfc210d7743ab6108fdd
[www@361way myhubot]# export HUBOT_BEARYCHAT_MODE=rtm
[www@361way myhubot]# export EXPRESS_PORT=10090
[www@361way myhubot]# nohup ./bin/hubot -a bearychat 2&1> hubot.log
[www@361way myhubot]# netstat -tnlp|grep 10090
tcp        0      0 0.0.0.0:10090            0.0.0.0:*               LISTEN  

注意,此处token如果有多个可以使用逗号分隔。为了以后启动方便可以直接将上面的变量信息写入当前用户的环境变量:

$ tail -3 .bash_profile
export HUBOT_BEARYCHAT_TOKENS=18ff191266f7bfc210d7743ab6108fdd
export HUBOT_BEARYCHAT_MODE=rtm
export EXPRESS_PORT=10090

六、chatops测试

下载bearychat客户端或者直接使用网页版,找到刚刚的机器人,发送消息进行测试,如下:

chatops

同时也可以健入help或者其他指今返回可用的帮助信息,由于help返回的信息过多,就不截图了,这里只截图下shellcmd返回的可用模块:

hubot-help

这里只是简单的脚本调用,先不涉及CoffeeScript的编写吧。




本站的发展离不开您的资助,金额随意,欢迎来赏!

  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.