hubot 初探
hubot 是什么?
Hubot is your friendly robot sidekick. Install him in your company to dramatically improve employee efficiency.
更多介绍,请到官网,这里就不做翻译了 https://hubot.github.com/
如何搭建hubot服务?
1. 安装实例化 hubot 工厂。
依赖 npm ,这里默认你已经安装好 npm环境。这步相当于修建了一个制造机器人的工厂。
npm install -g yo generator-hubot
2. 配置命令
把安装完生成的 yo 通过软连接方式放进已经配置好的环境变量目录,方便后续可以直接调动 yo 命令。如果所运行环境没有添加 /usr/local/bin/ 到环境变量,可以把 yo 放入 /usr/bin 目录
ln -s /usr/local/node/bin/yo /usr/local/bin/yo ln -s /usr/local/node/bin/yo-complete /usr/local/bin/yo-complete
3. 实例化一个机器人。
这个机器工厂可以生产适配各个环境运行的机器人(Adapter),目前有这么几种,官方提供:Shell、Campfire,其他流行的几种分别为:Gitter、HipChat、IRC、Rocket.Chat、Slack、XMPP。还有更多,这里附上 github 地址 https://github.com/search?q=topic%3Ahubot-adapter&type=Repositories 。
下面用默认Adapter(campfire )做一些展示,当然在国内还是wechat 更流行,上面的搜索结果我已经看到了适配 wechat 的 Adapter 了,抽时间再试试效果。接下来的步骤建议使用非root权限操作,否则会提示config目录/文件权限问题,我这里切换为普通账户,错误信息类似
/data/nodejs/lib/node_modules/yo/node_modules/mkdirp/index.js:90 throw err0; ^ Error: EACCES: permission denied, mkdir '/root/.config' at Error (native) at Object.fs.mkdirSync (fs.js:923:18) at sync (/data/nodejs/lib/node_modules/yo/node_modules/mkdirp/index.js:71:13) at Function.sync (/data/nodejs/lib/node_modules/yo/node_modules/mkdirp/index.js:77:24) at Object.get (/data/nodejs/lib/node_modules/yo/node_modules/configstore/index.js:38:13) at Object.Configstore (/data/nodejs/lib/node_modules/yo/node_modules/configstore/index.js:27:44) at new Insight (/data/nodejs/lib/node_modules/yo/node_modules/insight/lib/index.js:37:34) at Object.<anonymous> (/data/nodejs/lib/node_modules/yo/lib/cli.js:172:11) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10)
3.1 新建目录,初始化 hubot (campfire Adapter)
mkdir myhubot3 cd myhubot3/ yo hubot这一步有些交互口令需要输入,第一个输入 Yes,其他步骤可以直接回车略过,同时我们可以看到系统预制好的默认值,当然也可以按提示信息,输入更加合适的内容。这里列出所有提示信息:
1. May we anonymously report usage statistics to improve the tool over time? 2. Owner 3. Bot name 4. Description 5. Bot adapter
3.2 启动 hubot
bin/hubot
有可能会有些报错信息,不过正常情况是可以直接回车略过,这里罗列一些简单的预制命令:
myhubot3> myhubot3 ping myhubot3> PONG myhubot3> myhubot3 animate me myhubot3> Google Image Search API is not longer available. Please [setup up Custom Search Engine API](https://github.com/hubot-scripts/hubot-google-images#cse-setup-details). http://i.imgur.com/CzFTOkI.png myhubot3> myhubot3 help myhubot3> Shell: myhubot3 adapter - Reply with the adapter myhubot3 animate me <query> - The same thing as `image me`, except adds a few parameters to try to return an animated GIF instead. myhubot3 echo <text> - Reply back with <text> myhubot3 help - Displays all of the help commands that Hubot knows about. myhubot3 help <query> - Displays all help commands that match <query>. myhubot3 image me <query> - The Original. Queries Google Images for <query> and returns a random top result. myhubot3 map me <query> - Returns a map view of the area returned by `query`. myhubot3 mustache me <url|query> - Adds a mustache to the specified URL or query result. myhubot3 ping - Reply with pong myhubot3 pug bomb N - get N pugs myhubot3 pug me - Receive a pug myhubot3 shellcmd - list (bash)shell commands myhubot3 shellcmd <foo> - performs bashshell command myhubot3 the rules - Make sure hubot still knows the rules. myhubot3 time - Reply with current time myhubot3 translate me <phrase> - Searches for a translation for the <phrase> and then prints that bad boy out. myhubot3 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 myhubot3>
4. 让 hubot 执行 shell 、PHP 脚本
可以使用nodejs 来开发机器人执行的口令,也可以借助已经实现的 shellcmd 扩展来自定义一些口令
npm install hubot-script-shellcmd cp -R node_modules/hubot-script-shellcmd/bash ./
修改一下external-scripts.json,添加上以下模块:hubot-script-shellcmd。如果到此为止,你操作的步骤和我基本一样的话,你的external-scripts.json应该长的像这个样子:
[ "hubot-diagnostics", "hubot-help", "hubot-google-images", "hubot-google-translate", "hubot-pugme", "hubot-maps", "hubot-rules", "hubot-shipit", "hubot-script-shellcmd" ]我们在 bash/handlers 目录下新建 2个文件,分别为 cpu 、 eat ,内容分别如下:
#!/bin/bash top -b -n2 -p 1 | fgrep "Cpu(s)" | tail -1 | awk -F'id,' -v prefix="$prefix" '{ split($1, vs, ","); v=vs[length(vs)]; sub("%", "", v); printf "%s%.1f%%\n", prefix, 100 - v }' exit 0
#!/usr/local/bin/php <?php echo "Hello.."; $input = array( '玫瑰花园自助烤肉(青年路店)', '又见成都', 'Miss火新派重庆火锅', '大董', '京味缘烤鸭', '妈妈的味道', '七号鲜', '花牛餐馆', '金手勺', '达美B1食堂', '大鸭梨(石佛营店)', ); $rand_keys = array_rand($input, 1); echo $input[$rand_keys]; ?>这时我们分别使用 shell 与 PHP 实现了一个查看cpu使用情况与选择去哪吃饭的功能。接下来让我们体验一下吧:
myhubot3> myhubot3 shellcmd myhubot3> spawn! available commands: cpu eat helloworld update myhubot3> myhubot3 shellcmd cpu myhubot3> spawn! 5.0% myhubot3> myhubot3 shellcmd eat myhubot3> spawn! Hello..达美B1食堂 myhubot3>
相关内容:
hubot + slack 结合
hubot + wechat 结合
参考内容:
http://www.jianshu.com/p/ca8d3f9ba143
https://segmentfault.com/a/1190000006681056
https://hubot.github.com/