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

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

    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) {
        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
        }

    }

    //  HTTP acceptance and the recipient result are separate checks.
    if (parsed != true) || (reply.jsonType(of: "messages[0].accepted").intValue != 5) {
        print("Missing recipient result. Check the message report.")
        return
    }

    if reply.bool(of: "messages[0].accepted") != true {
        print("Message rejected: \(reply.string(of: "messages[0].error.code")!)  \(reply.string(of: "messages[0].error.description")!)")
        return
    }

    if reply.jsonType(of: "messages[0].apiMessageId").intValue != 1 {
        print("Accepted without a message ID. Check the message report.")
        return
    }

    print("Accepted. Message ID: \(reply.string(of: "messages[0].apiMessageId")!)")

}