ChatOps工具之Hubot

来源:互联网 发布:二手笔记本电脑淘宝 编辑:程序博客网 时间:2024/06/14 03:32

这里写图片描述

Hubot简介

Hubot的最初的一个版本是用于作为Github的chat room, 当然不只是一个聊天机器人,使用Hubot可以用于自动化集成,实施部署,搞点乐子等等,在Github他们就是这样做的,Hubot被称为Github最忙碌的员工。而后又使用Coffeescript对项目进行了重写,在Github上也受到了广泛的好评。

使用场景

使用Hubot可以用于很多场景,其中很多都已经是在被实践之中了

  • 预定内部会议室
  • 通知代办事项
  • 更新系统补丁
  • 检查系统状态
  • 部署应用程序

当然这些需要自己写脚本或者模块来实现,而这些根DevOps的实践也非常的相近。其实Hubot聊天机器人作为ChatOps的一个实现,除了Hubot之外还有收费的hipchat等,而ChatOps本身也是受DevOps的触动而产生的一个概念。当然除了这些正经的场景还有一些其他的场景诸如:

  • 咨询附近的美食
  • 工作累了让Hubot讲个笑话
  • 问Hubot自己长得是不是很帅

不要妄图去断定我们程序员都是怎么想的,你猜来猜去也猜不明白。我只能说,这些也非常之重要,虽然很无聊,人生不就是图一乐和麽。

ChatOps工具

除了Hubot,ChatOps常用的工具还有诸如

  • Slack
  • HipChat
  • Gitter

等等,在此不再一一列举。

依赖

前面已经提到现在的Hubot已经是使用CoffeeScript改写过的,所以自然其运行环境的依赖如下

项番 依赖组件 No.1 Node.js No.2 Npm

支持的平台

Hubot缺省支持Heroku,但并非仅限于此,目前Hubot支持部署的平台如下

  • Windows
  • Unix
  • Heroku
  • Azure
  • BlueMix

其他依赖

除了Node.js和Npm以外,由于Hubot使用Redis报存其持久层数据,所以Redis也是需要安装的,但是仅仅是体验功能的话,Redis不安装也可以。

安装Node & Npm

安装地址如下,

项目 详细信息 地址 https://nodejs.org/en/download/ 下载地址 https://nodejs.org/dist/v6.10.3/node-v6.10.3-linux-x64.tar.xz Nodejs版本 6.10.3 Npm版本 3.10.10

安装命令

mkdir -p /usr/local/nodejscd /usr/local/nodejswget https://nodejs.org/dist/v6.10.3/node-v6.10.3-linux-x64.tar.xztar node-v6.10.3-linux-x64.tar.xz

安装日志

[root@liumiaocn ~]# mkdir -p /usr/local/nodejs[root@liumiaocn ~]# cd /usr/local/nodejs[root@liumiaocn nodejs]# wget https://nodejs.org/dist/v6.10.3/node-v6.10.3-linux-x64.tar.xz--2017-05-30 04:58:11--  https://nodejs.org/dist/v6.10.3/node-v6.10.3-linux-x64.tar.xzResolving nodejs.org (nodejs.org)... 104.20.22.46, 104.20.23.46, 2400:cb00:2048:1::6814:162e, ...Connecting to nodejs.org (nodejs.org)|104.20.22.46|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 9366220 (8.9M) [application/x-xz]Saving to: ‘node-v6.10.3-linux-x64.tar.xz’100%[=========================================================================================================>] 9,366,220    278KB/s   in 35s2017-05-30 04:58:48 (258 KB/s) - ‘node-v6.10.3-linux-x64.tar.xz’ saved [9366220/9366220][root@liumiaocn nodejs]#tar xvpf node-v6.10.3-linux-x64.tar.xz...node-v6.10.3-linux-x64/lib/node_modules/npm/node_modules/umask/index.jsnode-v6.10.3-linux-x64/lib/node_modules/npm/node_modules/umask/README.mdnode-v6.10.3-linux-x64/lib/node_modules/npm/node_modules/umask/.npmignore[root@liumiaocn nodejs]#

设置环境变量

[root@liumiaocn bin]#[root@liumiaocn bin]# export PATH=$PATH:/usr/local/nodejs/node-v6.10.3-linux-x64/bin[root@liumiaocn bin]#

这样设定的环境变量需要每次都设定,如果需要长久设定的话,请根据具体情况设定到/etc/profile或者.bashrc或.profile中。

确认版本

[root@liumiaocn ~]# node --versionv6.10.3[root@liumiaocn ~]# npm --version3.10.10[root@liumiaocn ~]#

Hubot Generator

安装Hubot Generator,之前我们在使用Racher的时候的自定义模板时也有使用类似的功能,其实就是yoman,提供一些简化我们使用方式。

安装命令

