Archit Dadwal
Archit Dadwal

Reputation: 1

Whatsapp cloud API integration with react web app

I am trying to integrate my web app with Whatsapp cloud API. I am able to send test messages but I want to add variables and API documentation says it can accept it as well but I am not able to pass data.

Call for sending message:

const sendMessage = () => {
    const body = {
      messaging_product: "whatsapp",
      to: "91" + 8872649119,
      type: "template",
      template: {
        name: "hello_world",
        language: { code: "en_US" },
      },
    };

    axios
      .post(
        "  https://graph.facebook.com/v17.0/100320906470290/messages",
        body,
        header
      )
      .then((res) => console.log("sent successfully", res))
      .catch((err) => console.log("error while sending", err));
  };

Template

Welcome and congratulations!! This message demonstrates your ability to send a WhatsApp message notification from the Cloud API, hosted by Meta. Thank you for taking the time to test with us. {{1}}

Here template is set and saved on meta's website (screenshot below ). Now suppose I want to send user name in {{1}} how can I do it?

whatsapp cloud api template page

Upvotes: 0

Views: 1809

Answers (1)

Aman
Aman

Reputation: 1

Try adding the component to your body as follows and replace {1} with the string value that you want to send

const body = {
      messaging_product: "whatsapp",
      to: "91" + 8872649119,
      type: "template",
      template: {
        name: "hello_world",
        language: { code: "en_US" },
        components: [
            {
                "type": "body",
                "parameters": [
                    {
                        "type": "text",
                        "text": "{1}"
                    }]}]
      }
    };

Upvotes: 0

Related Questions