Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

Clickatell One API: Send an approved WhatsApp shipment template

See more Clickatell Examples

Send a business-initiated shipment update, including outside an open customer service window. First approve a template on the WhatsApp integration with two positional body placeholders for the order number and delivery date. Supply its exact name and approved language. The keys 1 and 2 refer to those placeholders in order.

Clickatell API reference

Chilkat Java Downloads

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

    public static void main(String[] args) {
        // Assumes Chilkat is unlocked. See Global Unlock Sample.
        Http http = new Http();
        http.setRequireSslCertVerify(true);
        // Load the key for this One API integration from your application configuration.
        http.setRequestHeader("Authorization", "ONE_API_KEY");
        http.setRequestHeader("Accept", "application/json");

        // Example approved wording: Order {{1}} is due for delivery on {{2}}.
        JsonObject body = new JsonObject();
        body.updateString("messages[0].channel", "whatsapp");
        body.updateString("messages[0].to", "PHONE_NUMBER");
        body.updateString("messages[0].template.templateName", "shipment_update");
        body.updateString("messages[0].template.language", "en_US");
        body.updateString("messages[0].template.body.parameters.1", "ORD-1042");
        body.updateString("messages[0].template.body.parameters.2", "March 19");

        HttpResponse resp = new HttpResponse();
        boolean sent;
        try {
            http.httpJson("POST", "https://platform.clickatell.com/v1/message", body, "application/json", resp);
            sent = true;
        } catch (ChilkatException e) {
            sent = false;
        }
        if (resp.getStatusCode() == 0) {
            System.out.println("No HTTP response. Check the message report before resending.");
            return;
        }

        JsonObject reply = new JsonObject();
        boolean parsed;
        try {
            reply.load(resp.getBodyStr());
            parsed = true;
        } catch (ChilkatException e) {
            parsed = false;
        }
        if ((!sent) || (resp.getStatusCode() != 202)) {
            System.out.println("Send returned HTTP " + resp.getStatusCode());
            if (reply.hasMember("error.description")) {
                System.out.println(reply.stringOf("error.description"));
            }

            return;
        }

        if (reply.hasMember("error.code")) {
            if (!reply.isNullOf("error.code")) {
                System.out.println("Submission error " + reply.stringOf("error.code") + "  " + reply.stringOf("error.description"));
                return;
            }
        }

        // HTTP acceptance and the recipient result are separate checks.
        if ((!parsed) || (reply.jsonTypeOf("messages[0].accepted") != 5)) {
            System.out.println("Missing recipient result. Check the message report.");
            return;
        }

        if (!reply.boolOf("messages[0].accepted")) {
            System.out.println("Message rejected: " + reply.stringOf("messages[0].error.code") + "  " + reply.stringOf("messages[0].error.description"));
            return;
        }

        if (reply.jsonTypeOf("messages[0].apiMessageId") != 1) {
            System.out.println("Accepted without a message ID. Check the message report.");
            return;
        }

        System.out.println("Accepted. Message ID: " + reply.stringOf("messages[0].apiMessageId"));
    }
}