如何拦截/挂钩Hubot响应

时间:2015-09-28 07:13:36

标签: coffeescript hubot chatbot

有没有办法全局拦截所有Hubot触发器/响应?拦截应该能够在发送之前检查,修改,转发或拒绝Hubot响应。

我想实现的一些目标:

  • 限制Hubot发送的所有消息(来自所有插件/脚本)以防止泛滥。
  • 应用某种ACL(访问控制列表)来限制谁可以使用命令。

我在Hubot官方文档中找不到它。我错过了一些东西吗?

2 个答案:

答案 0 :(得分:1)

要控制对侦听器的访问,请查看侦听器中间件:https://hubot.github.com/docs/scripting/#listener-middleware https://hubot.github.com/docs/patterns/#restricting-access-to-commands

对于速率限制命令执行,请查看hubot-rate-limit:https://github.com/michaelansel/hubot-rate-limit

为了控制响应,请密切关注响应中间件PR:https://github.com/github/hubot/pull/1021

答案 1 :(得分:1)

这是我写的一个简单的中间件,用于记录针对机器人的消息。根据用户名或房间名称或其他内容,可以轻松修改它以执行其他操作。

module.exports = (robot) ->
  robot.listenerMiddleware (context, next, done) ->
    #create a regex with the robots name in it
    robotName = new RegExp("#{context.listener.robot.name}", "i")
    #only log messages meant for the robot
    if robotName.test("#{context.response.message.text}")
      #only log messages once with the "everything" listener context
      if context.listener.regex.source is /(.+)/i.source
        console.log "User: #{context.response.message.user.name} asked me to \"#{context.response.message.text}\" in Channel: #{context.response.message.room}"
        #your code goes here
    next()

this可以让你限速

相关问题