Skip to content

Retrieving user data and sending PM's

Eddie Penta edited this page Jun 5, 2013 · 6 revisions

##Notes before using the User API Before you get started on using the User API within HippyJava, please make sure your bot has a valid API key in the apiKey() method. The User API uses Hipchat's HTTP API to retrieve the user data, and if your bot does not have a valid API Key, then an empty array of HipchatUsers will be returned

##Getting user data Getting user data is really simple. First invoke the "findUser" method like so:

HipchatUser user = super.findUser(nickname);

You can use the findUser method in the recieveMessage method like so:

@Override
public void recieveMessage(String message, String from, Room room) {
    HipchatUser user = super.findUser(from);
    System.out.println("Email: " + user.getEmail());
    System.out.println("@mention name: " + user.getMentionName());
    System.out.println("Status: " + user.getStatus());
    System.out.println("Status Message: " + user.getStatusMessage());
    System.out.println("Title: " + user.getTitle());
    System.out.println("Account Deleted?: " + user.isDeletedAccount());
    System.out.println("Group Admin?: " + user.isGroupAdmin());
    System.out.println("Profile Photo URL: " + user.getPhotoUrl());
    System.out.println("User ID: " + user.getUserID());
    BufferedImage image = user.getProfilePhoto();
}

To get a List of HipchatUser objects, you can invoke the method "getUsers". This method will return all the users in the same group as the bot. However, the list returned is an unmodifiable list, so attempting to add or remove from the list may result in an exception being thrown.

To use this method, simply do the following:

List<HipchatUser> user_list = super.getUsers();

##Sending Private Messages (PM's)

Sending a private message is very simple. Just invoke the sendPM method in your bot class and follow the documentation.

There are 2 versions of this method, the first one being:

super.sendPM("This is the message", "[email protected]");

This method accepts a message (String), and a JID. You can get the JID of a user by invoking the method

String jid = super.nickToJID("Bob Joe");

So the final piece of code should something look like this:

String jid = super.nickToJID("Bob Joe");
super.sendPM("This is the message to send", jid);

The second type is a convenience method to do the above for you. All you have to do is provide a HipchatUser object and the rest will be done for you. To get the HipchatUser object, please refer to above for the many ways to retrieve it.

Example:

HipchatUser user = super.findUser(from);
super.sendPM("This is the message to send", user);

All of the sendPM methods return a boolean that tells you whether the message was sent without any errors. If the method returns true, then the message was sent. If it returns false, then the message was not sent and an error occurred.

Clone this wiki locally