coffeescript - Hubot 听觉变量

标签 coffeescript hubot

我希望找到办法让 hubot 到达这里一个变量。 例如

name = "Peter"
module.exports = (robot) ->
   robot.hear /hello name/i, (msg) ->
       msg.send "Peter?! No I am Hubot."

我已经尝试过如下所示的“#{}”语法,但没有任何结果。

name = "Peter"
module.exports = (robot) ->
   robot.hear /hello #{name}/i, (msg) ->
       msg.send "Peter?! No I am Hubot."

任何帮助将不胜感激。

问候,

奥斯汀

最佳答案

由于您的正则表达式不是常量,因此您应该使用 new Regex() :

Using the constructor function provides runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.

代码

name = "Peter"
regx = new Regex("hello #{ name }", 'i')
module.exports = (robot) ->
  robot.hear regx, (msg) ->
      msg.send "Peter?! No I am Hubot."

编辑 以名称作为参数

module.exports = (robot, name) ->
  regx = new Regex("hello #{ name }", 'i')
  robot.hear regx, (msg) ->
      msg.send "#{ name }?! No I am Hubot."

关于coffeescript - Hubot 听觉变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34098300/

相关文章:

javascript - 如何使用 CoffeeScript 将 IPv4 转换为整数?

coffeescript - CoffeeScript 中的非表达式是什么?

node.js - 为什么 Node.js 以这种方式执行?

javascript - Backbone Marionette : Router Not Routing Correctly

javascript - 清除 hubot 上的计时器重新加载所有脚本

javascript - 在 Meteor 中访问 Stylus 中的 Javascript 变量

json - 我的 Node.js hubot 代码中出现奇怪的 JSON.parse 错误

regex - 在 Coffeescript 中匹配 Hubot 的名字

git - 使用github api从 pull 请求号获取 pull 请求 merge 提交sha