Louuke / WhatsJava

Implementation of the WhatsApp Web API in Java
Apache License 2.0
37 stars 16 forks source link
java java11 reverse-engineering whatsapp whatsapp-api whatsapp-web whatsapp-web-api whatsappweb

! Note !

This project is broken and I will most likely no longer maintain it. I suggest to switch to the project WhatsappWeb4j.

WhatsJava

WhatsJava is a reimplementation of the WhatsApp Web API and provides a direct interface for developers.

Big thanks to @Sigalor and all participants of the whatsapp-web-reveng project and @adiwajshing for the Typescript/Javascript implementation.

Gradle

allprojects {
    repositories {
    ...
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
    implementation 'com.github.JicuNull:WhatsJava:v1.0.2'
}

Find more options here: Jitpack

How to use it

Note: The project is tested with Java 11

Connect and login

WAClient connects to the WhatsApp backend server and is used for all interactions with the server. Therefore, a new WAClient instance needs to be created first.

WAClient client = new WAClient();

The session is opened by calling openConnection().

WAClient client = new WAClient();
client.openConnection();

You can also define a path to a storage location to store keys needed to reestablish a session.

client.setCredentialsPath("credentials.json"); // Optional - It defaults to "credentials.json"

After a session has been successfully initialized, a qr code must be scanned from the device where WhatsApp is running on. To print a QR code in the console, call the setPrintQRCode() method:

client.setPrintQRCode(true);

To learn how to receive the qr code as BufferedImage please refer to "Message handlers".

Complete example

WAClient client = new WAClient();
client.setPrintQRCode(true);
client.addClientActionListener(new ClientActionListener() {
    @Override
    public void onQRCodeScanRequired(BufferedImage img) {
        System.out.println("Authentication required! Please scan the QR code!");
    }
});
int httpCode = client.openConnection();
if(httpCode == 200) {
    System.out.println("Logged in successfully!");
    System.out.println("You have " + client.loadChats().length + " chats");
} else {
    System.out.println("Login failed! Code: " + httpCode);
}

Message handlers

After a WAClient instance is initialized, the addClientActionListener method can be called to register a ClientActionInterface and receive a number of callbacks. ClientActionListener is an empty implementation of ClientActionInterface interface.

@Override public void onWAChat(WAChat[] chats) { // ... }

@Override public void onWAContact(WAContact[] contacts) { // ... }

@Override public void onWAEmoji(WAEmoji[] emojis) { // ... }

## Sending messages
```java
String remoteJid = "0123456789@s.whatsapp.net";

client.sendMessage(remoteJid, "Hello World");

Note:

Deleting messages

Lets you delete a sent message for yourself

String remoteJid = "0123456789@s.whatsapp.net";
String messageId = "3EM04B5BA7A366D3F9AE";
boolean owner = true;

client.clearMessage(remoteJid, messageId, owner);

Loading messages

Queries the chat history of a conversation

// Query the last 25 messages
WAMessage[] waMessages = client.loadChatHistory(remoteJid, 25);

Queries the chat history after a certain message

// Query the last 25 messages after the message with the id x, which I'm not the owner of
WAMessage[] waMessages = client.loadChatHistory(remoteJid, 25, "4C739872E8K15F7L0ACB", false);

When you query the messages of a chat, you get an array of WAMessage objects, which in turn can contain different types of message types. To determine which type it is, the methods waMessage.hasImageMessage(), waMessage.hasVideoMessage(), waMessage.hasConversationMessage() and waMessage.hasStubMessage() can be used. For example:

WAMessage[] waMessages = client.loadChatHistory("490123456789@s.whatsapp.net", 25);
for(WAMessage message : waMessages) {
    if(message.hasConversationMessage()) {
        System.out.println(message.getConversationMessage().getText());
    } else if(message.hasImageMessage()) {
        BufferedImage img = message.getImageMessage().getJpegFullResolution();
        // ...
    }
}

Loading chats

Loads direct and group chats

WAChat[] waChats = client.loadChats();

Loading contacts

Queries your WhatsApp contacts

WAContact[] waContacts = client.loadContacts();

Misc

To get recently used emojis and their relative frequency of use

WAEmoji[] waEmojis = client.loadEmojis();

To get the displayed picture of some person or group

BufferedImage img = client.getChatPicture("abc@c.us");

To get the status of some person

String status = client.getStatus("abc@c.us");

To get someone's presence (if they're typing, online)

String presence = client.requestPresenceUpdate("abc@c.us");

To set your global presence status

client.updatePresence(Presence.AVAILABLE);
// Others: Presence.UNAVAILABLE, Presence.COMPOSING, Presence.RECORDING, Presence.PAUSED

Objects

Method Description
getJid Returns unique chat identification
getName Returns your given contact name
getUnreadMessages Returns the amount of unread messages
getLastInteraction Returns the timestamp of the last message
isMuted Returns if the chat is muted
Method Description
getJid Returns unique chat identification
getName Returns the name your contact gave to WhatsApp
Method Description
getCode Returns emoji code
getValue Returns the frequency of use
Method Description
getText Returns content of the message
hasQuotedTextMessage Returns true if it contains a QuotedTextMessage
getQuotedTextMessage Returns QuotedTextMessage
Method Description
getText Returns quote
Method Description
getMimetype Returns mimetype of the image
getCaption Returns caption
getJpegThumbnail Returns a thumbnail of the image
getJpegFullResolution Returns the image with full resolution or null if it could not be loaded
... ...
Method Description
getMimetype Returns mimetype of the video
getSeconds Returns video length
getMp4Thumbnail Returns a thumbnail of the video
getMp4FullResolution Returns the video with full resolution or null if it could not be loaded
... ...
Method Description
getRemoteJid Returns unique chat identification
getId Returns the id of a message
getFromMe Returns true if the message is from you
getMessageTimestamp Returns the message timestamp
getStatus Returns the status of the message
getImageMessage
getConversationMessage
getVideoMessage
getStubMessage
... ...

Note: Audio and document messages are not implemented

Legal

This code is in no way affiliated with, authorized, maintained, sponsored or endorsed by WhatsApp or any of its affiliates or subsidiaries. This is an independent and unofficial software. Use at your own risk.