Send Automated Messages On WhatsApp Using Java

May 30, 2024

Imagine automating your WhatsApp messages to send your customers reminders, notifications, shipping confirmations, promotions, and more quickly and efficiently. This saves you time, reduces costs, ensures effective delivery and enhances your customers’ experience. With Wassenger and Java, you can make all this a reality.

In this tutorial, we will guide you step-by-step on how to set up and use the Wassenger API with Java. Learn how to send automated WhatsApp messages and discover how this powerful combination can optimize your efficiency and strengthen customer communication. 🚀

🫣 Don’t want to use programming? No problem! Explore our new no-code WhatsApp Campaigns feature. Import your contacts, define a message, set a delivery date and relax! 🥳 🥳

Wassenger is a robust and versatile tool that allows you to send different messages according to your needs. Whether it’s reminders, personalized notifications, or promotional content, Wassenger makes it easy to maintain consistent and effective communication with your customers.

Key Benefits:

  • Automate WhatsApp Messages: Save time by automating your WhatsApp communications.
  • Better Customer Engagement: Send personalized messages that connect with your customers.
  • Cost-Effective Marketing: Lower your costs while boosting your marketing effectiveness.
  • Easy Integration: Integrate the Wassenger API with Java smoothly and easily.

🤩 🤖 Wassenger is a complete communication platform and API solution for WhatsApp. Explore more than 100+ API use cases and automate anything on WhatsApp by signing up for a free trial and getting started in minutes!

Requirements

  • To have a WhatsApp number already linked to the platform and online.
  • Message recipient phone number with international prefix in E164 format. Example: +12345678900. Validate the phone number format here.

API endpoint

We will use the following API endpoint to send messages to a chat:

Prepare the request

Target API URL using the POST method

https://api.wassenger.com/v1/messages

Required HTTPS headers > Obtain your API key here

Content-Type: application/json
Token: $API_TOKEN

Use body in JSON format

{
  "phone": "+1234567890",
  "message": "Hello world, this is a sample message"
}

🖥️ Looking for a code example? Go to the API live tester and get ready-to-use code examples in 15+ programming languages, including Python, JavaScript, PHP, C#, Java, Ruby, Go, Powershell, cURL and more.

Send automated messages with Java

Steps:

1. Install Unirest: Ensure you have the Unirest package installed.

2. Prepare the Request: Use Unirest to create and send a POST request with the necessary headers and JSON body.

// This code requires the Unirest package to be installed. // Documentation: https://kong.github.io/unirest-java/#requests // Installation: http://kong.github.io/unirest-java/

import kong.unirest.HttpResponse; import kong.unirest.Unirest;

public class SendMessage { public static void main(String[] args) { HttpResponse response = Unirest.post("https://api.wassenger.com/v1/messages") .header("Content-Type", "application/json") .header("Token", "ENTER API KEY HERE") .body("{\"phone\":\"+1234567890\",\"message\":\"Hello world, this is a sample message\"}") .asString();

    if (response.getStatus() == 200) {
        System.out.println("Message sent successfully.");
        System.out.println("Response: " + response.getBody());
    } else {
        System.out.println("Failed to send message. Status code: " + response.getStatus());
        System.out.println("Error: " + response.getBody());
    }
}

}

Detailed Steps:

  • Install Unirest: Add the Unirest dependency to your project using Maven or Gradle.
  • Prepare the Request: Use Unirest to configure and send the POST request.
  • Headers: Add the necessary Content-Type and Token headers.
  • Request Body: Configure the request body with the message in JSON format.
  • Execute and Handle the Response: Execute and handle the request, checking the status code to determine if the message was sent successfully.

Do you want to know more? We have all message cases covered!

  1. Send text messages with high priority to a group
HttpResponse<String> response = Unirest.post("https://api.wassenger.com/v1/messages")
.header("Content-Type", "application/json")
.header("Token", "<api token goes here>")
.body("{\"group\":\"123456789000000000@g.us\", \"priority\":\"high\", \"message\":\"Hello world! This is a simple test message.\"}")
.asString();

2. Send media messages to users. Note the file must be updated first

Learn how to send multimedia messages and upload files here

HttpResponse response = Unirest.post("https://api.wassenger.com/v1/messages") .header("Content-Type", "application/json") .header("Token", "") .body("{\"phone\":\"+1234567890\",\"message\":\"Hello world! This is a test media message.\",\"media\":{\"file\":\"<24 characters length file ID>\"}}") .asString();

3. Send a text message that should be delivered now

HttpResponse response = Unirest.post("https://api.wassenger.com/v1/messages") .header("Content-Type", "application/json") .header("Token", "") .body("{\"phone\":\"+1234567890\",\"message\":\"Hello world! This is a simple test message.\",\"enqueue\":\"never\"}") .asString();

4. Send scheduled messages with a custom delay

HttpResponse response = Unirest.post("https://api.wassenger.com/v1/messages") .header("Content-Type", "application/json") .header("Token", "") .body("{\"phone\":\"+1234567890\",\"schedule\":{\"delayTo\":\"1h\"},\"message\":\"Hello world! This is a simple test message.\"}") .asString();

5. Send a scheduled message at a specific date with a valid ISO 8601 date

HttpResponse response = Unirest.post("https://api.wassenger.com/v1/messages") .header("Content-Type", "application/json") .header("Token", "") .body("{\"phone\":\"+1234567890\",\"deliverAt\":\"2019-01-01T11:00:00.410Z\",\"message\":\"Hello world! This is a simple test message.\"}") .asString();

🤩 🤖 Wassenger is a complete API solution for WhatsApp. Sign up for a 7-day free trial and get started in minutes!

Live API testing

You can live-test and play with the API directly from your browser.

Once you are done testing, get the auto-generated code example in your preferred programming language and you will be ready to go.

🤩 🤖 Wassenger is a complete communication platform and API solution for WhatsApp. Explore more than 100+ API use cases and automate anything on WhatsApp by signing up for a free trial and getting started in minutes!

FAQ

From where can I obtain the API code examples?

Please check the API documentation and select the ready-to-use code example in the desired language.

We provide code examples for most common languages and CLI tools, such as JavaScript, Node.js, Python, Ruby, PHP, Java, C#, Swift, Go and more.

Can I use Wassenger for chatbots?

Yes, you can build your chatbot with our API and webhooks. To do so, you need to subscribe to any Platform plan allowing you to implement chatbots on top of the API.

Explore more from our related article here.

How can I validate phone numbers?

You can validate whether a given phone number exists in WhatsApp or not, and therefore can receive messages in WhatsApp, by using the Number exists API endpoint.

Please note you must have at least one WhatsApp number connected to the platform to perform the validation.

Looking for more answers? Check out the extended FAQs.

Further useful resources

API Documentation 🖥️

For more details about the endpoint API, please check the documentation where you will find all the details about the accepted request params, possible success or error responses and ready-to-use code examples in multiple programming languages:

https://app.wassenger.com/docs/#tag/Messages/operation/createMessage

Ready to transform your WhatsApp communication?

Start automating your customer interactions today with Wassenger

Get Started Free