Introduction
In this tutorial, you'll learn how to send messages to phone numbers stored in a CSV file using PHP, the PECL HTTP extension, and the Wablapps API. We will read the phone numbers and messages from the CSV file, and then send them using the Wablapps API.
Prerequisites
- Basic understanding of PHP
- PHP 7.4 or higher installed on your computer
- A text editor for writing code (e.g: Visual Studio Code, Atom, Vim)
- A Wablapps API token (you can obtain this by signing up here)
pecl_http
extension installed for HTTP networking
Install required packages
First, create a new directory for your project and navigate to it in your terminal. Then, run the following commands to install the necessary libraries:
pecl install pecl_http
Create the CSV file
Create a new file named numbers.csv
in your project directory with two columns:
- First column: phone number in E164 format with the country prefix.
- Second column: text message to send to the target phone number.
The spreadsheet document should look like this:
Phone number | Message body |
---|---|
+1234567890 | 👋 Welcome to {{your-business-name}}! Thanks for signing up. We are just a message away! |
+1234567890 | 💐 Your order has been shipped. Tracking number is {{tracking-number}}. Don't hesitate to reach out to if you need help! 🤗 |
The equivalent spreadsheet document exported as CSV should look like this:
+1234567890,"👋 Welcome to {{your-business-name}}! Thanks for signing up. We are just a message away!"
+1234567890,"💐 Your order has been shipped. Tracking number is {{tracking-number}}. Don't hesitate to reach out to if you need help! 🤗"
You can export any Office Excel or Google Sheets document as CSV file by following these instructions:
- Learn how to export an Excel spreadsheet as a CSV file
- Learn how to export Google Sheet as a CSV file
Create a file with the code
Create a new file named send_messages.php
in your project directory and add the following code:
<?php
$csvFile = 'numbers.csv';
// Replace this with your Wablapps API token
// Get your API token here: https://app.wablapps.com/apikeys
$token = 'ENTER API KEY HERE';
// Optionally specify the target WhatsApp device ID connected to Wablapps
// you want to use for messages delivery (24 characters hexadecimal value)
$device = 'DEVICE ID GOES HERE';
// Define the headers for the API request
$headers = [
'Content-Type' => 'application/json',
'Authorization' => $token,
];
// Define the URL for the Wablapps API
$url = 'https://api.wablapps.com/v1/messages';
function sendMessage($phone, $message, $url, $headers, $device)
{
$body = [
'phone' => $phone,
'message' => $message,
'device' => $device,
];
$client = new http\Client;
$request = new http\Client\Request;
$request->setRequestUrl($url);
$request->setRequestMethod('POST');
$request->setBody(json_encode($body));
$request->setHeaders($headers);
try {
$response = $client->send($request);
if ($response->getResponseCode() == 200) {
echo "=> Message created: {$phone}\n";
} else {
echo "Failed to create message to {$phone}: {$response->getBody()}\n";
}
} catch (Exception $error) {
echo "Error: {$error->getMessage()}\n";
}
}
function main($csvFile, $url, $headers, $device)
{
try {
$file = fopen($csvFile, 'r');
while (($row = fgetcsv($file)) !== false) {
list($phone, $message) = $row;
sendMessage($phone, $message, $url, $headers, $device);
}
fclose($file);
} catch (Exception $err) {
echo "Error: {$err->getMessage()}\n";
}
}
main($csvFile, $url, $headers, $device);
?>
Replace the API token
In the send_messages.php file, make sure you have defined the API token of your actual Wablapps account:
// Replace this with your Wablapps API token
$token = 'ENTER API KEY HERE';
Optionally, if you have multiple WhatsApp numbers connected in your Wablapps account, you can specify which WhatsApp number you want to use for the messages delivery by specifying the Wablapps unique device ID (24 characters hexadecimal value) in the following line:
// Optionally specify the target WhatsApp device ID connected to Wablapps
// you want to use for messages delivery (24 characters hexadecimal value)
$device = 'DEVICE ID GOES HERE';
Run the program
Open a terminal in your project directory and run the following command to execute the send_messages.php
script:
php send_messages.php
If everything is set up correctly, you should see output indicating the messages have been created successfully:
=> Message created: +1234567890
=> Message created: +1234567890
=> Message created: +1234567890
Note messages will be added to your number's message delivery queue and delivered asynchronously in background over time based on your number's subscription message delivery speed per minute limit or the manually configured delivery speed you have defined in your number's settings.
Conclusion
In this tutorial, you learned how to send messages to phone numbers stored in a CSV file using PHP, the PECL HTTP extension, and the Wablapps API.
You can update the numbers.csv
file and run the program again anytime you want to send new messages through your Wablapps connected WhatsApp number.
You can further customize the script to handle additional columns, create different types of messages, or integrate it with your own software as needed.