javascriptnode.jsslackslack-apibolt

How to get the current user email address for Slack bot using Bolt for JavaScript(Node.JS)


For context, I'm using the Bolt for JavaScript framework to develop a Slack bot. I have a workspace where I'm the only member.

In the app home, I need to retrieve the current user's email address, but I'm not sure how to do that.

WebAPI methods for users for example, users.profile.get and users.identity, require a user token, I suppose I need to get the user token of the current user.

Any ideas on how to retrieve the user token from the backend? Suggestions would be appreciated.


Solution

  • Instead of a user token, I'd recommend using a bot token to call users.info (https://api.slack.com/methods/users.info). You will need to request the users:read and the users:read.email scopes in order for the email to be returned in the API call. When you install your app on your Workspace your bot token will appear in the OAuth & Permissions page of your Developer Config site (https://api.slack.com/apps). Your app should be listening for the app_home_opened event. When a user opens the App Home the user_id will be returned in the event payload. You can then use that user_id to call users.info and get the user email.