Sample code for 30+ languages & platforms
Swift 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 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 recipient: String? = "PHONE_NUMBER"
    let pdf = CkoBinData()!
    var loaded: Bool = pdf.loadFile(path: "invoice.pdf")
    if loaded != true {
        print("Could not read invoice.pdf.")
        return
    }

    //  Restrict this customer's invoice to that customer's number.
    http.setUrlVar(name: "recipient", value: recipient)
    let upload = CkoHttpResponse()!
    var uploaded: Bool = http.httpBd(verb: "POST", url: "https://platform.clickatell.com/v1/media?fileName=invoice.pdf&to={$recipient}", bd: pdf, contentType: "application/pdf", response: upload)
    if (uploaded != true) || (upload.statusCode.intValue != 201) {
        print("PDF upload failed. HTTP status \(upload.statusCode.intValue)")
        return
    }

    let media = CkoJsonObject()!
    var decoded: Bool = media.load(json: upload.bodyStr)
    if (decoded != true) || (media.bool(of: "accepted") != true) {
        print("Upload was not accepted: \(media.string(of: "error.description")!)")
        return
    }

    var fileId: String? = media.string(of: "fileId")
    if (media.jsonType(of: "fileId").intValue != 1) || (fileId == "") {
        print("Upload returned no file ID.")
        return
    }

    let body = CkoJsonObject()!
    body.updateString(jsonPath: "messages[0].channel", value: "whatsapp")
    body.updateString(jsonPath: "messages[0].to", value: recipient)
    body.updateString(jsonPath: "messages[0].media.fileId", value: fileId)
    body.updateString(jsonPath: "messages[0].media.contentType", value: "application/pdf")
    body.updateString(jsonPath: "messages[0].media.fileName", value: "invoice.pdf")

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

}