Sending custom alerts and notifications to customers is crucial for any business. With Wbiztool’s API, you can automate WhatsApp messaging for various notifications using Node.js. In this article, we’ll build a simple Node.js application to send custom alerts through the Wbiztool API.
Prerequisites
Before you start, ensure you have:
- A Wbiztool account with an active API key.
- Node.js and npm are installed on your system.
- Basic knowledge of JavaScript and REST APIs.
Step 1: Create a New Node.js Project
Start by creating a new directory for your project and initializing a Node.js project:
mkdir wbiztool-alerts
cd wbiztool-alerts
npm init -y
This will create a package.json
file with default settings.
Step 2: Install Required Packages
We need the axios
package to make HTTP requests and dotenv
to manage environment variables:
npm install axios dotenv
Step 3: Set Up Environment Variables
Create a .env
file to store your Wbiztool credentials:
touch .env
Open .env
and add the following lines:
API_KEY=your_api_key
CLIENT_ID=your_client_id
WHATSAPP_CLIENT_ID=your_whatsapp_client_id
Step 4: Write the Node.js Script
Create a file named sendAlert.js
:
const axios = require('axios');
require('dotenv').config();
// Load environment variables
const API_KEY = process.env.API_KEY;
const CLIENT_ID = process.env.CLIENT_ID;
const WHATSAPP_CLIENT_ID = process.env.WHATSAPP_CLIENT_ID;
// Function to send a custom alert
async function sendCustomAlert(phone, countryCode, message) {
const url = 'https://wbiztool.com/api/v1/send_msg/';
const headers = {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
};
const payload = {
client_id: CLIENT_ID,
api_key: API_KEY,
whatsapp_client: WHATSAPP_CLIENT_ID,
phone: phone,
country_code: countryCode,
msg: message,
};
try {
const response = await axios.post(url, payload, { headers: headers });
if (response.status === 200) {
console.log('Alert sent successfully!');
} else {
console.error(`Failed to send alert: ${response.status} - ${response.statusText}`);
}
} catch (error) {
console.error('Error:', error.message);
}
}
// Example usage
const phone = '1234567890'; // Replace with recipient's phone number
const countryCode = '91'; // Replace with recipient's country code
const message = 'This is a custom alert notification from Wbiztool!';
sendCustomAlert(phone, countryCode, message);
Explanation of the Script
- Imports: We use
axios
for making HTTP requests anddotenv
to manage environment variables. - Environment Variables: API credentials are loaded from the
.env
file. sendCustomAlert
Function: This function constructs a POST request to Wbiztool’ssend_msg
API endpoint. It includes headers for authorization and sends the necessary payload (phone number, country code, and message) to the API.- Error Handling: It includes basic error handling to catch and display any issues during the request.
- Example Usage: At the bottom, we call the
sendCustomAlert
function with a sample phone number, country code, and message. Replace these values with real data to test it.
Step 5: Run the Script
Run your script using Node.js:
node sendAlert.js
If everything is set up correctly, you should see a message in the console saying, “Alert sent successfully!” This indicates that the notification was sent using the Wbiztool API.
Next Steps
- Scheduling Alerts: Use the
node-cron
package to schedule these alerts for specific times. - Dynamic Inputs: Modify the script to accept user inputs for phone numbers and messages.
Conclusion
With Wbiztool’s API and Node.js, you can easily build a custom alert system to send WhatsApp notifications. This script is a basic implementation, but it can be extended to include scheduling, user input, and error handling for a more robust application.
For more information, visit the Wbiztool API Documentation.