Skip to content

Commit c96e3ba

Browse files
authored
Update README.md
1 parent f0c0d2d commit c96e3ba

File tree

1 file changed

+87
-53
lines changed

1 file changed

+87
-53
lines changed

README.md

Lines changed: 87 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ WhatsJava is a reimplementation of the WhatsApp Web API and provides a direct in
33

44
Big thanks to [@Sigalor](https://github.com/sigalor) and all participants of the [whatsapp-web-reveng project](https://github.com/sigalor/whatsapp-web-reveng) and [@adiwajshing](https://github.com/adiwajshing/Baileys) for the Typescript/Javascript implementation.
55

6-
This project is **WIP** and things could break in the future.
7-
86
### Gradle
97
```java
108
allprojects {
@@ -22,26 +20,33 @@ dependencies {
2220
Find more options here: **[Jitpack](https://jitpack.io/#JicuNull/WhatsJava)**
2321

2422
## How to use it
25-
**Note**: The project is tested and compiled with Java 11
23+
**Note**: The project is tested with Java 11
2624

27-
### Connect and login
25+
## Connect and login
2826
`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.
2927

30-
A path to a storage location is also required to store the keys needed to reestablish a session.
3128
```java
32-
WAClient client = new WAClient("auth.json");
29+
WAClient client = new WAClient();
3330
```
3431
The session is opened by calling `openConnection()`.
3532
```java
36-
WAClient client = new WAClient("auth.json");
33+
WAClient client = new WAClient();
3734
client.openConnection();
3835
```
39-
After a session has been successfully initialized, a qr code must be scanned from the device where WhatsApp is running on. To learn how to receive the qr code please refer to "Message handlers".
36+
You can also define a path to a storage location to store keys needed to reestablish a session.
37+
```java
38+
client.setCredentialsPath("credentials.json"); // Optional - It defaults to "credentials.json"
39+
```
40+
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:
41+
```java
42+
client.setPrintQRCode(true);
43+
```
44+
To learn how to receive the qr code as BufferedImage please refer to "Message handlers".
4045

41-
### Message handlers
46+
## Message handlers
4247
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.
4348

44-
- Called when a new qr code has been generated and has to be scanned for login. A qr code has a lifetime of 20 seconds, which is why it can be generated up to five times.
49+
- Called when a new qr code has been generated and has to be scanned for login. A qr code has a lifetime of 20 second sand up to 5 five new ones can be created.
4550
```java
4651
client.addClientActionListener(new ClientActionListener() {
4752
@Override
@@ -57,7 +62,7 @@ client.addClientActionListener(new ClientActionListener() {
5762
@Override
5863
public void onReceiveLoginResponse(int httpCode) {
5964
if(httpCode == 200) {
60-
System.out.println("Logged in successfully! Code: " + httpCode);
65+
System.out.println("Logged in successfully!);
6166
} else {
6267
System.out.println("Login failed! Code: " + httpCode);
6368
}
@@ -66,52 +71,37 @@ public void onReceiveLoginResponse(int httpCode) {
6671
- Other callbacks defined in `ClientActionInterface` interface:
6772
```java
6873
@Override
69-
public void onWebChat(WebChat[] chats) {
74+
public void onWAMessage(WAMessage[] waMessage) {
7075
// ...
7176
}
7277
7378
@Override
74-
public void onWebContact(WebContact[] contacts) {
79+
public void onWAChat(WAChat[] chats) {
7580
// ...
7681
}
7782
7883
@Override
79-
public void onWebEmoji(WebEmoji[] emojis) {
84+
public void onWAContact(WAContact[] contacts) {
8085
// ...
8186
}
8287
8388
@Override
84-
public void onWebStatus(WebStatus[] status) {
85-
// ...
86-
}
87-
88-
@Override
89-
public void onWebConversationMessage(WebConversationMessage conversationMessage) {
90-
// ...
91-
}
92-
93-
@Override
94-
public void onWebImageMessage(WebImageMessage imageMessage) {
95-
// ...
96-
}
97-
98-
@Override
99-
public void onWebVideoMessage(WebVideoMessage videoMessage) {
89+
public void onWAEmoji(WAEmoji[] emojis) {
10090
// ...
10191
}
10292
```
103-
### Send text message
93+
## Sending messages
10494
```java
10595
String remoteJid = "0123456789@s.whatsapp.net";
10696
10797
client.sendMessage(remoteJid, "Hello World");
10898
```
10999
**Note**:
110-
- WhatsApp identifies a person or a group with a unique chat identification - `jid`.
100+
- WhatsApp identifies a person or a group with a unique chat identification called `jid`.
111101
- Chats: `[country code][phone number]@s.whatsapp.net`, for example `490123456789@s.whatsapp.net`
112102
- Groups: `[phone number of group creator]-[timestamp of group creation]@g.us`, e.g. `490123456789-1596766695@g.us`
113103
114-
### Delete sent message
104+
## Deleting messages
115105
Lets you delete a sent message for yourself
116106
```java
117107
String remoteJid = "0123456789@s.whatsapp.net";
@@ -121,20 +111,68 @@ boolean owner = true;
121111
client.clearMessage(remoteJid, messageId, owner);
122112
```
123113
124-
### Load conversation
114+
## Loading messages
125115
Queries the chat history of a conversation
126116
```java
127117
// Query the last 25 messages
128-
client.loadConversation(remoteJid, 25);
118+
WAMessage[] waMessages = client.loadChatHistory(remoteJid, 25);
129119
```
130120
Queries the chat history after a certain message
131121
```java
132122
// Query the last 25 messages after the message with the id x, which I'm not the owner of
133-
client.loadConversation(remoteJid, 25, "4C739872E8K15F7L0ACB", false);
123+
WAMessage[] waMessages = client.loadChatHistory(remoteJid, 25, "4C739872E8K15F7L0ACB", false);
124+
```
125+
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.
126+
For example:
127+
```java
128+
WAMessage[] waMessages = client.loadChatHistory("490123456789@s.whatsapp.net", 25);
129+
for(WAMessage message : waMessages) {
130+
if(message.hasConversationMessage()) {
131+
System.out.println(message.getConversationMessage().getText());
132+
} else if(message.hasImageMessage()) {
133+
BufferedImage img = message.getImageMessage().getJpegFullResolution();
134+
// ...
135+
}
136+
}
137+
```
138+
139+
## Loading chats
140+
Loads direct and group chats
141+
```java
142+
WAChat[] waChats = client.loadChats();
134143
```
135144
136-
### Objects
137-
- **WebChat**: Contains information about direct chats or groups
145+
## Loading contacts
146+
Queries your WhatsApp contacts
147+
```java
148+
WAContact[] waContacts = client.loadContacts();
149+
```
150+
151+
## Misc
152+
To get recently used emojis and their relative frequency of use
153+
```java
154+
WAEmoji[] waEmojis = client.loadEmojis();
155+
```
156+
To get the displayed picture of some person or group
157+
```java
158+
BufferedImage img = client.getChatPicture("abc@c.us");
159+
```
160+
To get the status of some person
161+
```java
162+
String status = client.getStatus("abc@c.us");
163+
```
164+
To get someone's presence (if they're typing, online)
165+
```java
166+
String presence = client.requestPresenceUpdate("abc@c.us");
167+
```
168+
To set your global presence status
169+
```java
170+
client.updatePresence(Presence.AVAILABLE);
171+
// Others: Presence.UNAVAILABLE, Presence.COMPOSING, Presence.RECORDING, Presence.PAUSED
172+
```
173+
174+
## Objects
175+
- **WAChat**:
138176
139177
| Method | Description |
140178
| ------------- |---------------|
@@ -144,30 +182,21 @@ client.loadConversation(remoteJid, 25, "4C739872E8K15F7L0ACB", false);
144182
| getLastInteraction | Returns the timestamp of the last message |
145183
| isMuted | Returns if the chat is muted |
146184
147-
- **WebContact**: Contains information about direct chats or groups
185+
- **WAContact**:
148186
149187
| Method | Description |
150188
| ------------- |---------------|
151189
| getJid | Returns unique chat identification |
152190
| getName | Returns the name your contact gave to WhatsApp |
153191
154-
- **WebEmoji**: Contains information about the emojis you used the most
192+
- **WAEmoji**:
155193
156194
| Method | Description |
157195
| ------------- |---------------|
158196
| getCode | Returns emoji code |
159197
| getValue | Returns the frequency of use |
160198
161-
- **WebStatus**: Contains a WebImageMessage or WebVideoMessage
162-
163-
| Method | Description |
164-
| ------------- |---------------|
165-
| isWebImageMessage | Returns `true` if it contains a WebImageMessage |
166-
| isWebVideoMessage | Returns `true` if it contains a WebVideoMessage |
167-
| getWebImageMessage | Returns WebImageMessage |
168-
| getWebVideoMessage | Returns WebVideoMessage |
169-
170-
- **WebConversationMessage**: Contains information about generic text messages
199+
- **WAConversationMessage**:
171200
172201
| Method | Description |
173202
| ------------- |---------------|
@@ -181,7 +210,7 @@ client.loadConversation(remoteJid, 25, "4C739872E8K15F7L0ACB", false);
181210
| ------------- |---------------|
182211
| getText | Returns quote |
183212
184-
- **WebImageMessage**: Contains information about image messages
213+
- **WAImageMessage**:
185214
186215
| Method | Description |
187216
| ------------- |---------------|
@@ -191,7 +220,7 @@ client.loadConversation(remoteJid, 25, "4C739872E8K15F7L0ACB", false);
191220
| getJpegFullResolution | Returns the image with full resolution or null if it could not be loaded |
192221
| ... | ... |
193222
194-
- **WebVideoMessage**: Contains information about video messages
223+
- **WAVideoMessage**:
195224
196225
| Method | Description |
197226
| ------------- |---------------|
@@ -201,7 +230,7 @@ client.loadConversation(remoteJid, 25, "4C739872E8K15F7L0ACB", false);
201230
| getMp4FullResolution | Returns the video with full resolution or null if it could not be loaded |
202231
| ... | ... |
203232
204-
- **WebMessage**: Each Web[Type]Message class extends WebMessage and therefore contains the following methods
233+
- **WAMessage**:
205234
206235
| Method | Description |
207236
| ------------- |---------------|
@@ -210,6 +239,11 @@ client.loadConversation(remoteJid, 25, "4C739872E8K15F7L0ACB", false);
210239
| getFromMe | Returns `true` if the message is from you |
211240
| getMessageTimestamp | Returns the message timestamp |
212241
| getStatus | Returns the status of the message |
242+
| getImageMessage |
243+
| getConversationMessage |
244+
| getVideoMessage |
245+
| getStubMessage |
246+
| ... | ... |
213247
214248
**Note**: Audio and document messages are not implemented
215249

0 commit comments

Comments
 (0)