rampatra / jbot

Make Slack and Facebook Bots in Java.
GNU General Public License v3.0
1.2k stars 349 forks source link

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

Open AtbTheGreat opened 7 years ago

AtbTheGreat commented 7 years ago

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 commented 7 years ago

@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 commented 7 years ago

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

adamoutler commented 6 years ago

@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 commented 6 years ago

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 commented 6 years ago

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 notifications@github.com 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 commented 6 years ago

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 commented 6 years ago

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