Introduction
In this tutorial, you'll learn how to send messages to phone numbers stored in a CSV file using Java, the Unirest library, and the TaxiMessages API. We will read the phone numbers and messages from the CSV file, and then send them using the TaxiMessages API.
Prerequisites
- Basic understanding of Java
- Java Development Kit (JDK) installed on your computer
- A text editor for writing code (e.g: IntelliJ IDEA, Eclipse, NetBeans)
- A TaxiMessages API token (you can obtain this by signing up here)
- Apache Maven installed to manage dependencies
Create a new Maven project
Create a new Maven project using your preferred IDE or via command line.
Add required dependencies
Add the following dependencies to your pom.xml
file:
<dependencies>
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>3.11.00</version>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.2</version>
</dependency>
</dependencies>
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! 🤗"
Create a file with the code
Create a new file named SendMessage.java
in your project directory and add the following code:
import com.opencsv.CSVReader;
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
public class SendMessage {
private static final String CSV_FILE = "numbers.csv";
// Replace this with your TaxiMessages API token
private static final String API_TOKEN = "ENTER API KEY HERE";
// Optionally specify the target WhatsApp device ID connected to TaxiMessages
// you want to use for messages delivery (24 characters hexadecimal value)
private static final String DEVICE = "DEVICE ID GOES HERE";
// API URL endpoint
private static final String URL = "https://api.taximessages.com/v1/messages";
public static void main(String[] args) {
try (CSVReader csvReader = new CSVReader(new FileReader(CSV_FILE))) {
List<String[]> records = csvReader.readAll();
for (String[] record : records) {
String phone = record[0];
String message = record[1];
sendMessage(phone, message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void sendMessage(String phone, String message) {
HttpResponse<JsonNode> response = Unirest.post(URL)
.header("Content-Type", "application/json")
.header("Authorization", API_TOKEN)
.field("phone", phone)
.field("message", message)
.asJson();
if (response.isSuccess()) {
System.out.println("=> Message created: " + phone);
} else {
System.out.println("Failed to create message to " + phone + ": " + response.getStatusText());
}
}
}
Replace the API token
In the SendMessage.java
file, make sure you have defined the API token of your actual TaxiMessages account:
// Replace this with your TaxiMessages API token
private static final String API_TOKEN = "ENTER API KEY HERE";
Optionally, if you have multiple WhatsApp numbers connected in your TaxiMessages account, you can specify which WhatsApp number you want to use for the messages delivery by specifying the TaxiMessages unique device ID (24 characters hexadecimal value) in the following line:
// Optionally specify the target WhatsApp device ID connected to TaxiMessages
// you want to use for messages delivery (24 characters hexadecimal value)
private static final String DEVICE = "DEVICE ID GOES HERE";
Run the program
Run the SendMessage.java
file in your IDE or compile and run it using the command line:
javac SendMessage.java
java SendMessage
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 Java, the Unirest library, and the TaxiMessages API.
You can update numbers.csv file and run the program again anytime you want to send new messages through your TaxiMessages 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.