可变听力 Hubot
Hubot Hear Variable
我希望找到办法让 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."
如果它有用,使用 javascript 而不是 coffeescript 的 hubot 示例也采用所选选项并将其放在另一个变量中:
const options = 'now|later|never';
const regexOptions = new RegExp(`starting (${options})`, 'i');
robot.respond(regexOptions, (msg) => {
// this will respond to:
// hubot starting now
// but not to:
// hubot starting notAnOption
const optionChosen = msg.match[1];
msg.send(`option chosen: ${optionChosen}`);
});
当我有可以添加或删除名称的动态列表时,我会使用这种技术,然后我想在响应中使用它——在将名称添加到列表中时,它确实需要快速重新加载 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."
如果它有用,使用 javascript 而不是 coffeescript 的 hubot 示例也采用所选选项并将其放在另一个变量中:
const options = 'now|later|never';
const regexOptions = new RegExp(`starting (${options})`, 'i');
robot.respond(regexOptions, (msg) => {
// this will respond to:
// hubot starting now
// but not to:
// hubot starting notAnOption
const optionChosen = msg.match[1];
msg.send(`option chosen: ${optionChosen}`);
});
当我有可以添加或删除名称的动态列表时,我会使用这种技术,然后我想在响应中使用它——在将名称添加到列表中时,它确实需要快速重新加载 hubot当然是命令。