whatsapp-api-js - v6.1.1
    Preparing search index...

    Interface API

    interface API {
        broadcastMessage(
            phoneID: string,
            to: string[],
            message: ClientMessage,
            batch_size: number,
            delay: number,
        ): Promise<ServerMessageResponse>[];
        broadcastMessage<T>(
            phoneID: string,
            to: T[],
            message_builder: (data: T) => [string, ClientMessage],
            batch_size: number,
            delay: number,
        ): Promise<ServerMessageResponse>[];
        markAsRead(
            phoneID: string,
            messageId: string,
            indicator?: "text",
        ): Promise<ServerMarkAsReadResponse>;
        sendMessage(
            phoneID: string,
            to: string,
            message: ClientMessage,
            context?: string,
            biz_opaque_callback_data?: string,
        ): Promise<ServerMessageResponse>;
    }

    Implemented by

    Index

    Methods

    • Send a Whatsapp message to multiple phone numbers.

      In order to avoid reaching the API rate limit, this method will send the messages in batches of 50 per second by default, but this can be changed using the batch_size and delay parameters.

      The API rate limit can be increased by contacting Facebook as explained here.

      Parameters

      • phoneID: string

        The bot's phone ID

      • to: string[]

        The users' phone numbers

      • message: ClientMessage

        A Whatsapp message, built using the corresponding module for each type of message.

      • batch_size: number

        The number of messages to send per batch

      • delay: number

        The delay between each batch of messages in milliseconds

      Returns Promise<ServerMessageResponse>[]

      The server's responses

      import { WhatsAppAPI } from "whatsapp-api-js";
      import { Text } from "whatsapp-api-js/messages/text";

      const Whatsapp = new WhatsAppAPI({
      token: "YOUR_TOKEN",
      appSecret: "YOUR_APP_SECRET"
      });

      const phoneID = "YOUR_BOT_NUMBER";
      const users = ["YOUR_USER1_NUMBER", "YOUR_USER2_NUMBER"];
      const message = new Text("Hello World");

      const responses = Whatsapp.broadcastMessage(phoneID, users, message);

      Promise.all(responses).then(console.log);

      if batch_size is lower than 1

      if delay is lower than 0

    • Type Parameters

      • T

        The type of the data to be used in the message builder

      Parameters

      • phoneID: string

        The bot's phone ID

      • to: T[]

        The users' data

      • message_builder: (data: T) => [string, ClientMessage]

        A Whatsapp message builder, it returns an array with the phone number and the message.

      • batch_size: number

        The number of messages to send per batch

      • delay: number

        The delay between each batch of messages in milliseconds

      Returns Promise<ServerMessageResponse>[]

      The server's responses

      import { WhatsAppAPI } from "whatsapp-api-js";
      import { Text } from "whatsapp-api-js/messages/text";

      const Whatsapp = new WhatsAppAPI({
      token: "YOUR_TOKEN",
      appSecret: "YOUR_APP_SECRET"
      });

      const phoneID = "YOUR_BOT_NUMBER";
      const users = [{ user: "USER1_ID" }, { user: "USER2_ID" }];
      const message_builder = ({ user }) => [DB.fetch(user).phone, new Text(`Hello ${user}`)];

      const responses = Whatsapp.broadcastMessage(phoneID, users, message);

      Promise.all(responses).then(console.log);

      if batch_size is lower than 1

      if delay is lower than 0

    • Send a Whatsapp message

      Parameters

      • phoneID: string

        The bot's phone ID

      • to: string

        The user's phone number

      • message: ClientMessage

        A Whatsapp message, built using the corresponding module for each type of message.

      • Optionalcontext: string

        The message ID of the message to reply to

      • Optionalbiz_opaque_callback_data: string

        An arbitrary 512B string, useful for tracking (length not checked by the framework)

      Returns Promise<ServerMessageResponse>

      The server response

      import { WhatsAppAPI } from "whatsapp-api-js";
      import { Text } from "whatsapp-api-js/messages/text";

      const Whatsapp = new WhatsAppAPI({
      token: "YOUR_TOKEN",
      appSecret: "YOUR_APP_SECRET"
      });

      Whatsapp.sendMessage(
      "BOT_PHONE_ID",
      "USER_PHONE",
      new Text("Hello World")
      ).then(console.log);