SOChat4j is a general-purpose Java library for interacting with Stack Overflow Chat and its sister chat sites.
- Java 17
- Maven (for building)
var email = "email@example.com";
var password = "password";
try (IChatClient client = ChatClient.connect(Site.STACKOVERFLOW, email, password)) {
var room = client.joinRoom(1);
room.addEventListener(UserEnteredEvent.class, event -> {
try {
room.sendMessage("Welcome, " + event.getUsername() + "!");
} catch (RoomPermissionException | IOException e) {
e.printStackTrace();
}
});
room.addEventListener(UserLeftEvent.class, event -> {
try {
room.sendMessage("Bye, " + event.getUsername() + "!");
} catch (RoomPermissionException | IOException e) {
e.printStackTrace();
}
});
room.sendMessage("WelcomeBot Online!");
System.out.println("Press Enter to terminate the bot.");
try (var reader = new BufferedReader(new InputStreamReader(System.in))) {
reader.readLine();
}
room.sendMessage("Leaving, bye!");
} catch (InvalidCredentialsException e) {
System.err.println("Login credentials invalid.");
} catch (RoomNotFoundException e) {
System.err.println("Room not found.");
} catch (PrivateRoomException e) {
System.err.println("Cannot join room because it is private.");
} catch (IOException e) {
e.printStackTrace();
}
This project uses Maven and adheres to its conventions.
To build the project, run the command below.
mvn package
Please submit an issue on the issue tracker.