Secreto31126 / whatsapp-api-js

A TypeScript server agnostic Whatsapp's Official API framework
MIT License
142 stars 33 forks source link

Error sending message: Error: Header image must not have a caption #198

Closed HysMX closed 1 year ago

HysMX commented 1 year ago

Describe the bug An error in console while trying to send a broadcast message

To Reproduce Steps to reproduce the behavior:

const image = new Image("https://i.imgur.com/RZ1MmvC.png")
const button = new Interactive(
  new ActionButtons(
    new Button("button_quote_offer", "Responder"),
    new Button("button_quote_ignore", "Ignorar")
  ),
  new Body(`*Nueva Solicitud*
*Cliente:* ${customer?.name}
*Solicitud:* ${prompt}
*Producto / Servicio:*: ${product}
*Categoría:* ${category}
*Presupuesto:* ${price}
  `),
  new Header(image)
);

const responses = await Whatsapp.broadcastMessage(PHONE_ID, recipients, button);

Promise.all(responses).then(console.log);    

Expected behavior Send the message with the image header

It was sending the message just fine until i updated the library to the latest version, the code is unmodified

HysMX commented 1 year ago

Downgraded to 1.0.2 and worked just fine image

Secreto31126 commented 1 year ago

This is a really weird issue. The diff between 1.0.2..1.0.3 only includes changes for template.ts (ignoring the modified .md files), so your code shouldn't be affected by the update...

I will test your snippet in a few hours and try to find the solution.

Thanks!

Secreto31126 commented 1 year ago

I managed to replicate the issue, and to my surprise it's REALLY weird.

Given this snippet:

const image = new Image("https://i.imgur.com/RZ1MmvC.png");
console.log(image);

The obtained output on 1.0.2 is:

{ link: 'https://i.imgur.com/RZ1MmvC.png' }

However, on 1.0.3 (and v2), the terminal logs the following:

{
  id: undefined,
  link: 'https://i.imgur.com/RZ1MmvC.png',
  caption: undefined
}

Which is really annoying, because the library uses the keyword in to check if the class contains such property. Although undefined, the property is in the object, hence throwing the error as it thinks the image contains a caption.

My only guess for this issue is a breaking dev dependecy update. I will try to find out which one and see if it's intentional or not. IIRC, esbuild had a mid version update, so maybe the issue is there.

This is why people write unit tests :)

Thanks for opening the issue.


Update

I unfortunately found the broken dependecy: esbuild@18

https://github.com/Secreto31126/whatsapp-api-js/pull/166 https://github.com/evanw/esbuild/issues/2993 https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#the-usedefineforclassfields-flag-and-the-declare-property-modifier

There's an easy solution, which would be setting useDefineForClassFields to true on tsconfig, but I'm not certain it's the right answer.

Secreto31126 commented 1 year ago

Hi, I just published 1.0.4 to npm with a bug fix.

Let me know if it works!

HysMX commented 1 year ago

The error persists on 1.0.4 its weird because logging the image object only returns the URL image

Image Object image

Code:

        const image = new Image("https://i.imgur.com/CFpN5kh.png");
        console.log(JSON.stringify(image), "imagen 2")
        const button = new Interactive(
          new ActionButtons(
            new Button("button_menu_seller", "Vendedor"),
            new Button("button_menu_buyer", "Comprador")
          ),
          new Body(`¡Bienvenido de nuevo ${customer.name}! ¿Qué menu deseas visualizar?`),
          new Header(image)
        );
        await Whatsapp.sendMessage(PHONE_ID as string,message.from, button);
Secreto31126 commented 1 year ago

That's unfortunate. Seems like a full update will be necessary. I will do it later today.

HysMX commented 1 year ago

Thank you, pibe, where can i send you a tip? 😋 con eso de que el dólar está por las nubes en argentina

Obtener Outlook para iOShttps://aka.ms/o0ukef


De: Tomás Raiti @.> Enviado: Thursday, August 24, 2023 3:57:16 AM Para: Secreto31126/whatsapp-api-js @.> Cc: Omar @.>; Author @.> Asunto: Re: [Secreto31126/whatsapp-api-js] Error sending message: Error: Header image must not have a caption (Issue #198)

That's unfortunate. Seems like a full update will be necessary. I will do it later today.

— Reply to this email directly, view it on GitHubhttps://github.com/Secreto31126/whatsapp-api-js/issues/198#issuecomment-1691460044, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AL63AXP55FVIDHMA7YYLVBLXW4XQZANCNFSM6AAAAAA3TAKV4M. You are receiving this because you authored the thread.Message ID: @.***>

Secreto31126 commented 1 year ago

Wow, eso sería increíble. Si realmente creés que vale la pena, podés usar Patreon:

https://patreon.com/Secreto31126

Muchísimas gracias 😁.

(Sí, el dólar es un caos acá 😂)

Secreto31126 commented 1 year ago

I just did a full rewrite of the property checks and published to the beta tag (npm i whatsapp-api-js@beta), let me know if the issue is fixed for you!

Secreto31126 commented 1 year ago

Based on my white box tests I believe the issue is fixed, so I will publish to npm and close the issue.

Thanks for reporting!