Sample code for 30+ languages & platforms
Swift 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 Swift Downloads

Swift

func chilkatTest() {
    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    let http = CkoHttp()!
    http.requireSslCertVerify = true
    //  Load the key for this One API integration from your application configuration.
    http.setRequestHeader(name: "Authorization", value: "ONE_API_KEY")
    http.setRequestHeader(name: "Accept", value: "application/json")

    var firstPhone: String? = "FIRST_PHONE_NUMBER"
    var secondPhone: String? = "SECOND_PHONE_NUMBER"
    let body = CkoJsonObject()!
    body.updateString(jsonPath: "messages[0].channel", value: "sms")
    body.updateString(jsonPath: "messages[0].to", value: firstPhone)
    body.updateString(jsonPath: "messages[0].content", value: "Your appointment with Lee is at 10:00.")
    body.updateString(jsonPath: "messages[0].clientMessageId", value: "appt-1042")
    body.updateString(jsonPath: "messages[1].channel", value: "sms")
    body.updateString(jsonPath: "messages[1].to", value: secondPhone)
    body.updateString(jsonPath: "messages[1].content", value: "Your appointment with Sam is at 14:30.")
    body.updateString(jsonPath: "messages[1].clientMessageId", value: "appt-1043")

    let resp = CkoHttpResponse()!
    var sent: Bool = http.httpJson(verb: "POST", url: "https://platform.clickatell.com/v1/message", json: body, contentType: "application/json", response: resp)
    if resp.statusCode.intValue == 0 {
        print("No HTTP response. Check the message report before resending.")
        return
    }

    let reply = CkoJsonObject()!
    var parsed: Bool = reply.load(json: resp.bodyStr)
    if (sent != true) || ((resp.statusCode.intValue != 202) && (resp.statusCode.intValue != 207)) {
        print("Send returned HTTP \(resp.statusCode.intValue)")
        if reply.hasMember(jsonPath: "error.description") == true {
            print("\(reply.string(of: "error.description")!)")
        }

        return
    }

    if reply.hasMember(jsonPath: "error.code") == true {
        if reply.isNull(of: "error.code") != true {
            print("Submission error \(reply.string(of: "error.code")!)  \(reply.string(of: "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.
    let results = CkoJsonObject()!
    var i: Int = 0
    while (i < 2) {
        body.i = i
        var appointment: String? = body.string(of: "messages[i].clientMessageId")
        var phone: String? = body.string(of: "messages[i].to")
        let result = CkoJsonObject()!
        var found: Bool = reply.findRecord2(arrayPath: "messages", relPath: "to", value: phone, caseSensitive: true, jsonObj: result)
        results.i = i
        results.updateString(jsonPath: "appointments[i].clientMessageId", value: appointment)
        if (found != true) || (result.jsonType(of: "accepted").intValue != 5) {
            results.updateString(jsonPath: "appointments[i].outcome", value: "unknown")
            print("\(appointment!) has no confirmed result.")
        }
        else {
            if result.bool(of: "accepted") == true {
                results.updateString(jsonPath: "appointments[i].outcome", value: "accepted")
                if result.jsonType(of: "apiMessageId").intValue == 1 {
                    results.updateString(jsonPath: "appointments[i].apiMessageId", value: result.string(of: "apiMessageId"))
                }

                print("\(appointment!) accepted.")
            }
            else {
                results.updateString(jsonPath: "appointments[i].outcome", value: "rejected")
                results.updateString(jsonPath: "appointments[i].reason", value: result.string(of: "error.description"))
                print("\(appointment!) rejected: \(result.string(of: "error.description")!)")
            }

        }

        i = i + 1
    }

    //  This demo writes a snapshot. In an application, update the two appointment records.
    var saved: Bool = results.writeFile(path: "appointment-send-results.json")
    if saved != true {
        print("Could not save the results. Reconcile the submitted messages before retrying.")
        return
    }

    print("Saved appointment-send-results.json")

}