How to send messages to a WhatsApp group in Java

Using the WhatsMate WA Gateway REST API



This article shows you how to send a message to a WhatsApp group in Java.

You MUST obtain the secret gateway number by signing up for a Forever Green account before you can send a message to a WhatsApp group. Instructions are available on the official site.

To send a WhatsApp group message from your Java application, do this:

  1. Create a new group with a unique name. The API won’t work if it’s not unique.
    • Create a new WhatsApp group

  2. Add the secret gateway to the group.
    • Name the WhatsApp group

  3. Say “Hi” to the group to let the gateway recognize the new group.
  4. Copy the following source code to a Java file named WhatsappSender.java.
    import java.io.BufferedReader;
    import java.io.OutputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    public class WhatsappSender {
    // TODO: Replace the following with your gateway instance ID, Forever Green Client ID and Secret below.
    private static final String INSTANCE_ID = "YOUR_INSTANCE_ID_HERE";
    private static final String CLIENT_ID = "YOUR_CLIENT_ID_HERE";
    private static final String CLIENT_SECRET = "YOUR_CLIENT_SECRET_HERE";
    private static final String WA_GATEWAY_URL = "http://api.whatsmate.net/v3/whatsapp/group/text/message/" + INSTANCE_ID;
    /**
    * Entry Point
    */
    public static void main(String[] args) throws Exception {
    String groupAdmin = "12025550108"; // TODO: Specify the WhatsApp number of the group creator, including the country code
    String groupName = "Happy Club"; // TODO: Specify the name of the group
    String message = "Guys, let's party tonight!"; // TODO: Specify the content of your message
    WhatsappSender.sendGroupMessage(groupAdmin, groupName, message);
    }
    /**
    * Sends out a WhatsApp message to a group
    */
    public static void sendGroupMessage(String groupAdmin, String groupName, String message) throws Exception {
    // TODO: Should have used a 3rd party library to make a JSON string from an object
    String jsonPayload = new StringBuilder()
    .append("{")
    .append("\"group_admin\":\"")
    .append(groupAdmin)
    .append("\",")
    .append("\"group_name\":\"")
    .append(groupName)
    .append("\",")
    .append("\"message\":\"")
    .append(message)
    .append("\"")
    .append("}")
    .toString();
    URL url = new URL(WA_GATEWAY_URL);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("X-WM-CLIENT-ID", CLIENT_ID);
    conn.setRequestProperty("X-WM-CLIENT-SECRET", CLIENT_SECRET);
    conn.setRequestProperty("Content-Type", "application/json");
    OutputStream os = conn.getOutputStream();
    os.write(jsonPayload.getBytes());
    os.flush();
    os.close();
    int statusCode = conn.getResponseCode();
    System.out.println("Response from WA Gateway: \n");
    System.out.println("Status Code: " + statusCode);
    BufferedReader br = new BufferedReader(new InputStreamReader(
    (statusCode == 200) ? conn.getInputStream() : conn.getErrorStream()
    ));
    String output;
    while ((output = br.readLine()) != null) {
    System.out.println(output);
    }
    conn.disconnect();
    }
    }
  5. Customize the TODO lines in the Java program:
    • Specify your gateway instance ID on line 9.
    • Specify your Client ID and Client secret on lines 10 and 11.
    • Specify the group admin number (i.e. your WhatsApp number including the country code) on line 18.
    • Specify the group name (e.g. Happy Club) on line 19.
    • Specify the content of the message on line 20.
  6. Compile the Java file: javac WhatsappSender.java
  7. Execute the class to send your message: java WhatsappSender

As mentioned at the beginning of this tutorial, you will need a trial account to call the above API. Go sign up now.

Happy coding :)