n8n 中文文档

Slack 凭据

您可以使用这些凭据来验证以下节点:

支持的身份验证方法

相关资源

请参考 Slack 的 API 文档{:target=_blank .external-link},了解有关该服务的更多信息。

使用 API 访问令牌

要配置此凭据,您需要一个 Slack{:target=_blank .external-link} 账户和:

  • 一个 Access Token 访问令牌

要生成访问令牌,请创建 Slack 应用。n8n 建议从 manifest 创建应用,这会一步配置所有 scopes:

从 manifest 创建 API 访问令牌应用

  1. 打开 Slack API Apps{:target=_blank .external-link} 页面。
  2. 选择 Create New App > From a manifest。
  3. 选择要在其中开发应用的 Workspace,然后选择 Next。
  4. 选择 JSON 选项卡,并用下面的 JSON 替换默认 manifest。可按需调整 scopes。
  5. 选择 Next。
  6. 查看摘要并选择 Create。应用详情会打开。
{
  "display_information": {
    "name": "n8n integration"
  },
  "features": {
    "bot_user": {
      "display_name": "n8n integration"
    }
  },
  "oauth_config": {
    "scopes": {
      "bot": [
        "channels:read",
        "channels:history",
        "chat:write",
        "files:read",
        "files:write",
        "groups:read",
        "groups:history",
        "im:read",
        "im:history",
        "mpim:read",
        "mpim:history",
        "reactions:read",
        "reactions:write",
        "usergroups:read",
        "usergroups:write",
        "users.profile:read",
        "users:read"
      ]
    }
  },
  "settings": {
    "org_deploy_enabled": false,
    "socket_mode_enabled": false,
    "token_rotation_enabled": false
  }
}

从头创建 API 访问令牌应用

  1. 打开 Slack API Apps{:target=_blank .external-link} 页面。
  2. 选择 Create New App > From scratch。
  3. 输入 App Name。
  4. 选择要在其中开发应用的 Workspace。
  5. 选择 Create App。应用详情会打开。
  6. 在左侧菜单的 Features 下,选择 OAuth & Permissions。
  7. 在 Scopes 部分,为你的应用选择合适的 scopes。推荐 scopes 列表请参阅 Scopes。

然后安装应用并复制令牌:

  1. 在左侧菜单的 Features 下,选择 OAuth & Permissions。
  2. 在 OAuth Tokens 部分,选择 Install to Workspace。你必须是 Slack workspace 管理员才能完成此操作。
  3. 选择 Allow。
  4. 复制 Bot User OAuth Token,并在 n8n 凭据中作为 Access Token 访问令牌 输入。
  5. 如果你将此凭据用于 Slack Trigger,请按照 Slack Trigger 配置 中的步骤完成应用设置。

更多信息请参考 Slack API Quickstart{:target=_blank .external-link}。

Slack Trigger 配置

要将 Slack 应用与 Slack Trigger 节点结合使用:

  1. 在 Slack 中转到 Your Apps{:target=_blank .external-link},并选择要使用的应用。
  2. 转到 Features > Event Subscriptions。
  3. 打开 Enable Events 控件。
  4. 在 n8n 中复制 Webhook URL,并将其作为 Request URL 输入到 Slack 应用中。

    Request URL

    Slack 每个应用只允许一个 request URL。如果要测试工作流,需要执行以下操作之一:

    • 先使用 Test URL 测试,验证一切正常后,再将 Slack 应用改为使用 Production URL
    • 使用带执行日志记录的 Production URL。

  5. 验证后,选择要订阅的 bot events。使用 n8n 中的 Trigger on 触发条件 字段过滤这些请求。

    • 要使用列表中没有的事件,请将其添加为 bot event,并在 n8n 节点中选择 Any Event 任何事件。

更多信息请参阅 Quickstart | Configuring the app for event listening{:target=_blank .external-link}。

提示

Slack 验证 Request URL 后,你也可以在应用的 manifest 中管理事件订阅:转到 Settings > App Manifest,编辑 settings.event_subscriptions block。

n8n 建议为 Slack Trigger 启用请求签名验证,以获得额外安全性:

  1. 在 Slack 中转到 Your Apps{:target=_blank .external-link},并选择要使用的应用。
  2. 转到 Settings > Basic Information。
  3. 复制 Signing Secret 的值。
  4. 在 n8n 中,将此值粘贴到凭据的 Signature Secret 签名密钥 字段中。

Slack 中的审批也需要 Signature Secret 签名密钥 字段。

使用 OAuth2

提示

Note for n8n Cloud users n8n 云用户注意事项

云用户不需要提供连接详细信息。选择 Connect my account 连接我的帐户 通过浏览器进行连接。

如果你自托管 n8n 并且需要从头配置 OAuth2,则需要一个 Slack{:target=_blank .external-link} 账户和:

  • 一个 Client ID
  • 一个 Client Secret

要获取两者,请创建 Slack 应用。n8n 建议从 manifest 创建应用,这会一步配置 scopes 和 redirect URL:

从 manifest 创建 OAuth2 应用

  1. 在 n8n 中,从 Slack 凭据复制 OAuth Callback URL。
  2. 打开 Slack API Apps{:target=_blank .external-link} 页面。
  3. 选择 Create New App > From a manifest。
  4. 选择要在其中开发应用的 Workspace,然后选择 Next。
  5. 选择 JSON 选项卡,并用下面的 JSON 替换默认 manifest。将 <your-oauth-callback-url> 替换为从 n8n 复制的 URL,并按需调整 scopes。
  6. 选择 Next。
  7. 查看摘要并选择 Create。应用详情会打开。
{
  "display_information": {
    "name": "n8n integration"
  },
  "features": {
    "bot_user": {
      "display_name": "n8n integration"
    }
  },
  "oauth_config": {
    "redirect_urls": [
      "<your-oauth-callback-url>"
    ],
    "scopes": {
      "bot": [
        "channels:read",
        "channels:history",
        "chat:write",
        "files:read",
        "files:write",
        "groups:read",
        "groups:history",
        "im:read",
        "im:history",
        "mpim:read",
        "mpim:history",
        "reactions:read",
        "reactions:write",
        "usergroups:read",
        "usergroups:write",
        "users.profile:read",
        "users:read"
      ],
      "user": [
        "channels:write",
        "groups:write",
        "search:read",
        "search:read.public",
        "search:read.private",
        "search:read.im",
        "search:read.mpim",
        "stars:read",
        "stars:write",
        "users.profile:write"
      ]
    }
  },
  "settings": {
    "org_deploy_enabled": false,
    "socket_mode_enabled": false,
    "token_rotation_enabled": false
  }
}
仅包含 user scopes 的 manifest 某些 Slack API endpoints,包括 Slack 的 MCP endpoints,只能使用 user tokens,不能使用 bot tokens。如果你需要 user token,请改用此 manifest。它会把所有 scopes 作为 user scopes 请求:
{
  "display_information": {
    "name": "n8n integration"
  },
  "oauth_config": {
    "redirect_urls": [
      "<your-oauth-callback-url>"
    ],
    "scopes": {
      "user": [
        "channels:read",
        "channels:write",
        "channels:history",
        "chat:write",
        "files:read",
        "files:write",
        "groups:read",
        "groups:write",
        "groups:history",
        "im:read",
        "im:history",
        "mpim:read",
        "mpim:history",
        "reactions:read",
        "reactions:write",
        "stars:read",
        "stars:write",
        "usergroups:read",
        "usergroups:write",
        "users.profile:read",
        "users.profile:write",
        "users:read",
        "search:read",
        "search:read.public",
        "search:read.private",
        "search:read.im",
        "search:read.mpim"
      ]
    }
  },
  "settings": {
    "org_deploy_enabled": false,
    "socket_mode_enabled": false,
    "token_rotation_enabled": false
  }
}

从头创建 OAuth2 应用

  1. 打开 Slack API Apps{:target=_blank .external-link} 页面。
  2. 选择 Create New App > From scratch。
  3. 输入 App Name。
  4. 选择要在其中开发应用的 Workspace。
  5. 选择 Create App。应用详情会打开。
  6. 在左侧菜单的 Features 下,选择 OAuth & Permissions。
  7. 在 Redirect URLs 部分,选择 Add New Redirect URL。
  8. 从 n8n 复制 OAuth Callback URL,并将其作为新的 Redirect URL 输入到 Slack。
  9. 选择 Add。
  10. 选择 Save URLs。
  11. 在 Scopes 部分,为你的应用选择合适的 scopes。scopes 列表请参阅 Scopes。

然后完成将应用连接到 n8n:

  1. 在 Settings > Basic Information 中,打开 App Credentials 部分。
  2. 复制 Client ID 和 Client Secret。将它们粘贴到 n8n 中的相应字段。
  3. 在左侧菜单的 Features 下,选择 OAuth & Permissions。
  4. 在 OAuth Tokens 部分,选择 Install to Workspace。你必须是 Slack workspace 管理员才能完成此操作。
  5. 选择 Allow。
  6. 此时,你应该可以在 n8n 凭据中选择 OAuth 按钮进行连接。

更多信息请参考 Slack API Quickstart{:target=_blank .external-link}。有关 OAuth 流程本身的更多详情,请参考 Slack Installing with OAuth{:target=_blank .external-link} 文档。

Scopes

Scopes 决定应用具有哪些权限。

  • 如果你希望应用代表授权应用的用户行事,请在 User Token Scopes 部分下添加所需 scopes。
  • 如果你正在构建 bot,请在 Bot Token Scopes 部分下添加所需 scopes。

以下是 OAuth 凭据所需的 scopes 列表,可作为良好起点。上面的 app manifests 已包含这些 scopes:

Scope name 备注
channels:read
channels:write 不可用作 bot token scope
channels:history
chat:write
files:read
files:write
groups:read
groups:write 用于私有频道管理操作
groups:history
im:read
im:history
mpim:read
mpim:history
reactions:read
reactions:write
stars:read 不可用作 bot token scope
stars:write 不可用作 bot token scope
usergroups:read
usergroups:write
users.profile:read
users.profile:write 不可用作 bot token scope
users:read
search:read 已被 Slack 弃用;为节点版本 <= 2.6 保留
search:read.public 用于搜索公开频道消息
search:read.private 不可用作 bot token scope
search:read.im 不可用作 bot token scope
search:read.mpim 不可用作 bot token scope

常见问题

Token 过期

Slack 提供 token rotation 代币轮换,您可以为机器人和用户令牌打开它。这使得每个令牌都会在 12 小时后过期。虽然这可能对测试有用,但使用启用此功能的令牌的 n8n 凭证在过期后将失败。如果您想在生产中使用 Slack 凭证,则此功能必须是 off 关闭。

要检查您的 Slack 应用是否启用了令牌轮换,请参阅 Slack API 文档 | 令牌轮换{:target=_blank .external-link}。

提示

If your app uses token rotation 如果您的应用程序使用令牌轮换

请注意,如果您的 Slack 应用程序使用令牌轮换,您将无法再次将其关闭。您需要创建一个新的 Slack 应用程序并禁用令牌轮换。

不想自己部署和维护 n8n?n8ncn 托管 n8n 云服务:开通账号即可使用,不用买服务器;也可以在 n8ncn 工作流库 找现成的中文工作流。