我有一条关于我的 signingSecret for slack bot 的消息错误

I have a message error about my signingSecret for slack bot

我是开发初学者,我尝试使用以下代码开发一个 slack 机器人:

const {App} = require('@slack/bolt');

const app = new App ({
    token : process.env.SLACK_BOT_TOKEN,
    signingSecret: process.env.SLACK_SIGNING_SECRET
});

(async () => {
    await app.start(process.env.PORT || 3000);
})();

对于我的 .env:

{
  SLACK_BOT_TOKEN = "my token",
  SLACK_SIGNING_SECRET = "my Signing Secret"
}

和我的 package.json:

"scripts": {
    "dev":"nodemon slack.js"
  },
  "dependencies": {
    "@slack/bolt": "^3.11.0",
}

但它不起作用。我有这条消息:

AppInitializationError: signingSecret is required to initialize the default receiver. Set signingSecret or use a custom receiver. You can find your Signing Secret in your Slack App Settings.

我参考了Slack的文档,经过验证和代码审查,好像是一样的,有人知道为什么吗?

您必须安装 dotenv 包并使用它来解析 .env 文件并加载其变量。

  • 安装dotenv:npm i dotenv

  • 在程序入口文件的开头写上:require('dotenv').config()

当您使用 process.env.{SOME_VARIABLE} 时,您在执行 js 脚本期间引用了您的本地系统变量,因此这意味着它们在某种程度上应该是 sourced。 因为它已经很难过 Ali Rashidi 你可以使用 dotenv npm 包来实现这个目标,但我个人更喜欢通过配置文件加载它们,或者甚至更好地直接在你的 [=26= 中获取你的 .env 文件] 在开始你的脚本之前 即:

source .env
node yourscript.js

注意:此方法每次都需要手动获取 .env 文件,因此这种方法不太实用

相反,您可以尝试做类似

的事情
// slack_configuration.json
{

   "SLACK_BOT_TOKEN": "YOUR TOKEN",
   "SLACK_SIGNING_SECRET": "YOUR SIGNING SECRET"
}
const { App } = require('@slack/bolt');

const slackConfiguration = require('./slack_configuration.json')

const app = new App ({
    token : slackConfiguration.SLACK_BOT_TOKEN,
    signingSecret: slackConfiguration.SLACK_SIGNING_SECRET
});

(async () => {
    await app.start(process.env.PORT || 3000);
})();

一般来说,这种方法在本地机器中是首选,而不是在专门为 运行 应用程序设计的特定容器中,因为它更安全,所以你 SECRET 不应该通过 .env 访问,因为系统中的所有进程都可以访问它们,因此它们是公开的