Hubot是Github开发并开源的chatbot,将其部署到运维环境中后,可通过编写自定义的脚本,将之前的web端、shell命令等人工进行的运维工作,通过聊天工具交给机器人来代替。
搭建hubot本地开发环境
安装Node.js:
下载Node.js LTS的Binary版本:https://nodejs.org/en/download/current/。将binary压缩包解压缩到安装目录NODE_INSTALL_PATH中。 设置环境变量NODE_HOME指向到解压缩后的目录,并设置PATH环境变量包含NODE_HOME。
在NODE_INSTALL_PATH中创建一个npm_cache目录,使用下面的命令将npm_cache的路径变更到这个目录中:
npm config set cache NODE_INSTALL_PATH/npm_cache --globalnode -v
v10.14.1
npm -v
6.4.1使用npm安装hubot generator生成我们自己本地的hubot:
npm install -g yo generator-hubotmkdir myhubot
cd myhubot
yo hubot接下来会是交互的初始化hubot,这里略过。
Hubot需要使用redis来持久化数据,这里是本地开发环境,编辑external-scripts.json删除hubot-redis-brain,在本地不使用redis。
删除external-scripts.json中的hubot-heroku-keepalive。
执行bin/hubot进入:
bin/hubot
wing> help
usage:
history
exit, \q - close shell and exit
help, \? - print this usage
clear, \c - clear the terminal screenhu编写第一个hubot脚本
hubot使用CoffeScript开发自定义机器人脚本,先查看一下本地myhubot的目录结构:
bin/ #hubot运行脚本
node_modules/ #引用的包文件
scripts/ #自定义机器人脚本
external-scripts.json #引用的外部脚本
package.json #项目全局配置信息module.exports = (robot) ->
robot.hear /hello/i, (res) ->
res.send "hello everyone."
robot.respond /hi/i, (res) ->
res.reply "hi everyone."hear和respond的区别,hear监听任何消息,respond只监听群组中发送给机器人的消息,即需要指定机器人名称,参见下面的触发脚本的方式。
send和reply的区别,send会将消息发送到群组中,而reply会将消息回复给具体的人,如@。
wing> hello
wing> hello everyone.
wing> wing hi
wing> hi everyone.