3

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.

S. Thomas
  • 33
  • 1
  • 5
  • Welcome S. Thomas, it would be easier to help if we can see some code that you have tried. Links to Bolt documentation and the exact section you are working on solving a problem about would also help. What is this token you're talking about, where does it come from and where is the documentation for that? Etc. – anatolhiman Aug 13 '21 at 03:48

1 Answers1

7

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.

sandra
  • 1,301
  • 3
  • 3
  • Thanks so much Sandra! I just started learning how to use the Slack API and didn't consider using the app_home_opened event to retrieve the user id. I was able to save the user id in a global local variable to use is my command handler function. Thanks again for your help. – S. Thomas Aug 15 '21 at 19:10
  • Thank you very much. I made it using. `user_email = app.client.users_info(user=body["user_id"])['user']['profile']['email']` – Nathaniel Varona Feb 02 '22 at 18:30