Louuke / WhatsJava

Implementation of the WhatsApp Web API in Java
Apache License 2.0
37 stars 16 forks source link

Get a list of chats #11

Closed micw closed 3 years ago

micw commented 3 years ago

Hello, how can I retrieve a list of all chats? Kind regards, Michael.

Louuke commented 3 years ago

Hey, I more or less stopped working on this project some time ago, so this is why there is no method to get a list of all chats by command. However, if you have successfully logged in, then you should get a WebChat array over the onWebChat callback, which is basically a list of chats. Each WebChat object contains information about a direct or group chat. You can find more detailed information in the README.

If you no longer have the WebChat array saved, you could simply open a new session.

I hope this helps.

micw commented 3 years ago

Hi, thank you. But I already tried this (and expected it after reading some implementations). But after login or session restore, I get "Conn", "Blocklist", "Stream" and "Props". Followed by

Logged in successfully! Code: 200
863.--1,
863.--2,
863.--3,
863.--4,
863.--5,
863.--6,
863.--7,
863.--4,{"status":499}
863.--5,{"status":499}
863.--7,{"status":200}

No sign of a chat list :-(

Louuke commented 3 years ago

I have now tried again with a copy from Github and still can't reproduce your problem. That's my complete main method:

public static void main(String[] args) {
    WAClient client = new WAClient("credentials.json");
    client.openConnection();
    client.addClientActionListener(new ClientActionListener() {

        @Override
        public void onReceiveLoginResponse(int httpCode) {
            if(httpCode == 200) {
                System.out.println("Logged in successfully! Code: " + httpCode);
            } else {
                System.out.println("Restore of previous session failed! Code: " + httpCode);
            }
        }

        @Override
        public void onQRCodeScanRequired(BufferedImage img) {
            System.out.println("Authentication required! Please scan the QR code!");
            saveQRCode(img);
        }

        @Override
        public void onWebChat(WebChat[] chats) {
            for(WebChat webChat : chats) {
                System.out.println(webChat.getName()); // Returns chat name
            }
            System.out.println("You have " + chats.length + " chats");
        }
    });
}

Console output without foreach loop:

Logged in successfully! Code: 200 487.--3, 487.--4, 487.--5, 487.--6, 487.--7, 487.--8, 487.--9, You have 20 chats 487.--6,{"status":499} 487.--7,{"status":499} 487.--9,{"status":200}

Update: It would also be interesting to know if binary messages are received in general. Have you tried the onWebContact callback? Alternatively you can look at the decrypted data in the WAClient class. You just need to output the json string in the onBinaryMessage method:

// ...
// Use protobuf to make messages of the type "message" human readable
BinaryDecoder decoder = new BinaryDecoder();
String json = decoder.decode(decrypted);
System.out.println(json);
// ...

I also made some minor changes to the project.

micw commented 3 years ago

I tried that, running 0.1.3-alpha from maven. The onWebChat method is never called. I got a lot of message and other callbacks.

Edit: tried with the latest snapshot from maven (8fabef1eb0). Same result.

Here's my code:

import java.awt.Desktop;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import icu.jnet.whatsjava.ClientActionInterface;
import icu.jnet.whatsjava.WAClient;
import icu.jnet.whatsjava.web.WebChat;
import icu.jnet.whatsjava.web.WebContact;
import icu.jnet.whatsjava.web.WebConversationMessage;
import icu.jnet.whatsjava.web.WebEmoji;
import icu.jnet.whatsjava.web.WebImageMessage;
import icu.jnet.whatsjava.web.WebStatus;
import icu.jnet.whatsjava.web.WebVideoMessage;

public class WhatsAppExporter implements ClientActionInterface {

    public static void main(String[] args) throws Exception {
        new WhatsAppExporter().start();
    }

    protected final WAClient client;
    public WhatsAppExporter() {
        client = new WAClient("auth.json");
        client.addClientActionListener(this);
    }

    public void start() {
        client.openConnection();
    }

    @Override
    public void onReceiveLoginResponse(int httpCode) {
        if(httpCode == 200) {
            System.out.println("Logged in successfully! Code: " + httpCode);

        } else {
            System.out.println("Login failed! Code: " + httpCode);
        }
    }

    @Override
    public void onQRCodeScanRequired(BufferedImage img) {
        System.err.println(img.getWidth()+" x "+img.getHeight());
        File outputfile = new File("image.png");
        try {
            ImageIO.write(img, "png", outputfile);
            Desktop.getDesktop().open(outputfile);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    @Override
    public void onWebConversationMessage(WebConversationMessage conversationMessage) {
//      System.err.println("Message: "+conversationMessage.getRemoteJid());
    }

    @Override
    public void onWebImageMessage(WebImageMessage imageMessage) {
//      System.err.println("Image: "+imageMessage.getRemoteJid());
    }

    @Override
    public void onWebVideoMessage(WebVideoMessage videoMessage) {
//      System.err.println("Video: "+videoMessage.getRemoteJid());
    }

    @Override
    public void onWebChat(WebChat[] chats) {
        for (WebChat chat: chats) {
            System.err.println("Chat: "+chat);
        }
        System.err.println(String.format("You have %s chats",chats.length));
    }

    @Override
    public void onWebContact(WebContact[] contacts) {
        for (WebContact contact: contacts) {
            System.err.println("Contact: "+contact);
        }
    }

    @Override
    public void onWebStatus(WebStatus[] status) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onWebEmoji(WebEmoji[] emojis) {
        // TODO Auto-generated method stub

    }

}
micw commented 3 years ago

Seems to be an issue with json->object conversion:

["action", null, [["contacts", {"type":"frequent"}, [["message", {"jid":"49xxx@c.us"}, null], ...
java.lang.IllegalStateException: Not a JSON Object: null
    at com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:91)
["response", {"duplicate":"true","type":"contacts"}, null]
java.lang.IllegalStateException: Not a JSON Array: null
    at com.google.gson.JsonElement.getAsJsonArray(JsonElement.java:107)

Still seems not to receive any "chats" events but at least "frequent contacts"

Louuke commented 3 years ago

Does it also say in which class and line the error occurs? Can you please give me the entire error?

micw commented 3 years ago

Oh sorry, missed this. Here it is:

java.lang.IllegalStateException: Not a JSON Object: null
    at com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:91)
    at icu.jnet.whatsjava.WAMessage.jsonToObject(WAMessage.java:59)
    at icu.jnet.whatsjava.WAClient.onBinaryMessage(WAClient.java:289)
    at com.neovisionaries.ws.client.ListenerManager.callOnBinaryMessage(ListenerManager.java:385)
    at com.neovisionaries.ws.client.ReadingThread.callOnBinaryMessage(ReadingThread.java:276)
    at com.neovisionaries.ws.client.ReadingThread.handleBinaryFrame(ReadingThread.java:996)
    at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:755)
    at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:108)
    at com.neovisionaries.ws.client.ReadingThread.runMain(ReadingThread.java:64)
    at com.neovisionaries.ws.client.WebSocketThread.run(WebSocketThread.java:45)

It tries to decode the attributes of the null action.

Louuke commented 3 years ago

Okay, the exception should be fixed now and you can get the build from JitPack (Version / Commit: d92bc676e8) However, I doubt that this is the problem causing the missing WebChat objects.