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

Objective-C
#import <CkoHttp.h>
#import <NSString.h>
#import <CkoBinData.h>
#import <CkoHttpResponse.h>
#import <CkoJsonObject.h>

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
//  Load the key for this One API integration from your application configuration.
[http SetRequestHeader: @"Authorization" value: @"ONE_API_KEY"];
[http SetRequestHeader: @"Accept" value: @"application/json"];

NSString *recipient = @"PHONE_NUMBER";
CkoBinData *pdf = [[CkoBinData alloc] init];
BOOL loaded = [pdf LoadFile: @"invoice.pdf"];
if (loaded != YES) {
    NSLog(@"%@",@"Could not read invoice.pdf.");
    return;
}

//  Restrict this customer's invoice to that customer's number.
[http SetUrlVar: @"recipient" value: recipient];
CkoHttpResponse *upload = [[CkoHttpResponse alloc] init];
BOOL uploaded = [http HttpBd: @"POST" url: @"https://platform.clickatell.com/v1/media?fileName=invoice.pdf&to={$recipient}" bd: pdf contentType: @"application/pdf" response: upload];
if ((uploaded != YES) || ([upload.StatusCode intValue] != 201)) {
    NSLog(@"%@%d",@"PDF upload failed. HTTP status ",[upload.StatusCode intValue]);
    return;
}

CkoJsonObject *media = [[CkoJsonObject alloc] init];
BOOL decoded = [media Load: upload.BodyStr];
if ((decoded != YES) || ([media BoolOf: @"accepted"] != YES)) {
    NSLog(@"%@%@",@"Upload was not accepted: ",[media StringOf: @"error.description"]);
    return;
}

NSString *fileId = [media StringOf: @"fileId"];
if (([[media JsonTypeOf: @"fileId"] intValue] != 1) || (fileId == @"")) {
    NSLog(@"%@",@"Upload returned no file ID.");
    return;
}

CkoJsonObject *body = [[CkoJsonObject alloc] init];
[body UpdateString: @"messages[0].channel" value: @"whatsapp"];
[body UpdateString: @"messages[0].to" value: recipient];
[body UpdateString: @"messages[0].media.fileId" value: fileId];
[body UpdateString: @"messages[0].media.contentType" value: @"application/pdf"];
[body UpdateString: @"messages[0].media.fileName" value: @"invoice.pdf"];

CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
BOOL sent = [http HttpJson: @"POST" url: @"https://platform.clickatell.com/v1/message" json: body contentType: @"application/json" response: resp];
if ([resp.StatusCode intValue] == 0) {
    NSLog(@"%@",@"No HTTP response. Check the message report before resending.");
    return;
}

CkoJsonObject *reply = [[CkoJsonObject alloc] init];
BOOL parsed = [reply Load: resp.BodyStr];
if ((sent != YES) || ([resp.StatusCode intValue] != 202)) {
    NSLog(@"%@%d",@"Send returned HTTP ",[resp.StatusCode intValue]);
    if ([reply HasMember: @"error.description"] == YES) {
        NSLog(@"%@",[reply StringOf: @"error.description"]);
    }

    return;
}

if ([reply HasMember: @"error.code"] == YES) {
    if ([reply IsNullOf: @"error.code"] != YES) {
        NSLog(@"%@%@%@%@",@"Submission error ",[reply StringOf: @"error.code"],@"  ",[reply StringOf: @"error.description"]);
        return;
    }

}

//  HTTP acceptance and the recipient result are separate checks.
if ((parsed != YES) || ([[reply JsonTypeOf: @"messages[0].accepted"] intValue] != 5)) {
    NSLog(@"%@",@"Missing recipient result. Check the message report.");
    return;
}

if ([reply BoolOf: @"messages[0].accepted"] != YES) {
    NSLog(@"%@%@%@%@",@"Message rejected: ",[reply StringOf: @"messages[0].error.code"],@"  ",[reply StringOf: @"messages[0].error.description"]);
    return;
}

if ([[reply JsonTypeOf: @"messages[0].apiMessageId"] intValue] != 1) {
    NSLog(@"%@",@"Accepted without a message ID. Check the message report.");
    return;
}

NSLog(@"%@%@",@"Accepted. Message ID: ",[reply StringOf: @"messages[0].apiMessageId"]);