I'm using the Slack node SDK to try to send private messages from a bot based on a user 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');
When I do so, I get the following error:
Error: An API error occurred: user_not_found
at platformErrorFromResult (/Users/mej/Documents/test_repo/node_modules/.pnpm/@[email protected]/node_modules/@slack/web-api/dist/errors.js:62:33)
at WebClient.apiCall (/Users/mej/Documents/test_repo/node_modules/.pnpm/@[email protected]/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] }
}
}
This error is confusing because the user IDs I've tried were all returned by another call to the API (client.users.list()
) with the same client / Bot User OAuth token. I know those users exist. I can see my own user ID in my Slack account settings, as well, and it also returns the user_not_found error.
The Slack app has the following permissions: channels:manage, channels:read, chat:write, groups:write, im:write, mpim:write, users:read, users:read.email.
The users
parameter for the conversations.open
method is actually supposed to be a comma-separated string and not an array, which is why you're getting the user_not_found
error! You can see the full specifications of the conversations.open
method here: https://api.slack.com/methods/conversations.open#arg_users.
Also, just in case, the method to send messages into Slack is chat.postMessage
not conversations.open
: https://api.slack.com/methods/chat.postMessage