Gets information about a user.
import com.slack.api.bolt.App; import com.slack.api.bolt.AppConfig; import com.slack.api.bolt.jetty.SlackAppServer; import com.slack.api.methods.SlackApiException; import com.slack.api.model.event.TeamJoinEvent; import java.io.IOException; public class UsersInfo { public static void main(String[] args) throws Exception { var config = new AppConfig(); config.setSingleTeamBotToken(System.getenv("SLACK_BOT_TOKEN")); config.setSigningSecret(System.getenv("SLACK_SIGNING_SECRET")); var app = new App(config); // `new App()` does the same app.event(TeamJoinEvent.class, (req, ctx) -> { var logger = ctx.logger; try { var event = req.getEvent(); // Call the users.info method using the built-in WebClient var result = ctx.client().usersInfo(r -> r .token(ctx.getBotToken()) .user(event.getUser().getId()) ); // Print result logger.info("result: {}", result); } catch (IOException | SlackApiException e) { logger.error("error: {}", e.getMessage(), e); } // Acknowledge incoming command event return ctx.ack(); }); var server = new SlackAppServer(app); server.start(); } }
Code to initialize Bolt app// Require the Node Slack SDK package (.com/slackapi/node-slack-sdk) const { WebClient, LogLevel } = require("@slack/web-api"); // WebClient instantiates a client that can call API methods // When using Bolt, you can use either `app.client` or the `client` passed to listeners. const client = new WebClient("xoxb-your-token", { // LogLevel can be imported and used to make debugging simpler logLevel: LogLevel.DEBUG });
// ID of user you watch to fetch information for const userId = "U12345"; try { // Call the users.info method using the WebClient const result = await client.users.info({ user: userId }); console.log(result); } catch (error) { console.error(error); }
Code to initialize Bolt appimport logging import os # Import WebClient from Python SDK (.com/slackapi/python-slack-sdk) from slack_sdk import WebClient from slack_sdk.errors import SlackApiError # WebClient instantiates a client that can call API methods # When using Bolt, you can use either `app.client` or the `client` passed to listeners. client = WebClient(token=os.environ.get("SLACK_BOT_TOKEN")) logger = logging.getLogger(__name__)
# ID of user you watch to fetch information for user_id = "U12345" try: # Call the users.info method using the WebClient result = client.users_info( user=user_id ) logger.info(result) except SlackApiError as e: logger.error("Error fetching conversations: {}".format(e))