WhatsApp / WhatsApp-Nodejs-SDK

The official Meta Business Messaging WhatsApp Cloud API Node.js SDK.
https://whatsapp.github.io/WhatsApp-Nodejs-SDK/
Other
151 stars 74 forks source link

400 Bad Request on interactive messages #12

Closed marluanespiritusanto closed 1 year ago

marluanespiritusanto commented 1 year ago

Prerequisites

Please ensure the following are done before submitting an issue:

Expected behavior

Using the interactive method of the messages object should send an interactive message (button or list).

Current behavior

The method returns a 400 Bad Request because an unexpected property is sent: interactive.

Failure information (for bug reporting)

If this is a bug, please complete the sections below. Otherwise, delete and enter your feature request here.

Steps to Reproduce

Detailed steps to reproduce the issue. Be as detailed as possible without compromising any sensitive data.

async sendButtons(message: string, recipientNumber: number) {
    try {
      const response = await this.whatsapp.messages.interactive(
        {
          interactive: {
            type: InteractiveTypesEnum.Button,
            body: {
              text: message,
            },
            action: {
              buttons: [
                {
                  type: "reply",
                  reply: {
                    id: "0001",
                    title: "Example 1",
                  },
                },
                {
                  type: "reply",
                  reply: {
                    id: "0002",
                    title: "Example 2",
                  },
                },
              ],
            },
          },
        },
        recipientNumber
      );

      response.responseBodyToJSON().then((data) => {
        console.log(data); // 400 bad request
      });
    } catch (ex) {
      console.log(ex);
    }
  }

Context

Please provide any other relevant information about your application or configuration.

The request is supposed to be like this:

{
    "messaging_product": "whatsapp",
    "recipient_type": "individual",
    "to": "10000000000",
    "type": "interactive",
    "interactive": {
        ...
    }
}

this is what is being sent instead:

{
    "messaging_product": "whatsapp",
    "recipient_type": "individual",
    "to": "10000000000",
    "type": "interactive",
    "interactive": {
        "interactive": {
               ...
        }
    }
}

The problem is here in this line.

Failure Logs

Please paste any relevant logs here. REMEMBER TO REMOVE ANY/ALL sensitive data (e.g. phone numbers, names, security keys, etc.)!!!

{
    "code": 100
    "fbtrace_id": "AobQ4HleWGGHWMbkW47A88E"
    "message": "(#100) Unexpected key 'interactive' on param 'interactive'."
    "type": "OAuthException"
}
rashedtalukder commented 1 year ago

@marluanespiritusanto, can you reference what docs you say you're looking at? The WhatsApp SDK docs state that you need to send the InteractiveObject as the argument to body for sending a button: https://whatsapp.github.io/WhatsApp-Nodejs-SDK/api-reference/messages/interactive

The object you pass in should be InteractiveObject, which should not have the parent "interactive" key.

marluanespiritusanto commented 1 year ago

@rashedtalukder I agree with you, the documentation does not indicate that an interactive property is required, but if you check this line, you will see that it is.

export type InteractiveObject = {
    [MessageTypesEnum.Interactive]:
        | ButtonInteractiveObject
        | ListInteractiveObject
        | ProductInteractiveObject
        | ProductListInteractiveObject;
};
rashedtalukder commented 1 year ago

Appreciate the follow up and you are right. I pushed a new branch that should remedy that issue. Let me know if it resolves your issue and I'll see about adding it to the main line: https://github.com/WhatsApp/WhatsApp-Nodejs-SDK/tree/hotfix/InteractiveObject

marluanespiritusanto commented 1 year ago

@rashedtalukder With your hotfix branch work like a charm. You can proceed with the PR.