-
Notifications
You must be signed in to change notification settings - Fork 9
Programming and Running a basic bot
To start programming your first HippyJava bot, you must create a class and extend the HippyBot class.
import com.ep.hippyjava.bot.HippyBot;
import com.ep.hippyjava.model.Room;
public class MyTestBot extends HippyBot {
@Override
public void receiveMessage(String message, String from, Room room) {
}
@Override
public String username() {
return "jid";
}
@Override
public String password() {
return "password";
}
@Override
public void onLoad() {
}
@Override
public String nickname() {
return "nickname";
}
@Override
public String apiKey() {
return "apiKey";
}
}
Each method has its own javadoc, but I'll go over each one.
@Override
public void receiveMessage(String message, String from, Room room) {
}
This method is called whenever the bot receives a new message. It provides the body of the message (message), who sent the message (from), and the room the message came from (room).
@Override
public String username() {
return "jid";
}
This method contains the JID that you'r bot will use. You can find the JID of your account in your profile.
@Override
public String password() {
return "password";
}
This method contains the password for the bot's account.
@Override
public void onLoad() {
}
This method is called after the bot connects and has successfully logged into the server. You can use this method to join a room, or load/register additional data.
@Override
public String nickname() {
return "nickname";
}
This method contains the nickname for your bot. Unlike your username, your nickname is the display name that users will see in hipchat. This should reflect whats in your profile, and if its wrong, the exception will tell you which nickname to use.
@Override
public String apiKey() {
return "apiKey";
}
This method contains an API key to use when using the HTTP API. While the API Key is not needed for a bot to run correctly, it IS required if you plan to send notifications to certain rooms. The API Key can also help the bot retrieve certain data about a room, however, most of these functions can also be done using XMPP so the library will fallback to using those functions if an API Key is not present.
Running your bot is simple, simply create a normal main method in java and insert the following code.
HippyJava.runBot(new MyBotClass());
So your entire main mehod should look something like this.
public static void main(String[] args) {
HippyJava.runBot(new MyTestBot());
}
When you read the javadoc for the runBot method, it states:
This method will block until the bot has been disconnected from the server
This means, if you have code after the runBot method in your main method, then that code will not execute until your bot disconnects. For example:
public static void main(String[] args) {
HippyJava.runBot(new MyTestBot()); //Run my bot
int i = 1 + 1; //This code wont execute until my bot disconnects
System.out.println(i); //This code wont execute until my bot disconnects
}
To avoid this problem, you can invoke runBotDesync, which will run your bot inside a different thread. If you wish to do this, then your main method will look like this.
public static void main(String[] args) {
HippyJava.runBotDesync(new MyTestBot());
}
This will allow the code after this to be executed as well.