Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the username of who is interacting with the bot, Plus one more issue #64

Open
AtbTheGreat opened this issue Jul 20, 2017 · 7 comments

Comments

@AtbTheGreat
Copy link

I don't mean to bother you with so many issues, but I still need help on some aspects of the bot I'm working on, because I am almost done with my bot that uses the JBot framework except for a few aspects.

  1. How do I get the username of the user currently interacting with the bot, as a String?

  2. What EventType should I set the parameter events equal to so that my bot will automatically post a DM to each user at a certain time every day, even if no one is logged in? BTW, I am aware of how to use TimerTask to set a certain time for the bot to send a message, but I need to know what to set events to.

@mndominguez
Copy link

@AtbTheGreat I didn't find a way to do this without making an API call to Slack.

My code is:

public JSONObject getSlackUsername(Event event) {
        String slackUserGetUri = "users.info?token=YOUR_TOKEN&user=";
        Response slackUserResponse = slackRestClient.get(slackUserGetUri + event.getUserId());
        return slackUserResponse.asJSON();
    }

I know this is less than optimal, but it gets the job done.

@jjenksy
Copy link

jjenksy commented Oct 21, 2017

Did you figure out the send a Direct message piece for this? I am needing to send a DM to users.

@adamoutler
Copy link

@mndominguez can you elaborate on that example? these other methods aren't available in the example or the api. Where can I form a slackRestClient?

@mndominguez
Copy link

Hey @adamoutler

In my case, that slackRestClient gets initialized like this

private final NioRestClient slackRestClient = (NioRestClient) RestClientBuilder.builder()
            .setNio(true)
            .setBaseUrl("https://slack.com/api/")
            .setSocketTimeout(2000)
            .setMaxConnections(30)
            .setMaxConnectionPerRoute(100)
            .setConnectionTimeout(2000)
            .build();

But any Rest Client implementation will be fine. OkHttp is a good choice.

I'm unable to share the full code since it's for internal use at my company, but if you have any other questions just let me know.

@adamoutler
Copy link

adamoutler commented Jul 17, 2018 via email

@adamoutler
Copy link

adamoutler commented Jul 18, 2018

OK. Here's my solution. No additional dependencies were needed.

Add Method to me.ramswaroop.jbot.core.slack.SlackApiEndpoints

    public String getUserConnectApi() {
        return slackApi + "/users.info?token={token}";  //literaly copy and paste this. it will put the token in by itself.
    }

Add Class UserResponse.java, anywhere.

public class UserResponse {

    private boolean ok;
    private User user;

    /**
     * @return the ok
     */
    public boolean isOk() {
        return ok;
    }

    /**
     * @return the user
     */
    public User getUser() {
        return user;
    }
}

Create a method

    private User getUser(Event event){
        UserResponse userResponse= new RestTemplate()
                .getForEntity(slackApiEndpoints
                        .getUserConnectApi()+"&user="+event.getUserId(), 
                        UserResponse.class, slackToken)
                .getBody();
        return userResponse.getUser();
    }

and access that with

String username=getUser(event).getName();

@adamoutler
Copy link

String displayName=u.getProfile().getRealName();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants