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

Clickatell One API: Send personalized SMS reminders in a batch

See more Clickatell Examples

Send two appointments in one request with a different text and application ID for each. Use two distinct recipient numbers in international digits. For a US two-way integration, add its provisioned from number to each message.

Read each recipient result even when HTTP 207 reports a mixed outcome. Save the accepted provider ID against the appointment. A missing result calls for checking the message report before retrying. A client message ID helps correlate records and does not guarantee deduplication.

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

        String firstPhone = "FIRST_PHONE_NUMBER";
        String secondPhone = "SECOND_PHONE_NUMBER";
        JsonObject body = new JsonObject();
        body.updateString("messages[0].channel", "sms");
        body.updateString("messages[0].to", firstPhone);
        body.updateString("messages[0].content", "Your appointment with Lee is at 10:00.");
        body.updateString("messages[0].clientMessageId", "appt-1042");
        body.updateString("messages[1].channel", "sms");
        body.updateString("messages[1].to", secondPhone);
        body.updateString("messages[1].content", "Your appointment with Sam is at 14:30.");
        body.updateString("messages[1].clientMessageId", "appt-1043");

        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();
        reply.load(resp.getBodyStr());
        if ((!sent) || ((resp.getStatusCode() != 202) && (resp.getStatusCode() != 207))) {
            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;
            }
        }

        // Look up each submitted appointment in the reply using its unique recipient.
        // This works even if the result order changes or clientMessageId is absent.
        JsonObject results = new JsonObject();
        int i = 0;
        while (i < 2) {
            body.setI(i);
            String appointment = body.stringOf("messages[i].clientMessageId");
            String phone = body.stringOf("messages[i].to");
            JsonObject result = new JsonObject();
            boolean found;
            try {
                reply.findRecord2("messages", "to", phone, true, result);
                found = true;
            } catch (ChilkatException e) {
                found = false;
            }
            results.setI(i);
            results.updateString("appointments[i].clientMessageId", appointment);
            if ((!found) || (result.jsonTypeOf("accepted") != 5)) {
                results.updateString("appointments[i].outcome", "unknown");
                System.out.println(appointment + " has no confirmed result.");
            } else {
                if (result.boolOf("accepted")) {
                    results.updateString("appointments[i].outcome", "accepted");
                    if (result.jsonTypeOf("apiMessageId") == 1) {
                        results.updateString("appointments[i].apiMessageId", result.stringOf("apiMessageId"));
                    }

                    System.out.println(appointment + " accepted.");
                } else {
                    results.updateString("appointments[i].outcome", "rejected");
                    results.updateString("appointments[i].reason", result.stringOf("error.description"));
                    System.out.println(appointment + " rejected: " + result.stringOf("error.description"));
                }
            }

            i = i + 1;
        }

        // This demo writes a snapshot. In an application, update the two appointment records.
        try {
            results.writeFile("appointment-send-results.json");
        } catch (ChilkatException e) {
            System.out.println("Could not save the results. Reconcile the submitted messages before retrying.");
            return;
        }

        System.out.println("Saved appointment-send-results.json");
    }
}