簡體   English   中英

如何在不向每條消息添加hubot的情況下訪問hubot?

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

我正在使用hubot和hubot-fb適配器開發一個facebook messenger機器人。 所有基本設置都已完成並且工作正常。 但是,為了與機器人聊天,我需要添加所有命令的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

但是,我希望我的機器人可以在不使用所有messenges的“hubot”的情況下訪問。 我如何實現這一目標?

TIA

您可以使用“聽覺”方法,而不是“響應”方法。

Hubot有兩種方法可以與消息進行交互:

聽到 - 只要消息室中的文本與給定的正則表達式匹配,就會調用此方法。 在這種情況下不使用機器人的名稱。 例:

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

以下消息將導致調用robot.hear回調:

  • 我該如何ping服務器

注意:在這種情況下,正則表達式非常簡單,可以修改,因此不會為“我如何ping服務器”案例調用回調。

響應 - 只有當文本與給定的正則表達式匹配時才會調用它,並且緊跟在機器人的名稱或別名之后。 例:

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

以下消息將導致調用robot.respond回調:

  • @Hubot ping
  • Hubot ping

由於未使用機器人的名稱,因此不會調用以下內容。

  • 我該如何ping服務器

有關更多詳細信息,請參閱Hubot腳本文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM