如何使用Hubot丰富您的松弛频道

Hubot是开源的CoffeeScript机器人,可以通过各种聊天室应用程序监视和响应命令。 正如GitHub自豪地宣称的那样,“它可以帮助极大地提高和降低员工效率”(取决于您的使用方式)。 在本文中,我们将设置Hubot,并将其与Slack集成,并展示我们可以从劳动成果中获得的提高和降低的效率。

Hubot可以做什么?

Hubot的唯一限制是您自己的想象力。 但是,重要的是要意识到,尽管您可以使用Hubot进行几乎任何事情,但是更大的问题变成了,对吗? 从发布图像,翻译语言,发送即将到来的生日提醒,到报告团队的最新活动。 Hubot通过一个非常健壮的消息系统和适当放置的钩子具有多种功能。

启动并运行

因此,让我们启动并运行Hubot。 为此,我们需要安装Node.js以及npm(Node的程序包管理器)。 如果不确定如何执行此操作,请查看我们的教程: npm入门指南

接下来,全局安装以下软件包:

  • hubot -Hubot框架
  • coffee-script — Hubot的脚本使用CoffeeScript编写
  • yo —用于运行Yeoman发电机的CLI工具
  • generator-hubot —一个Yeoman生成器,用于使用Hubot框架创建自己的聊天机器人

您可以使用以下命令执行此操作:

npm install -g hubot coffee-script yo generator-hubot

然后,我们需要创建一个目录来容纳我们的新朋友并运行上述的Yeoman生成器。

mkdir test-bot && cd test-bot
yo hubot

如果一切顺利,Hubot将会弹出并询问您一些问题。 输入您的姓名,新机器人的名称和描述。 您可以将适配器保留为篝火。 在退出之前,它将创建一堆文件。

正在运行Hubot生成器的屏幕截图

现在运行bin/hubot来启动您的新机器人。

试运行

要查看您的新机器人可以做什么,请运行:

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

哇! 好的,让我们尝试其中之一:

test-bot translate me from German into English Rindfleischetikettierungsüberwachungsaufgabenübertragungsgesetz

产生:

test-bot> The German "Rindfleischetikettierungsüberwachungsaufgabenübertragungsgesetz" translates as "Beef labeling monitoring delegation law" in English

太棒了!

因此Hubot可以工作。 在继续下一步之前,可以随意玩一些。

将Hubot与Slack集成

Slack是一个团队协作工具,它提供持久的聊天室,以及私人群组和直接消息传递。 Slack还是SitePoint的首选工具,以促进其遍布全球的贡献者之间的协作。 让我们将我们的机器人与Slack集成。

我们需要做的第一件事是在项目中安装Slack适配器

npm install hubot-slack --save

完成后,打开您的Slack Team页面,然后转到“配置集成”区域。 找到Hubot集成,然后单击“添加”。 Slack会要求您为机器人指定一个用户名。 对于为Sitepoint论坛工作人员创建的两个机器人,我选择JarvisTars

提供用户名后,Slack将使用该用户名在您的团队中创建一个帐户,并为其分配API令牌。 将此API令牌保密是非常重要的,因此不要将其检入git存储库。 您还可以选择自定义机器人的图标,名字和姓氏,它的作用等等。

有了API令牌,我们现在可以使用以下命令在本地启动我们的机器人:

HUBOT_SLACK_TOKEN=YOUR_SLACK_API_TOKEN_HERE ./bin/hubot --adapter slack

这会将您的漫游器登录到Slack,以便您可以在那里与之对话。 通过运行以上命令,访问#general频道(Hubot默认使用此频道)并发出以下命令来进行尝试:

<bot_name> pug me

注意 :在本地完​​成实验后,最好将bot移到更永久的位置(例如Heroku)。 关于如何执行此操作 ,有很多很好的指南,在此不做介绍。

在哪里可以找到预建脚本

您可以在npm网站上使用关键字“ hubot-scripts”找到与Hubot配对的各种脚本 。 可以使用npm命令及其--save参数将所有这些脚本添加到您的机器人中。

让我们用获取随机猫图像脚本进行尝试。 真有趣! 跑:

npm install hubot-catme --save

然后将hubot-catme添加到您的external-scripts.json文件中:

[
  "hubot-catme",
  "hubot-diagnostics",
  "hubot-help",
   ...
]

在本地启动漫游器(如上所述),然后访问#general并输入:

<bot_name> cat me with funny

如果一切顺利,您的漫游器应从互联网上随机提取一张有趣的猫图片。

构建脚本以降低员工效率

有很多方法可以降低Hubot的工作效率,从使用哈巴pug bombs到构建一个脚本,当给定命令时,脚本随机选择响应(有时是可笑的)。 后者是我们将在本文中重点介绍的内容,它是Jarvis编程的一部分。

我将使用下面的脚本which-is-better.coffee来说明这一点 。 将此文件放在scripts目录中。

# Description:
#   Which is Better?
#
# Dependencies:
#   None
#
# Configuration:
#   None
# 
# Commands:
#   hubot which is better[?] <text> or <text>?
#   hubot who is better[?] <text> or <text>?
#   hubot which is worse[?] <text> or <text>?
#   hubot who is worse[?] <text> or <text>?
#
# Author:
#   cpradio

uhh_what = [
    "I could tell you, but then I'd have to kill you",
    "Answering that would be a matter of national security",
    "You can't possibly compare them!",
    "Both hold a special place in my heart"
  ]

module.exports = (robot) ->
  robot.respond /(which|who) is (better|worse)\?* (.*) or (.*?)\??$/i, (msg) ->
    choosen_response = msg.random [1..5]
    if choosen_response >= 3
      msg.send msg.random uhh_what
    else
      msg.send "Clearly #{msg.match[choosen_response + 2]} is #{msg.match[2]}"

脚本的顶部是我们的基本注释模板。 在这里,您可以指定脚本的用途,其依赖项以及配置方式的说明。 包括人们可以用来与脚本交互的命令也很重要。

再往下一点,我们找到一行与module.exports = (robots) -> ,这是您的Hubot指令所在的位置。

下一部分使用.respond方法侦听针对您的机器人的特定命令。 所有的听力和响应方法都以正则表达式作为输入。 当该正则表达式找到适用的匹配项时,将调用与.hear.respond命令关联的逻辑。

在这种情况下,我选择.respond ,因为我希望Hubot响应直接命令。 如果我希望它即使在命令不针对他的情况下也做出响应,则可以使用.hear

在这种情况下,正则表达式匹配短语“哪个更好”,“谁更好”,“哪个更糟”和“谁更糟”,后跟两个选项。

robot.respond /(which|who) is (better|worse)\?* (.*) or (.*?)\??$/i, (msg) ->

下一条语句选择1到5的随机数。如果选择的数字大于或等于3,它将使用存储在uhh_what数组中的响应之一发送给用户,否则,将选择适当的first或用户提供的第二个选项作为其响应的一部分。

choosen_response = msg.random [1..5]
if choosen_response >= 3
  msg.send msg.random uhh_what
else
  msg.send "Clearly #{msg.match[choosen_response + 2]} is #{msg.match[2]}"

要对此进行测试,请使用以下命令启动Hubot:

HUBOT_SLACK_TOKEN=YOUR_SLACK_API_TOKEN_HERE ./bin/hubot --adapter slack

然后,您可以使用以下命令在Slack中测试脚本:

<bot_name> who is better? Bill Gates or Steve Jobs?

或者,您可以得到一些更具创意的内容,如下所示。

“谁更好”的输出

构建脚本以提高员工效率

我实施Hubot的主要原因不一定是要减少宝贵的对话,事实上,这就是为什么我们喜欢玩的机器人Jarvis仅限于#random频道,而是我想自动化我们员工在定期的。 考虑到这一目标, Tars出生了,他住在Team Leader私人小组中,以帮助我们查找论坛统计信息。

每个团队负责人需要知道的一个统计数据是,谁最近在场,谁没有。 Tars的discourse.group.info.coffee脚本提供了这种见解。

# Description:
#   Query Discourse for Group Information
#
# Dependencies:
#   None
#
# Configuration:
#   HUBOT_DISCOURSE_URL
#
# Commands:
#   hubot show <group> group info [from discourse]
#
# Author:
#   cpradio

last_checkin_date = new Date();
last_checkin_date.setHours(0,0,0,0);
dif = (last_checkin_date.getDay() + 6) % 7;
last_checkin_date = new Date(last_checkin_date - dif * 24*60*60*1000);

module.exports = (robot) ->
  robot.respond /show (.+) group info( from discourse)?/i, (res) ->
    groupname = res.match[1]
    user_url = process.env.HUBOT_DISCOURSE_URL + "/groups/#{encodeURIComponent(groupname)}/members.json"
    res.http(user_url)
    .get() (err, _, body) ->
      return res.send "Sorry, the tubes are broken." if err
      try
        data = JSON.parse(body.toString("utf8"))
        checkin_date_str = last_checkin_date.toDateString()
        output = "Discourse Info: #{groupname} (last check-in date: #{checkin_date_str})\r\n"
        for own key, user of data.members
          username = user.username
          last_seen = new Date(user.last_seen_at)

          last_seen_as_date = new Date(last_seen)
          checked_in = last_seen_as_date > last_checkin_date
          checked_in_str = if checked_in then "(checked in)" else "(not checked in)"
          prefix_suffix = if checked_in then "" else "*"

          output += "     #{prefix_suffix}User #{username} was last seen on #{last_seen} #{checked_in_str}#{prefix_suffix}\r\n"
        res.send output
      catch e
        res.send "Discourse data for #{groupname} group is unavailable."

与上一个示例类似,该示例也填写了注释。 该命令将由各种成员使用,并且了解如何运行该命令很重要。 其次,需要配置一个环境变量以使此命令起作用,因此也列出了该变量。

在此示例中,我只需要Tars响应tars的直接命令即可tars show {group_name} group info ,其中{group_name}与在Discourse论坛中建立的组匹配(请注意:它必须有一个公开页面,列出此操作的组成员)工作)。

groupname = res.match[1]
user_url = process.env.HUBOT_DISCOURSE_URL + "/groups/#{encodeURIComponent(groupname)}/members.json"
res.http(user_url)
.get() (err, _, body) ->
  return res.send "Sorry, the tubes are broken." if err
  try
    data = JSON.parse(body.toString("utf8"))
    ...

然后,该命令将构建URL以从Discourse获取json提要,并在其数据集上循环以产生类似于以下内容的输出。

输出“显示指导者组信息”

现在,任何团队负责人都可以请求小组的信息,并查看谁一段时间没有活跃,并跟进这些成员。 就像之前的脚本一样,您将需要启动Hubot,但是,我们需要在启动脚本中添加一个新的环境变量,其中包含我们计划对其运行脚本的Discourse URL。 为了本文的方便,让我们使用Sitepoint的Discourse实例。

HUBOT_DISCOURSE_URL=https://www.sitepoint.com/community HUBOT_SLACK_TOKEN=YOUR_SLACK_API_TOKEN_HERE ./bin/hubot --adapter slack

现在在Slack中,您可以执行以下命令:

<bot_name> show mentors group info

继续实验!

简而言之,那就是Hubot。 您已经能够将其与众多适配器多种脚本集成在一起,甚至可以使用其API和一些CoffeeScript来创建自己的应用! 那么,您将用它来创建什么? 在下面的评论中让我知道。

From: https://www.sitepoint.com/spice-up-your-slack-channel-with-hubot/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值