WhatsApp Business Platform Node.js SDK

repository·main·Indexed 42 days ago

https://github.com/whatsapp/whatsapp-nodejs-sdk

A TypeScript and Node.js SDK for interfacing with the WhatsApp Business Platform Cloud API hosted by Meta. Version 0.0.5-Alpha. This project is currently archived and development has stopped. It provides the WhatsApp class for API interaction, support for sending text, media, interactive, and template messages, and tools for phone number verification and webhook management.

Tokens
33.2K
Snippets
128
Records
167
Agent score
75%

What's inside WhatsApp Node.js SDK

  1. Understand the InteractiveObject structure

    main

    An InteractiveObject is a container for interactive messages. While the specific requirements for the action field change based on the type, most interactive objects share common properties:

    • type: Determines the interactive mode (list, product_list, product, or button).
    • body: A SimpleTextObject containing the message text (supports emojis and markdown, max 1024 chars).
    • footer (optional): A SimpleTextObject (supports emojis, markdown, and links, max 60 chars).
    • header (optional): A HeaderObject displayed at the top.
    • action: An ActionObject defining the user interaction (e.g., selecting a list row, choosing a product, or clicking a button).
  2. How the Messages class is instantiated

    main

    The Messages class is not instantiated directly by the user. Instead, it is automatically instantiated by the WhatsApp class and attached as a member. You access its functionality through the <<SDK_NAMESPACE>>.messages property (e.g., wa.messages if your instance is named wa).

    When the SDK internally instantiates the Messages class, it passes the following arguments:

    1. config: An object of type WAConfigType containing the SDK configuration.
    2. HttpsClient: A RequesterClass object containing the HTTP client instance used to perform the requests.
  3. Use ComponentObject to fill message template parameters

    main

    The ComponentObject is used to populate available message template parameters with specific content types such as text, currency, date-time, media (document, image, video), or buttons.

    Each component object requires a type (either header or body) and a parameters array. Note that text-based templates only support type: "body" and text components. The parameters array can contain a mix of different component parameter types.

  4. How the webhook callback function works

    main

    When using wa.webhooks.start(callback), the provided callback function is invoked for incoming POST requests. GET requests are handled internally by the SDK for Cloud API subscription verification.

    The callback receives the following parameters:

    • statusCode: The HTTP status code of the incoming request.
    • headers: The request headers.
    • body: The parsed request body.
    • resp: The response object used to send a reply back to the Cloud API. Crucial: You must call resp.writeHead(200, ...) and resp.end() to acknowledge the message and prevent the Cloud API from retrying the delivery.
    • err: An error object if the request failed.
  5. Use StickerMediaObject to send stickers

    main

    The StickerMediaObject is used to specify the media for a sticker message. It supports both static and animated third-party outbound stickers.

    Requirements for stickers:

    • Static stickers: Must be 512x512 pixels and $\le$ 100 KB.
    • Animated stickers: Must be 512x512 pixels and $\le$ 500 KB.

    You can provide the sticker using either a Meta-hosted ID or a publicly accessible URL (self-hosted).