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

whatsapp-cloud-api-client

v0.0.3

Published

A Node.js client library for sending WhatsApp messages using the WhatsApp Cloud API. This package supports various types of messages, including text, media, location, interactive, template, sticker, and contact messages.

Downloads

9

Readme

WhatsApp Cloud API Node.js Client

A Node.js client library for sending WhatsApp messages using the WhatsApp Cloud API. This package supports various types of messages, including text, media, location, interactive, template, sticker, and contact messages.

Message Types

| Type | Description | Status | | --- | --- | --- | | Text | Send a text message | ✅ | | Image | Send an image | ✅ | | Video | Send a video | ⏳ | | Document | Send a document | ✅ | | Audio | Send an audio | ✅ | | Location | Send a location | ✅ | | Interactive | Send an interactive message | ⏳ | | Template | Send a template message | ⏳ | | Sticker | Send a sticker | ⏳ | | Contact | Send a contact | ⏳ |

Installation

You can install the package using npm:

npm install whatsapp-cloud-api-client

Usage

Importing the Client

import { WhatsAppClient } from 'whatsapp-cloud-api';

Initializing the Client

Create a singleton instance of the WhatsAppClient with your configuration:

const client = WhatsAppClient.getInstance({
  phoneNumberId: 'YOUR_PHONE_NUMBER_ID',
  accessToken: 'YOUR_ACCESS_TOKEN',
});

Sending Messages

Send a Text Message

await client.sendText({
  to: 'recipient_phone_number',
  message: 'Hello, World!',
});

Send an Image

await client.sendImage({
  to: 'recipient_phone_number',
  mediaUrl: 'https://example.com/image.jpg',
});

Send a Video

await client.sendVideo({
  to: 'recipient_phone_number',
  mediaUrl: 'https://example.com/video.mp4',
});

Send a Document

await client.sendDocument({
  to: 'recipient_phone_number',
  mediaUrl: 'https://example.com/document.pdf',
});

Send an Audio

await client.sendAudio({
  to: 'recipient_phone_number',
  mediaUrl: 'https://example.com/audio.mp3',
});

Send a Location

await client.sendLocation({
  to: 'recipient_phone_number',
  latitude: '37.7749',
  longitude: '-122.4194',
  name: 'San Francisco',
  address: 'California, USA',
});

Send an Interactive Message

await client.sendInteractive({
  to: 'recipient_phone_number',
  interactiveType: 'button',
  buttons: [
    { id: 'button1', title: 'Option 1' },
    { id: 'button2', title: 'Option 2' }
  ],
});

Send a Template Message

await client.sendTemplate({
  to: 'recipient_phone_number',
  templateName: 'your_template_name',
  templateParams: ['param1', 'param2'],
});

Send a Sticker

await client.sendSticker({
  to: 'recipient_phone_number',
  stickerUrl: 'https://example.com/sticker.webp',
});

Send a Contact

await client.sendContact({
  to: 'recipient_phone_number',
  contact: {
    name: {
      first_name: 'John',
      last_name: 'Doe',
    },
    phones: [
      { phone: '1234567890' },
    ],
  },
});

Configuration

You can set or update the configuration using:

client.setConfig({
  phoneNumberId: 'NEW_PHONE_NUMBER_ID',
  accessToken: 'NEW_ACCESS_TOKEN',
});

API Documentation

  • sendText(options: TextMessageOptions): Promise<WhatsAppMessageResponse> - Sends a text message.
  • sendImage(options: MediaMessageOptions): Promise<WhatsAppMessageResponse> - Sends an image.
  • sendVideo(options: MediaMessageOptions): Promise<WhatsAppMessageResponse> - Sends a video.
  • sendDocument(options: MediaMessageOptions): Promise<WhatsAppMessageResponse> - Sends a document.
  • sendAudio(options: MediaMessageOptions): Promise<WhatsAppMessageResponse> - Sends an audio.
  • sendLocation(options: LocationMessageOptions): Promise<WhatsAppMessageResponse> - Sends a location.
  • sendInteractive(options: InteractiveMessageOptions): Promise<WhatsAppMessageResponse> - Sends an interactive message.
  • sendTemplate(options: TemplateMessageOptions): Promise<WhatsAppMessageResponse> - Sends a template message.
  • sendSticker(options: StickerMessageOptions): Promise<WhatsAppMessageResponse> - Sends a sticker.
  • sendContact(options: ContactMessageOptions): Promise<WhatsAppMessageResponse> - Sends a contact.

Error Handling

Errors will be thrown as Error objects with descriptive messages. Handle them with try-catch blocks.

try {
  await client.sendText({ to: 'recipient_phone_number', message: 'Hello!' });
} catch (error) {
  console.error(error.message);
}

License

This package is licensed under the MIT License.

Contributing

Feel free to submit issues or pull requests to contribute to this project.