/sochat4j

General-purpose Java library for interacting with Stack Overflow Chat and its sister chat sites.

Primary LanguageHTML

SOChat4j

SOChat4j is a general-purpose Java library for interacting with Stack Overflow Chat and its sister chat sites.

Requirements

  • Java 17
  • Maven (for building)

Example

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();
}

More sample code

Build Instructions

This project uses Maven and adheres to its conventions.

To build the project, run the command below.

mvn package

Questions/Feedback

Please submit an issue on the issue tracker.