robot.hear /hello name/i, (-6ren">
gpt4 book ai didi

coffeescript - Hubot 听觉变量

转载 作者:行者123 更新时间:2023-12-01 15:01:31 24 4
gpt4 key购买 nike

我希望找到办法让 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com