⚙️Nodejs SDK

Installation

You can install the Waabot SDK using npm, yarn, or pnpm.

Using npm

npm install waabot-v2-sdk

Using yarn

yarn add waabot-v2-sdk

Using pnpm

pnpm add waabot-v2-sdk

Getting Started

Initializing the SDK

First, instantiate the SDK with your apiKey and apiSecret.

require('dotenv').config();

const { Waabot } = require('waabot-v2-sdk');

const apiKey = process.env.apiKey;
const apiSecret = process.env.apiSecret;
const to = process.env.to;

const app = new Waabot({ apiKey, apiSecret });

Listening to New Messages

You can listen to new messages with the following code:

app.socket.onMessage((message) => {
    const content = message.messages[0];
    const contact = message.contacts[0];

    console.log({ contact, content });
});

The interface of the message is as follows:

export interface WebhookWhatsAppMessage {
    messaging_product: "whatsapp";
    metadata: {
        display_phone_number: string;
        phone_number_id: string;
    };
    contacts: { profile: { name: string }; wa_id: string }[];
    messages: WhatsAppMessageEntry[];
}

export interface WhatsAppMessageEntry {
    text?: { body: string };
    context: { from: string; id: string };
    from: string;
    id: string;
    timestamp: string;
    type: "interactive";
    interactive?: {
        type: "button_reply";
        button_reply: { id: string; title: string };
    };
}

Sending a Text Message

To send a text message, use the following code:

await app.message.sendText({
    recipient: to,
    type: 'TEXT',
    body: {
        text: { body: 'hello' }
    }
});

Sending Reply Buttons

To send a message with reply buttons, use the following code:

await app.message.sendReplyButtons({
    recipient: to,
    body: {
        interactive: {
            type: 'button',
            body: {
                text: 'hello3',
            },
            action: {
                buttons: [{
                    type: 'reply',
                    reply: {
                        id: 'greetings',
                        title: 'Good Morning'
                    }
                }]
            }
        }
    }
});

Creating a Template

To create a template, use the following code:

await app.template.create({
    name: "utility_test_1",
    category: 'UTILITY',
    language: 'en_US',
    components: [
        {
            type: "BODY",
            text: "Thank you for your order, {{1}}! Your confirmation number is {{2}}. If you have any questions, please use the buttons below to contact support. Thank you for being a customer!",
            example: {
                body_text: [
                    ["Pablo", "860198-230332"]
                ]
            }
        },
        {
            type: "BUTTONS",
            buttons: [
                {
                    type: 'PHONE_NUMBER',
                    text: "Call",
                    phone_number: "2348179803743"
                },
                {
                    type: 'URL',
                    text: "Contact Support",
                    url: "https://www.luckyshrub.com/support"
                }
            ]
        }
    ]
});

Sending a Template

To send a template message, use the following code:

await app.message.sendTemplate({
    recipient: to,
    type: 'TEMPLATE',
    body: {
        template: {
            name: "utility_test_2",
            language: {
                code: 'en_US'
            },
            components: [
                {
                    type: 'body',
                    parameters: [
                        {
                            type: 'text',
                            text: 'Bankole'
                        },
                        {
                            type: 'text',
                            text: '860198-230332'
                        }
                    ]
                }
            ]
        }
    }
});

These examples should help you get started with the Waabot SDK for Node.js. For more information, please refer to the official documentation at npmjs.com/package/waabot-v2-sdk.

Last updated