Installation
You can install the Waabot SDK using npm, yarn, or pnpm.
Using npm
Copy npm install waabot-v2-sdk
Using yarn
Copy yarn add waabot-v2-sdk
Using pnpm
Copy pnpm add waabot-v2-sdk
Getting Started
Initializing the SDK
First, instantiate the SDK with your apiKey
and apiSecret
.
Copy 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:
Copy 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:
Copy 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:
Copy await app.message.sendText({
recipient: to,
type: 'TEXT',
body: {
text: { body: 'hello' }
}
});
To send a message with reply buttons, use the following code:
Copy 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:
Copy 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:
Copy 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'
}
]
}
]
}
}
});
Last updated 9 months ago