如何允许电报机器人检查命令的可选参数
How to allow telegram bot to check for optional arguements for a command
我正在尝试检查用户在向机器人发送命令时是否传递了可选参数。我写的代码如下所示。
bot.onText(/\/command (.+)?/, function(msg, match) {
let str = match[1];
if (str != null) {
console.log("not null");
} else
console.log("null");
});
现在,当我通过参数来测试代码时,会打印输出。但是,如果未通过可选参数,则没有输出。是我的正则表达式不正确还是我执行的方式不正确?
谢谢
您的正则表达式在命令和左括号之间有一个 space。 /\/command (.+)?/
。
因此,仅使用 /command
匹配 Regex 将 return 整个 null 而不是有效数组。
尝试使用 /\/command(\s.+)?/
.
我正在尝试检查用户在向机器人发送命令时是否传递了可选参数。我写的代码如下所示。
bot.onText(/\/command (.+)?/, function(msg, match) {
let str = match[1];
if (str != null) {
console.log("not null");
} else
console.log("null");
});
现在,当我通过参数来测试代码时,会打印输出。但是,如果未通过可选参数,则没有输出。是我的正则表达式不正确还是我执行的方式不正确?
谢谢
您的正则表达式在命令和左括号之间有一个 space。 /\/command (.+)?/
。
因此,仅使用 /command
匹配 Regex 将 return 整个 null 而不是有效数组。
尝试使用 /\/command(\s.+)?/
.