使用Slack Node SDK调用client.conversations.open()报user_not_found错误
Slack Node SDK 发送私信报user_not_found 问题排查
问题详情
我使用Slack Node SDK尝试通过用户ID发送机器人私信,代码如下:
const client = new WebClient(process.env.SLACK_TOKEN); const privateMessage = async (userId) => { try { await client.conversations.open({ users: [userId], text: 'Hi there.....', }); } catch (error) { console.log(error); } }; privateMessage('USERIDXXXXXX');
执行后返回user_not_found错误:
Error: An API error occurred: user_not_found at platformErrorFromResult (/Users/mej/Documents/test_repo/node_modules/.pnpm/@slack+web-api@6.10.0/node_modules/@slack/web-api/dist/errors.js:62:33) at WebClient.apiCall (/Users/mej/Documents/test_repo/node_modules/.pnpm/@slack+web-api@6.10.0/node_modules/@slack/web-api/dist/WebClient.js:181:56) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async privateMessage (/Users/mej/Documents/test_repo/apps/reports/bot/test_bot.ts:22:5) { code: 'slack_webapi_platform_error', data: { ok: false, error: 'user_not_found', response_metadata: { scopes: [Array], acceptedScopes: [Array] } } }
这些用户ID都是通过同一个机器人OAuth令牌调用client.users.list()获取的,甚至使用我自己账户的用户ID也会报错。应用已配置权限:channels:manage, channels:read, chat:write, groups:write, im:write, mpim:write, users:read, users:read.email。
解决方法
1. 修正conversations.open的参数格式
Slack API的conversations.open接口中,users参数接受的是逗号分隔的字符串,而非数组。你当前代码中传入数组会导致API无法正确解析用户ID,这是报错的核心原因。修改代码如下:
await client.conversations.open({ users: userId, // 单个用户直接传字符串 text: 'Hi there.....', });
如果需要给多个用户发私信,用逗号分隔:
users: 'U123456,U789012'
2. 确认令牌类型和权限有效性
- 确保使用的是Bot User OAuth Token(以
xoxb-开头),而非User OAuth Token(xoxp-开头)。conversations.open需要Bot Token才能正常调用。 - 重新安装应用到工作区:权限配置变更后,需重新安装应用使权限生效。进入Slack应用管理后台,找到你的应用,点击「重新安装到工作区」。
3. 验证机器人与用户的互动状态
- 检查目标用户是否在机器人所在的工作区内,且未拉黑机器人。可以在Slack客户端中搜索机器人账号,手动发送一条消息,触发会话建立后再尝试API调用。
内容的提问来源于stack exchange,提问作者mojones101
相关产品推荐
相关产品推荐

