猿问

有没有一种方法可以将js变量插入JSON有效负载?

我试图设置两个变量“ TOTAL_GHE_ISSUES”“ GHE_ISSUE_NUMBERS”,然后将它们插入到https://slack.dev/node-slack-sdk/webhook之后的松弛Webhook的json有效负载中。我对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");}

我希望webhook返回TOTAL_GHE_ISSUES和GHE_ISSUE_NUMBERS的10和90,但它返回的是文字字符串


编辑:我使用了下面发布的字符串连接,它达到了我的目的,并使我的有效负载更加模块化,非常感谢您的所有帮助


text: "@here *" + TOTAL_GHE_ISSUES + "* Overdue Nessus Vulnerability issues reported \nOverdue Nessus Vulnerability GHE Issue Numbers: *" + GHE_ISSUE_NUMBERS + "*"


慕容3067478
浏览 120回答 2
2回答

呼唤远方

您可以简单地使用``(模板文字)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 + "*"
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答