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

Clickatell One API: Upload and send a WhatsApp PDF

See more Clickatell Examples

Send a local invoice.pdf during the customer’s open WhatsApp service window. Upload the binary PDF for one recipient, then reference the returned fileId in a separate message to the same recipient. The upload and message submission have separate acceptance results.

Clickatell API reference

Chilkat Java Downloads

Java
import com.chilkatsoft.chilkat2.BinData;
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");

        String recipient = "PHONE_NUMBER";
        BinData pdf = new BinData();
        try {
            pdf.loadFile("invoice.pdf");
        } catch (ChilkatException e) {
            System.out.println("Could not read invoice.pdf.");
            return;
        }

        // Restrict this customer's invoice to that customer's number.
        http.setUrlVar("recipient", recipient);
        HttpResponse upload = new HttpResponse();
        boolean uploaded;
        try {
            http.httpBd("POST", "https://platform.clickatell.com/v1/media?fileName=invoice.pdf&to={$recipient}", pdf, "application/pdf", upload);
            uploaded = true;
        } catch (ChilkatException e) {
            uploaded = false;
        }
        if ((!uploaded) || (upload.getStatusCode() != 201)) {
            System.out.println("PDF upload failed. HTTP status " + upload.getStatusCode());
            return;
        }

        JsonObject media = new JsonObject();
        boolean decoded;
        try {
            media.load(upload.getBodyStr());
            decoded = true;
        } catch (ChilkatException e) {
            decoded = false;
        }
        if ((!decoded) || (!media.boolOf("accepted"))) {
            System.out.println("Upload was not accepted: " + media.stringOf("error.description"));
            return;
        }

        String fileId = media.stringOf("fileId");
        if ((media.jsonTypeOf("fileId") != 1) || ("".equals(fileId))) {
            System.out.println("Upload returned no file ID.");
            return;
        }

        JsonObject body = new JsonObject();
        body.updateString("messages[0].channel", "whatsapp");
        body.updateString("messages[0].to", recipient);
        body.updateString("messages[0].media.fileId", fileId);
        body.updateString("messages[0].media.contentType", "application/pdf");
        body.updateString("messages[0].media.fileName", "invoice.pdf");

        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"));
    }
}