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

标签 javascript facebook-messenger hubot

我正在使用 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

但是,我希望我的机器人可以在不使用“hubot”处理所有消息的情况下进行访问。我该如何实现这一目标?

TIA

最佳答案

您可以使用“hear”方法,而不是“respond”方法。

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

hear - 只要消息室中的文本与给定的正则表达式匹配,就会调用此函数。本例中未使用机器人的名称。示例:

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

以下消息将导致调用 robots.hear 回调:

  • 如何 ping 服务器

注意:本例中的正则表达式非常简单,可以进行修改,以便在“如何 ping 服务器”情况下不会调用回调。

respond - 仅当文本与给定的正则表达式匹配并且前面紧跟着机器人的名称或别名时才会调用此函数。示例:

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

以下消息将导致调用 robots.respond 回调:

  • @Hubot ping
  • Hubot ping

由于未使用机器人的名称,因此不会调用以下内容。

  • 如何 ping 服务器

请参阅Hubot scripting documentation了解更多详情。

关于javascript - 如何在不为每条消息添加 hubot 的情况下访问 hubot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38158856/

相关文章:

javascript - CoffeeScript - 使用参数执行 bash 脚本

node.js - 在ubuntu上安装hubot时出错

javascript - 使用 OneDrive API (JavaScript) 创建空白文件 (*.docx | *.ppt | *.xlsx)

javascript - 设置 Angular 变量互不影响

javascript - 在 javascript 方法调用中更改 'this'?

iOS Facebook Messenger,打开并完成任务后自动关闭链接

javascript - 如何实现 Wunderlist 所做的侧弹出窗口?

reactjs - 如何在 React 中集成 Messenger 聊天插件?

facebook-apps - Facebook Messenger 应用程序无效范围 - 权限 : pages_messaging_subscriptions

javascript - wit.ai Bot Engine 连接到 hubot 的故事