Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.0.0+

Clickatell One API: Send WhatsApp reply buttons

See more Clickatell Examples

Send an appointment question with Confirm and Reschedule buttons within 24 hours of the customer’s last WhatsApp message. This standalone example ends when Clickatell returns the send result. The response gives the outbound message ID. A customer’s eventual selection is a separate inbound event.

Clickatell API reference

Chilkat Objective-C Downloads

Objective-C
#import <CkoHttp.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"];

CkoJsonObject *body = [[CkoJsonObject alloc] init];
[body UpdateString: @"messages[0].channel" value: @"whatsapp"];
[body UpdateString: @"messages[0].to" value: @"PHONE_NUMBER"];
[body UpdateString: @"messages[0].button.body" value: @"Can you attend your appointment tomorrow at 10:00?"];
//  The visible titles can be translated. Each button has a distinct application ID.
[body UpdateString: @"messages[0].button.items[0].type" value: @"reply"];
[body UpdateString: @"messages[0].button.items[0].title" value: @"Confirm"];
[body UpdateString: @"messages[0].button.items[0].postbackData" value: @"appt-1042-confirm"];
[body UpdateString: @"messages[0].button.items[1].type" value: @"reply"];
[body UpdateString: @"messages[0].button.items[1].title" value: @"Reschedule"];
[body UpdateString: @"messages[0].button.items[1].postbackData" value: @"appt-1042-reschedule"];

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"]);