我正在尝试设置两个变量“ TOTAL_GHE_ISSUES”“ GHE_ISSUE_NUMBERS”,然后将它们插入到json有效负载中,以跟随https://slack.dev/node-slack-sdk/webhook进行松弛的Webhook。我对js / nodejs完全没有经验,所以请理解我是否在犯一个简单的错误
我尝试在.js脚本的顶部设置变量,并尝试转义/取消转义值,但是遇到麻烦
const IncomingWebhook = require('@slack/webhook').IncomingWebhook;
const url = process.env.SLACK_WEBHOOK_URL;
const webhook = new IncomingWebhook(url);
var x = "10"
const TOTAL_GHE_ISSUES = "10"
const GHE_ISSUE_NUMBERS = "90"
// Send the notification
if (x == "10")
(async () => {
await webhook.send({
text: "*Daily Overdue Nessus Vulnerability Alert*",
attachments: [{color: "#FF0000", blocks: [{type: "section",text: {type: "mrkdwn",text: "@here *TOTAL_GHE_ISSUES* Overdue Nessus Vulnerability issues reported \nOverdue Nessus Vulnerability GHE Issue Numbers: *GHE_ISSUE_NUMBERS*"}}]}]
});
})();
else {console.log("What's up");}
我希望网络钩子返回10和90代替TOTAL_GHE_ISSUES和GHE_ISSUE_NUMBERS,但它返回的是文字字符串
编辑:我使用了下面发布的字符串连接,它可以达到我的目的,并使我的有效负载更加模块化,非常感谢您的所有帮助
text: "@here *" + TOTAL_GHE_ISSUES + "* Overdue Nessus Vulnerability issues reported \nOverdue Nessus Vulnerability GHE Issue Numbers: *" + GHE_ISSUE_NUMBERS + "*"
答案 0 :(得分:0)
您可以简单地使用``((Template literal)
text: `@here *${TOTAL_GHE_ISSUES}* Overdue Nessus Vulnerability issues reported \nOverdue Nessus Vulnerability GHE Issue Numbers: *${GHE_ISSUE_NUMBERS}*`
或者您也可以使用字符串缩写
text: "@here *" + TOTAL_GHE_ISSUES + "* Overdue Nessus Vulnerability issues reported \nOverdue Nessus Vulnerability GHE Issue Numbers: *" + GHE_ISSUE_NUMBERS + "*"
答案 1 :(得分:-1)
您可以使用Template literals的字符串插值功能。语法是:
string text ${expression} string text
。
您可以的话
`text: "@here *${TOTAL_GHE_ISSUES}"`