简体   繁体   English

如何在不向每条消息添加hubot的情况下访问hubot?

[英]How can I access hubot without adding hubot with each message?

I am developing a facebook messenger bot using hubot & hubot-fb adapter. 我正在使用hubot和hubot-fb适配器开发一个facebook messenger机器人。 All the basic setup is done & is working perfectly. 所有基本设置都已完成并且工作正常。 But, in order to chat with the bot I need to add hubot with all the commands. 但是,为了与机器人聊天,我需要添加所有命令的hubot。 In case of facebook chats, it doesn't make much sense. 在Facebook聊天的情况下,它没有多大意义。 A current chat looks something like this: 当前聊天看起来像这样:

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

however, I want my bot to be accessible without using the "hubot" with all the messenges. 但是,我希望我的机器人可以在不使用所有messenges的“hubot”的情况下访问。 How do I achieve that? 我如何实现这一目标?

TIA TIA

You could use the 'hear' method, instead of the 'respond' method. 您可以使用“听觉”方法,而不是“响应”方法。

Hubot has two methods for how it interacts with with messages: Hubot有两种方法可以与消息进行交互:

hear - This is called whenever text in a message room matches a given regex. 听到 - 只要消息室中的文本与给定的正则表达式匹配,就会调用此方法。 The robot's name is not used in this case. 在这种情况下不使用机器人的名称。 Example: 例:

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

The following messages would cause the robot.hear callback to be called: 以下消息将导致调用robot.hear回调:

  • ping
  • how do I ping the server 我该如何ping服务器

Note: the regex in this case is very simplistic and could be modified so the callback is not called for the "how do I ping the server" case. 注意:在这种情况下,正则表达式非常简单,可以修改,因此不会为“我如何ping服务器”案例调用回调。

respond - this is only called when text matches a given regex AND is immediately preceded by the robot's name or alias. 响应 - 只有当文本与给定的正则表达式匹配时才会调用它,并且紧跟在机器人的名称或别名之后。 Example: 例:

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

The following messages would cause the robot.respond callback to be called: 以下消息将导致调用robot.respond回调:

  • @Hubot ping @Hubot ping
  • Hubot ping Hubot ping

It would NOT be called for the following, since the robot's name is not used. 由于未使用机器人的名称,因此不会调用以下内容。

  • ping
  • how do I ping the server 我该如何ping服务器

See the Hubot scripting documentation for more details. 有关更多详细信息,请参阅Hubot脚本文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM