使用机器人访问令牌发布到 Slack 频道

Posting to Slack channels with bot access token

我已访问 https://api.slack.com/apps/APP_ID/oauth 并将范围更新为包括 files:write:user,单击 保存更改 并重新安装我的应用程序以确保范围更改生效。

我对 post 我的机器人所属的频道执行以下操作:

web.files
  .upload({
    filename,
    file: fs.createReadStream(filename),
    channels: ['{CHANNEL_ID}']
  })

但是,我仍然收到以下错误消息:

{ ok: false,
  error: 'invalid_channel',
  channel: '["{CHANNEL_ID}"]',
  scopes: [ 'identify', 'bot:basic' ],
  acceptedScopes: [ 'files:write:user', 'post' ] } }

我做错了什么?

错误消息 invalid_channel 与范围无关,只是表示 Slack 无法识别您在 API 调用中提供的渠道之一。

官方文档说:

invalid_channel: One or more channels supplied are invalid

所以您可能需要仔细检查您的频道 ID 是否正确。

来源:https://api.slack.com/methods/files.upload#errors

我的错误是我使用的是频道 ID 的 数组 ,而不是 what the library expects,它是一个包含:

的字符串

Comma-separated list of channel names or IDs

就我而言,这是因为尚未邀请机器人用户加入频道。将它们添加到私人 Slack 频道解决了这个问题。