Secreto31126 / whatsapp-api-js

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

Fix str.replace is not a function #333

Closed zerobits-de closed 5 months ago

zerobits-de commented 5 months ago

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

Tests

Fix

I got this error when parsing a message through Whatsapp.post() on version 3.0.0

The PR fixed the error for me.

Secreto31126 commented 5 months ago

Hi, thanks for opening a PR!

I read the change, and I'm not sure if the error is an issue within the library. As you can see, the str argument in the escapeUnicode signature already requires a string, so there should be no reason to use .toString() on it. The only usage of the method is on Whatsapp.post(), and raw_body should be a string | undefined. Undefined is discarded a few lines above, so the only posibilities are either a string if you are following type check, or the method received something else that's not following type checking.

I would love to see what's your use case of the code, make sure the library is being used as expected, and then, if needed, hunt down the real issue and fix it.

Thanks for your help!

zerobits-de commented 5 months ago

Thanks for coming back to me.

I double checked my code and it seems that my body is in binary. Converting the body with body.toString()solved it for me.

  public async receive(body: any, headers: any) {
    console.log(body.toString());
    return await this.wa.post(
      JSON.parse(body ?? "{}"),
      body.toString(),
      headers["x-hub-signature-256"]
    );
  } 
Secreto31126 commented 5 months ago

Awesome, happy to help 😄