# npm install -g yo generator-hubot

创建一个聊天机器人

使用刚刚安装的Hubot Generator创建一个名为lili的聊天机器人,只需要注意一点,请用普通用户进行创建。

这里写图片描述

安装结果确认

安装之后,确认刚刚使用yoman创建的名为lili的聊天机器人,以下是详细信息:

[admin@liumiaocn myhubot]$ pwd/home/admin/myhubot[admin@liumiaocn myhubot]$ lsbin  external-scripts.json  hubot-scripts.json  node_modules  package.json  Procfile  README.md  scripts[admin@liumiaocn myhubot]$ cd bin[admin@liumiaocn bin]$ lshubot  hubot.cmd[admin@liumiaocn bin]$

执行确认

[admin@liumiaocn myhubot]$ pwd/home/admin/myhubot[admin@liumiaocn myhubot]$ bin/hubotlili> [Tue May 30 2017 05:47:15 GMT-0400 (EDT)] WARNING Loading scripts from hubot-scripts.json is deprecated and will be removed in 3.0 (https://github.com/github/hubot-scripts/issues/1113) in favor of packages for each script.our hubot-scripts.json is empty, so you just need to remove it.[Tue May 30 2017 05:47:15 GMT-0400 (EDT)] ERROR hubot-heroku-alive included, but missing HUBOT_HEROKU_KEEPALIVE_URL. `heroku config:set HUBOT_HEROKU_KEEPALIVE_URL=$(heroku apps:info -s  | grep web-url | cut -d= -f2)`[Tue May 30 2017 05:47:15 GMT-0400 (EDT)] INlt redis on localhost:6379lili>

可以看到其提示的有ERROR的信息,前面也有提到,Hubot缺省支持Heroku,并将Redis作为其持久方案,如果不希望使用Redis的话,可以通过直接删除如下文件中的hubot-heroku-keepalive和hubot-redis-brain即可。

[admin@liumiaocn 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"][admin@liumiaocn myhubot]$

hubot-scripts.json内容为空,所以也出了一个WARNING,不想看到的话,删除即可。

[admin@liumiaocn myhubot]$ cat hubot-scripts.json[][admin@liumiaocn myhubot]$

结果确认

[admin@liumiaocn myhubot]$ bin/hubotlili>

可以看到聊天机器人lili已经启动了,接下来可以看看lili能做什么了,使用lili help可以看到其能做的所有事情。

[admin@liumiaocn myhubot]$ bin/hubotlili> lili helplili> Shell: lili adapter - Reply with the adapterlili animate me <query> - The same thing as `image me`, except adds a few parameters to try to return an animated GIF instead.lili echo <text> - Reply back with <text>lili help - Displays all of the help commands that Hubot knows about.lili help <query> - Displays all help commands that match <query>.lili image me <query> - The Original. Queries Google Images for <query> and returns a random top result.lili map me <query> - Returns a map view of the area returned by `query`.lili mustache me <url|query> - Adds a mustache to the specified URL or query result.lili ping - Reply with ponglili pug bomb N - get N pugslili pug me - Receive a puglili the rules - Make sure hubot still knows the rules.lili time - Reply with current timelili translate me <phrase> - Searches for a translation for the <phrase> and then prints that bad boy out.lili translate me from <source> into <target> <phrase> - Translates <phrase> from <source> into <target>. Both <source> and <target> are optionalship it - Display a motivation squirrel

使用lili ping,她会回答pong

lili> lili pinglili> PONGlili>

问她规矩,她会背诵机器人三原则

lili> lili the ruleslili> 0. A robot may not harm humanity, or, by inaction, allow humanity to come to harm.1. A robot may not injure a human being or, through inaction, allow a human being to come to harm.2. A robot must obey any orders given to it by human beings, except where such orders would conflict with the First Law.3. A robot must protect its own existence as long as such protection does not conflict with the First or Second Law.lili>

唯一一条看起来有用的命令,是询问时间

lili> lili timelili> Server time is: Tue May 30 2017 06:51:28 GMT-0400 (EDT)lili>

总结

通过Hubot Generator就可以轻松创建lili这样一个聊天机器人,但是目前看起来她还完全不会聊天。唯一看似乎有用的time命令似乎还不如自己直接敲date,但是这只是一个例子,我们可以自己写自己的scripts以满足自己的实际的需求。而这些类似的Scripts已经有了一些,而Hubot的本意则正是希望能够创建一个生态环境,一个大体根Ansible的galaxy或者docker的dockerhub类似程度的生态圈吧。通过ChatOps方式的整合,是不是也可以将语音识别/自动运维/人工智能等不断的集成进取呢,真正社区的推动才会使得Hubot真正的发展,是否会有一个完整链条的生态圈出现目前还无法断言,但不妨拭目以待.

原创粉丝点击