jbot icon indicating copy to clipboard operation
jbot copied to clipboard

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

Open AtbTheGreat opened this issue 8 years ago • 7 comments

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.

AtbTheGreat avatar Jul 20 '17 23:07 AtbTheGreat

@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.

mndominguez avatar Aug 24 '17 13:08 mndominguez

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

jjenksy avatar Oct 21 '17 19:10 jjenksy

@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?

adamoutler avatar Jul 16 '18 23:07 adamoutler

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.

mndominguez avatar Jul 17 '18 15:07 mndominguez

Thank you. I was banging my head on this for the weekend. I will share my code when finished. I have no problem since it is open and for a gamer team.

On Tue, Jul 17, 2018, 11:11 mndominguez [email protected] wrote:

Hey @adamoutler https://github.com/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.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/rampatra/jbot/issues/64#issuecomment-405617721, or mute the thread https://github.com/notifications/unsubscribe-auth/AAgOL1_ZaDahDPFo7u6m4-WtXoEdtCwoks5uHf6NgaJpZM4OezNI .

adamoutler avatar Jul 17 '18 19:07 adamoutler

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 avatar Jul 18 '18 00:07 adamoutler

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

adamoutler avatar Jul 18 '18 01:07 adamoutler