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

Objective-C
#import <CkoHttp.h>
#import <NSString.h>
#import <CkoJsonObject.h>
#import <CkoHttpResponse.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 *firstPhone = @"FIRST_PHONE_NUMBER";
NSString *secondPhone = @"SECOND_PHONE_NUMBER";
CkoJsonObject *body = [[CkoJsonObject alloc] init];
[body UpdateString: @"messages[0].channel" value: @"sms"];
[body UpdateString: @"messages[0].to" value: firstPhone];
[body UpdateString: @"messages[0].content" value: @"Your appointment with Lee is at 10:00."];
[body UpdateString: @"messages[0].clientMessageId" value: @"appt-1042"];
[body UpdateString: @"messages[1].channel" value: @"sms"];
[body UpdateString: @"messages[1].to" value: secondPhone];
[body UpdateString: @"messages[1].content" value: @"Your appointment with Sam is at 14:30."];
[body UpdateString: @"messages[1].clientMessageId" value: @"appt-1043"];

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) && ([resp.StatusCode intValue] != 207))) {
    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;
    }

}

//  Look up each submitted appointment in the reply using its unique recipient.
//  This works even if the result order changes or clientMessageId is absent.
CkoJsonObject *results = [[CkoJsonObject alloc] init];
int i = 0;
while ((i < 2)) {
    body.I = [NSNumber numberWithInt: i];
    NSString *appointment = [body StringOf: @"messages[i].clientMessageId"];
    NSString *phone = [body StringOf: @"messages[i].to"];
    CkoJsonObject *result = [[CkoJsonObject alloc] init];
    BOOL found = [reply FindRecord2: @"messages" relPath: @"to" value: phone caseSensitive: YES jsonObj: result];
    results.I = [NSNumber numberWithInt: i];
    [results UpdateString: @"appointments[i].clientMessageId" value: appointment];
    if ((found != YES) || ([[result JsonTypeOf: @"accepted"] intValue] != 5)) {
        [results UpdateString: @"appointments[i].outcome" value: @"unknown"];
        NSLog(@"%@%@",appointment,@" has no confirmed result.");
    }
    else {
        if ([result BoolOf: @"accepted"] == YES) {
            [results UpdateString: @"appointments[i].outcome" value: @"accepted"];
            if ([[result JsonTypeOf: @"apiMessageId"] intValue] == 1) {
                [results UpdateString: @"appointments[i].apiMessageId" value: [result StringOf: @"apiMessageId"]];
            }

            NSLog(@"%@%@",appointment,@" accepted.");
        }
        else {
            [results UpdateString: @"appointments[i].outcome" value: @"rejected"];
            [results UpdateString: @"appointments[i].reason" value: [result StringOf: @"error.description"]];
            NSLog(@"%@%@%@",appointment,@" rejected: ",[result StringOf: @"error.description"]);
        }

    }

    i = i + 1;
}

//  This demo writes a snapshot. In an application, update the two appointment records.
BOOL saved = [results WriteFile: @"appointment-send-results.json"];
if (saved != YES) {
    NSLog(@"%@",@"Could not save the results. Reconcile the submitted messages before retrying.");
    return;
}

NSLog(@"%@",@"Saved appointment-send-results.json");