项目作者: kamax-matrix

项目描述 :
Matrix Java SDK
高级语言: Java
项目地址: git://github.com/kamax-matrix/matrix-java-sdk.git
创建时间: 2017-04-26T22:26:48Z
项目社区:https://github.com/kamax-matrix/matrix-java-sdk

开源协议:GNU Affero General Public License v3.0

下载


Matrix Client SDK for Java

Build Status


This project is no longer maintained.


Purpose

Matrix SDK in Java 1.8 for:

  • Client -> Homeserver
  • Client -> Identity Server
  • Application Server -> Homeserver

Use

Add to your project

Gradle

  1. repositories {
  2. maven {
  3. url 'https://kamax.io/maven/releases/'
  4. }
  5. }
  6. dependencies {
  7. compile 'io.kamax:matrix-java-sdk:<USE_LATEST_TAG_WITHOUT_LEADING_V>'
  8. }

Maven

  1. <repositories>
  2. <repository>
  3. <id>kamax-io</id>
  4. <name>kamax-io</name>
  5. <url>https://kamax.io/maven/releases/</url>
  6. </repository>
  7. </repositories>
  8. <dependencies>
  9. <dependency>
  10. <groupId>io.kamax</groupId>
  11. <artifactId>matrix-java-sdk</artifactId>
  12. <version>USE_LATEST_TAG_WITHOUT_LEADING_V</version>
  13. </dependency>
  14. </dependencies>

WARNING: This SDK was originally created to support Kamax.io projects and is
therefore not necessarily complete. It will be built as the various projects evolve and grow. The SDK is therefore still
in Alpha.

Getting started

Getting the client object

With .well-known auto-discovery:

  1. _MatrixClient client = new MatrixHttpClient("example.org");
  2. client.discoverSettings();

With C2S API Base URL:

  1. URL baseUrl = new URL("https://example.org");
  2. _MatrixClient client = new MatrixHttpClient(baseUrl);

Providing credentials

Access token:

  1. client.setAccessToken(accessToken);

Log in:

  1. client.login(new MatrixPasswordCredentials(username, password));

Sync

  1. // We will update this after each sync call
  2. String syncToken = null;
  3. // We sync until the process is interrupted via Ctrl+C or a signal
  4. while (!Thread.currentThread().isInterrupted()) {
  5. // We provide the next batch token, or null if we don't have one yet
  6. _SyncData data = client.sync(SyncOptions.build().setSince(syncToken).get());
  7. // We check the joined rooms
  8. for (JoinedRoom joinedRoom : data.getRooms().getJoined()) {
  9. // We get the relevant room object to act on it while we process
  10. _Room room = client.getRoom(joinedRoom.getId());
  11. for (_MatrixEvent rawEv : joinedRoom.getTimeline()) {
  12. // We only want to act on room messages
  13. if ("m.room.message".contentEquals(rawEv.getType())) {
  14. MatrixJsonRoomMessageEvent msg = new MatrixJsonRoomMessageEvent(rawEv.getJson());
  15. // Ping?
  16. if (StringUtils.equals("ping", msgg.getBody())) {
  17. // Pong!
  18. room.sendText("pong");
  19. }
  20. }
  21. }
  22. }
  23. // We check the invited rooms
  24. for (InvitedRoom invitedRoom : data.getRooms().getInvited()) {
  25. // We auto-join rooms we are invited to
  26. client.getRoom(invitedRoom.getId()).join());
  27. }
  28. // Done processing sync data. We save the next batch token for the next loop execution
  29. syncToken = data.nextBatchToken();
  30. }

As an Application Service

Use MatrixApplicationServiceClient instead of MatrixHttpClient when creating the main client object.

To talk to the API as a virtual user, use the method createClient(localpart) on MatrixApplicationServiceClient, then
processed normally.

Real-world usage

As a regular client

You can check the Send’n’Leave bot which make uses of this SDK in a more realistic fashion.
Direct link to the relevant code: here

As an Application Service

Contribute

Contributions and PRs are welcome to turn this into a fully fledged Matrix Java SDK.
Your code will be licensed under AGPLv3.

To ensure code formatting consistency, we use Spotless.
Before opening any PR, make sure you format the code:

  1. ./gradlew spotlessApply

Your code must pass all existing tests with and must provide tests for any new method/class/feature.
Make sure you run:

  1. ./gradlew test