Auties00 / Cobalt

Standalone unofficial fully-featured Whatsapp Web and Mobile API for Java and Kotlin
MIT License
616 stars 180 forks source link

Invalid response #263

Closed marekhudyma closed 1 year ago

marekhudyma commented 1 year ago

I use:

      <dependency>
            <groupId>com.github.auties00</groupId>
            <artifactId>whatsappweb4j</artifactId>
            <version>3.3.1</version>
        </dependency>

The code worked for me, but after third (or fourth) run of the code:

Whatsapp.mobileBuilder()
                .newConnection()
                .unregistered()
                .register(${MY_PHONE}, () -> {
                    System.out.println("Enter OTP: ");
                    return new Scanner(System.in).nextLine().trim();
                })
                .join()
                .addLoggedInListener(() -> System.out.println("Connected"))
                .addDisconnectedListener(reason -> System.out.printf("Disconnected: %s%n", reason))
                .addListener(new Listener() {
                    @Override
                    public void onNewMessage(Whatsapp whatsapp, MessageInfo info) {
                        System.out.println("----");
                        if (info.message().content() instanceof TextMessage textMessage) {
                            System.out.println("xxx" + textMessage.text().toLowerCase());
                        }
                    }
                })
                .connect()
                .join();

I got an exception:

Exception in thread "main" java.util.concurrent.CompletionException: java.lang.IllegalArgumentException: Invalid response, status code 200: {"email_otp_wait":0,"flash_type":0,"login":"48500168000","reason":"too_recent","retry_after":753,"sms_wait":753,"status":"fail","voice_wait":0,"wa_old_wait":0}

    at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:315)
    at java.base/java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:320)
    at java.base/java.util.concurrent.CompletableFuture$UniAccept.tryFire$$$capture(CompletableFuture.java:722)
    at java.base/java.util.concurrent.CompletableFuture$UniAccept.tryFire(CompletableFuture.java)
    at java.base/java.util.concurrent.CompletableFuture$Completion.exec(CompletableFuture.java:483)
    at java.base/java.util.concurrent.ForkJoinTask.doExec$$$capture(ForkJoinTask.java:373)
    at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java)
    at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)
    at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)
    at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)
    at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)
Caused by: java.lang.IllegalArgumentException: Invalid response, status code 200: {"email_otp_wait":0,"flash_type":0,"login":"48500168000","reason":"too_recent","retry_after":753,"sms_wait":753,"status":"fail","voice_wait":0,"wa_old_wait":0}

    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at it.auties.whatsapp.util.RegistrationHelper.checkResponse(RegistrationHelper.java:81)
    at java.base/java.util.concurrent.CompletableFuture$UniAccept.tryFire$$$capture(CompletableFuture.java:718)
    ... 8 more
Auties00 commented 1 year ago

That is normal, you can't request the otp every time or you'll get rate limited on that number. Use registered() instead on unregistered() if you are sure that the number is registered already. I have changed the behaviour of unregistered to not ask an otp if you are registered on the main branch, but I haven't pushed out a release with this patch yet

marekhudyma commented 1 year ago

1) in ideal word, the library should not throw an exception for it, but handle and inform me politly. That's why I created an issue. 2) What does it mean that my number is registered. I passed the code and I run it again with .registered(), but I still have Exception in thread "main" java.lang.IllegalStateException: Expected session to be already registered. I use phone number that never got WhatsApp installed as an application. Is it the reason?

Auties00 commented 1 year ago

1) in ideal word, the library should not throw an exception for it, but handle and inform me politly. That's why I created an issue. 2) What does it mean that my number is registered. I passed the code and I run it again with .registered(), but I still have Exception in thread "main" java.lang.IllegalStateException: Expected session to be already registered. I use phone number that never got WhatsApp installed as an application. Is it the reason?

The problem is that you are using new connection() which will create a new session, that's why it's throwing an error. Use lastConnection() or knownConnection(UUID): a new connection will obviously be unregistered. Also the exception is thrown because the response is unexpected. Sure I could probably word the exception better, but it shouldn't be a log warning most definitely.

marekhudyma commented 1 year ago

I used this code:


        Whatsapp.mobileBuilder()
                .lastConnection()
                .registered()
                .addLoggedInListener(() -> System.out.println("Connected"))
                .addDisconnectedListener(reason -> System.out.printf("Disconnected: %s%n", reason))
                .addListener(new Listener() {
                    @Override
                    public void onNewMessage(Whatsapp whatsapp, MessageInfo info) {
                        System.out.println("----");
                        if (info.message().content() instanceof TextMessage textMessage) {
                            System.out.println("xxx" + textMessage.text().toLowerCase());
                            whatsapp.sendMessage(info.chatJid(), "Hello ! ");
                        }
                    }
                })
                .connect()
                .join();

And this is throwing: Exception in thread "main" java.lang.IllegalStateException: Expected session to be already registered. I do not understand, how the library should know about connection number/id? Does it save it somewhere?

PS. I send you an email some days ago with proposal to improve examples ;)

Auties00 commented 1 year ago

I used this code:


        Whatsapp.mobileBuilder()
                .lastConnection()
                .registered()
                .addLoggedInListener(() -> System.out.println("Connected"))
                .addDisconnectedListener(reason -> System.out.printf("Disconnected: %s%n", reason))
                .addListener(new Listener() {
                    @Override
                    public void onNewMessage(Whatsapp whatsapp, MessageInfo info) {
                        System.out.println("----");
                        if (info.message().content() instanceof TextMessage textMessage) {
                            System.out.println("xxx" + textMessage.text().toLowerCase());
                            whatsapp.sendMessage(info.chatJid(), "Hello ! ");
                        }
                    }
                })
                .connect()
                .join();

And this is throwing: Exception in thread "main" java.lang.IllegalStateException: Expected session to be already registered. I do not understand, how the library should know about connection number/id? Does it save it somewhere?

PS. I send you an email some days ago with proposal to improve examples ;)

Read the documentation about how serialization works. The reason why that's popping up is that it's using the last connection you created, so make sure that it's registered.

Auties00 commented 1 year ago

Also I can't find your email

marekhudyma commented 1 year ago

I send an email to the one from your profile: al..@g...com at 6th of April, subject "Project - WhatsApp4j". Check your spam ;)