How to Send a WhatsApp Message in 30 Seconds Using Java and Gradle

March 29, 2022
Written by
Felistas Ngumi
Contributor
Opinions expressed by Twilio contributors are their own
Reviewed by
Diane Phan
Twilion

header - How to Send a WhatsApp Message in 30 Seconds Using Java and Gradle

Approximately Two billion people across the globe use WhatsApp as a communication medium. It is fast and a convenient app to use as it does not only provide peer to peer messaging, but it is also used by companies and entrepreneurs to run their businesses through WhatAapp business. Over the years, WhatsApp has proven to be an important tool to engage with customers as at least more than 58% of its users use it more than once in a day.

The Twilio WhatsApp Business API is handy and secure and enables you to accomplish a number of tasks including sending and receiving media files, sharing map locations and driving engagement with your users. It is a great enabler for businesses worldwide. So, why not scale your business and leverage tools that have proven to work over time?

In this tutorial, I will take you through how to send a WhatApp message in 30 seconds using Java and Gradle

Prerequisites

  1. Java 11.
  2. Gradle 7.1.
  3. Twilio account.
  4. Intellij Idea Community Edition.
  5. A smartphone with WhatsApp installed.

Twilio Project Setup and Sandbox Activation

After creating and verifying your account on Twilio, navigate to the Twilio console and create a new account, stating the Twilio project you would like to use, in this case Messaging. In the resulting dashboard, take a note of the ACCOUNT SID and AUTH TOKEN. We will need them later in our code.

Next, select Messaging on the left menu, Try it out, then click on Send a WhatsApp message to activate the WhatsApp sandbox.

twilio console to navigate to twilio whatsapp sandbox

A similar screen should appear as shown below:

join the twilio sandbox

Take your WhatsApp enabled device and text the unique join code to the WhatsApp number displayed on the screen in order to join the Twilio Sandbox. Shortly after you send the message you should receive a reply from Twilio indicating that your mobile number is connected to the sandbox and can start sending and receiving messages.

If you intend to test your application with additional smartphones, then you must repeat the sandbox registration process with each of them.

Java Project Setup

After downloading and installing the Intellij community edition, open and create a new Gradle project. Select Java 11 as the SDK and specify your project folder in the resulting window.

new gradle project in intellij idea

This bootstraps a Gradle project in the folder you specified.

In the root folder, navigate to the /src/main/java folder and create a file called WhatsApp.java inside it. This is where our project code will live.

Locate your build.gradle  file and add the Twilio dependency to be part of the project:

implementation group: "com.twilio.sdk", name: "twilio", version: "8.27.0"

This will enable us to use the Twilio API in our project.

Next, open the WhatsApp.java file that you just created and add the following lines of code:


import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;

public class WhatsApp {
  public static final String ACCOUNT_SID = System.getenv("ACCOUNT_SID");
  public static final String AUTH_TOKEN = System.getenv("AUTH_TOKEN");
  public static void main(String[] args) {
      Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
      Message message = Message.creator(
                      new com.twilio.type.PhoneNumber("whatsapp:<your_phone_number>"),
                      new com.twilio.type.PhoneNumber("whatsapp:<your_twilio_whatsapp_test_number>"),
                      "Hello, from Twilio")
              .create();
      System.out.println(message.getSid());
  }
}

The above code initializes Twilio client with an ACCOUNT SID and AUTH TOKEN. We then construct a message, specifying who we are sending the message to, who it is from, and the body of the message.

The message is then sent when the create() function is run. A successful operation of this function will print the Message SID on the terminal.

NOTE: Ensure to replace your_phone_number and whatapp_test_number, with their respective values. Your phone number should include your country’s code e.g +1 for US residents or +44 for UK residents.

Inside the Intellij console, select Run and click "Edit Configurations" in the console. Add a new run configuration for the "WhatsApp" application to include the ACCOUNT_SID and AUTH_TOKEN environment variables, as seen in the image below:

edit configurations to add twilio environment variables

Next, run the project and you should receive a WhatsApp message on your device as shown below.

Twilio text message on WhatsApp

Conclusion

Congratulations on making it to the end. In this tutorial, we have learned how to send WhatsApp messages using the Twilio API within a short period of time. You can get more insights on the Twilio Java library and build cool stuff with it.

Felistas is a software developer who enjoys writing technical articles from time to time and solving issues via code.