npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

meta-whatsapp-cloud-api-sdk

v1.1.4

Published

This is the SDK for simplifying messaging and webhooks for meta whatsapp cloud api.

Downloads

17

Readme

META-WHATSAPP-CLOUD-API-SDK

This Node.js SDK provides functionalities to interact with the WhatsApp Cloud Business API, including sending templates, messages, attachments, uploading media, and verifying webhooks.

Installation

  1. Install the package in your project.
npm i meta-whatsapp-cloud-api-sdk
  1. Require the package in your project.
const Whatsapp = require('meta-whatsapp-cloud-api-sdk');

Usage

Initialization

// Provide Phone Number ID, Access Token, Webhook Verify Token, and Meta App Version.

const phoneNumberId = "your_phone_number_id";
const accessToken = "your_access_token";
const appVersion = "your_app_version"; // Optional, Default version is v19.0.
const accountId = "your_account_id"; // Optional, Add this if you wish to use Template Management.
const webhookVerifyToken = "your_webhook_verify_token"; // Optional, Add this if you wish to use Webhook.

const whatsapp = new Whatsapp(phoneNumberId, accessToken, appVersion, accountId, webhookVerifyToken);

Sending Templates

// Example header
const header = [
    {
        type: "image",
        image: {
            link: "URL"
        }
    },
    {
        type: "location",
        location: {
            latitude: "<LATITUDE>",
            longitude: "<LONGITUDE>",
            name: "<NAME>",
            address: "<ADDRESS>"
        }
    }
];

// Example body
const body = [
    {
        type: "text",
        text: "TEXT-STRING"
    },
    {
        type: "currency",
        currency: {
            fallback_value: "VALUE",
            code: "USD",
            amount_1000: NUMBER
        }
    },
    {
        type: "date_time",
        date_time: {
            fallback_value: "MONTH DAY, YEAR"
        }
    }
];

// Example buttons
const buttons = [
    {
        type: "button",
        sub_type: "quick_reply",
        index: "0",
        parameters: [
            {
                type: "payload",
                payload: "PAYLOAD"
            }
        ]
    },
    {
        type: "button",
        sub_type: "quick_reply",
        index: "1",
        parameters: [
            {
                type: "payload",
                payload: "PAYLOAD"
            }
        ]
    }
];

// Send a template message (async/await) - header, body and buttons parameters are optional
const response = await whatsapp.sendTemplate(to, templateName, language, header, body, buttons);

Or

// Send a template message (promise) - header, body and buttons parameters are optional
whatsapp.sendTemplate(to, templateName, language, header, body, buttons)
.then((response)=>{
    console.log(response);
}).catch((error)=>{
    console.log(error);
});

Sending Messages

// Send a text message (async/await)
const response = await whatsapp.sendMessage(to, message);

Or

// Send a text message (promise)
whatsapp.sendMessage(to, message)
.then((response)=>{
    console.log(response);
}).catch((error)=>{
    console.log(error);
});

Sending Image

// Upload media (async/await)
const media = await whatsapp.uploadMedia(file);

// Send an image message (async/await) - 2nd Parameter is Media Id or Media Link
const response = await whatsapp.sendImage(to, media.id, caption);

Or

// Upload media (promise)
whatsapp.uploadMedia(file)
.then((media)=>{
    // Send an image message (promise) - 2nd Parameter is Media Id or Media Link
    whatsapp.sendImage(to, media.id, caption)
    .then((response)=>{
        console.log(response);
    }).catch((error)=>{
        console.log(error);
    });
}).catch((error)=>{
    console.log(error);
});

Sending Video

// Upload media (async/await)
const media = await whatsapp.uploadMedia(file);

// Send a video message (async/await) - 2nd Parameter is Media Id or Media Link
const response = await whatsapp.sendVideo(to, media.id, caption);

Or

// Upload media (promise)
whatsapp.uploadMedia(file)
.then((media)=>{
    // Send a video message (promise) - 2nd Parameter is Media Id or Media Link
    whatsapp.sendVideo(to, media.id, caption)
    .then((response)=>{
        console.log(response);
    }).catch((error)=>{
        console.log(error);
    });
}).catch((error)=>{
    console.log(error);
});

Sending Audio

// Upload media (async/await)
const media = await whatsapp.uploadMedia(file);

// Send an audio message (async/await) - 2nd Parameter is Media Id or Media Link
const response = await whatsapp.sendAudio(to, media.id);

Or

// Upload media (promise)
whatsapp.uploadMedia(file)
.then((media)=>{
    // Send an audio message (promise) - 2nd Parameter is Media Id or Media Link
    whatsapp.sendAudio(to, media.id)
    .then((response)=>{
        console.log(response);
    }).catch((error)=>{
        console.log(error);
    });
}).catch((error)=>{
    console.log(error);
});

Sending Document

// Upload media (async/await)
const media = await whatsapp.uploadMedia(file);

// Send a document message (async/await) - 2nd Parameter is Media Id or Media Link
const response = await whatsapp.sendDocument(to, media.id, caption, filename);

Or

// Upload media (promise)
whatsapp.uploadMedia(file)
.then((media)=>{
    // Send a document message (promise) - 2nd Parameter is Media Id or Media Link
    whatsapp.sendDocument(to, media.id, caption, filename)
    .then((response)=>{
        console.log(response);
    }).catch((error)=>{
        console.log(error);
    });
}).catch((error)=>{
    console.log(error);
});

Download Media

// Download media (async/await)
const response = await whatsapp.downloadMedia(mediaId);

Or

// Download media (promise)
whatsapp.downloadMedia(mediaId)
.then((response)=>{
    console.log(response);
}).catch((error)=>{
    console.log(error);
});

Webhook

// Verify webhook endpoint (Express.js example)
app.get("/webhook", whatsapp.verifyWebhook);

Manage Templates

// Register template
const response = await whatsapp.registerTemplate(name, components, category, allowCategoryChange, language);

// Get templates
const response = await whatsapp.getTemplates(query, fields, limit);

// Get template Info
const response = await whatsapp.getTemplateInfo(templateId);

// Update template
const response = await whatsapp.updateTemplate(templateId, category, components);

// Delete template
const response = await whatsapp.deleteTemplate(name, templateId);

Validate WhatsApp Account Users or Contacts

// Validate contacts
const response = await whatsapp.validateContacts(contacts, blocking, forceCheck);