如果没有在每条消息中添加hubot,我如何访问hubot ?

How can I access hubot without adding hubot with each message?

本文关键字:hubot 何访问 访问 消息 添加 如果没有      更新时间:2023-09-26

我正在开发一个facebook messenger bot使用hubot &hubot-fb适配器。所有的基本设置都完成了&工作得很好。但是,为了与bot聊天,我需要添加hubot和所有的命令。在facebook聊天的情况下,这没有多大意义。当前对话看起来像这样:

user: hubot ping
bot: PONG
user: hubot the rules
bot: 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 p
然而,我希望我的机器人可以访问,而不使用"hubot"与所有的消息。我该怎么做呢?

TIA

您可以使用'hear'方法,而不是' response '方法。

Hubot有两种与消息交互的方法:

hear -当消息室中的文本匹配给定的正则表达式时调用该函数。在这种情况下没有使用机器人的名称。例子:

module.exports = (robot) ->
  robot.hear /ping/i, (res) ->
    res.send "PONG"

以下消息将导致机器人。听到回调被调用:

  • ping
  • 如何ping服务器

注意:这种情况下的正则表达式非常简单,可以修改,这样就不会在"我如何ping服务器"的情况下调用回调。

response -这只在文本匹配给定的正则表达式并且前面紧接机器人的名称或别名时调用。例子:

module.exports = (robot) ->
  robot.respond /ping/i, (res) ->
    res.reply "PONG"

以下消息将导致机器人。要调用的响应回调:

  • @Hubot ping
  • Hubot ping

由于没有使用机器人的名称,因此在以下情况下不会调用它。

  • ping
  • 如何ping服务器

查看Hubot脚本文档了解更多细节