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

Tcl

load ./chilkat.dll

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
set http [new_CkHttp]

CkHttp_put_RequireSslCertVerify $http 1
#  Load the key for this One API integration from your application configuration.
CkHttp_SetRequestHeader $http "Authorization" "ONE_API_KEY"
CkHttp_SetRequestHeader $http "Accept" "application/json"

set firstPhone "FIRST_PHONE_NUMBER"
set secondPhone "SECOND_PHONE_NUMBER"
set body [new_CkJsonObject]

CkJsonObject_UpdateString $body "messages[0].channel" "sms"
CkJsonObject_UpdateString $body "messages[0].to" $firstPhone
CkJsonObject_UpdateString $body "messages[0].content" "Your appointment with Lee is at 10:00."
CkJsonObject_UpdateString $body "messages[0].clientMessageId" "appt-1042"
CkJsonObject_UpdateString $body "messages[1].channel" "sms"
CkJsonObject_UpdateString $body "messages[1].to" $secondPhone
CkJsonObject_UpdateString $body "messages[1].content" "Your appointment with Sam is at 14:30."
CkJsonObject_UpdateString $body "messages[1].clientMessageId" "appt-1043"

set resp [new_CkHttpResponse]

set sent [CkHttp_HttpJson $http "POST" "https://platform.clickatell.com/v1/message" $body "application/json" $resp]
if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
    puts "No HTTP response. Check the message report before resending."
    delete_CkHttp $http
    delete_CkJsonObject $body
    delete_CkHttpResponse $resp
    exit
}

set reply [new_CkJsonObject]

set parsed [CkJsonObject_Load $reply [CkHttpResponse_bodyStr $resp]]
if {expr [$sent != 1]  ||  [expr [[CkHttpResponse_get_StatusCode $resp] != 202]  &&  [[CkHttpResponse_get_StatusCode $resp] != 207]]} then {
    puts "Send returned HTTP [CkHttpResponse_get_StatusCode $resp]"
    if {[CkJsonObject_HasMember $reply "error.description"] == 1} then {
        puts [CkJsonObject_stringOf $reply error.description]
    }

    delete_CkHttp $http
    delete_CkJsonObject $body
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    exit
}

if {[CkJsonObject_HasMember $reply "error.code"] == 1} then {
    if {[CkJsonObject_IsNullOf $reply "error.code"] != 1} then {
        puts "Submission error [CkJsonObject_stringOf $reply error.code]  [CkJsonObject_stringOf $reply error.description]"
        delete_CkHttp $http
        delete_CkJsonObject $body
        delete_CkHttpResponse $resp
        delete_CkJsonObject $reply
        exit
    }

}

#  Look up each submitted appointment in the reply using its unique recipient.
#  This works even if the result order changes or clientMessageId is absent.
set results [new_CkJsonObject]

set i 0
while {$i < 2} {
    CkJsonObject_put_I $body $i
    set appointment [CkJsonObject_stringOf $body "messages[i].clientMessageId"]
    set phone [CkJsonObject_stringOf $body "messages[i].to"]
    set result [new_CkJsonObject]

    set found [CkJsonObject_FindRecord2 $reply "messages" "to" $phone 1 $result]
    CkJsonObject_put_I $results $i
    CkJsonObject_UpdateString $results "appointments[i].clientMessageId" $appointment
    if {expr [$found != 1]  ||  [[CkJsonObject_JsonTypeOf $result "accepted"] != 5]} then {
        CkJsonObject_UpdateString $results "appointments[i].outcome" "unknown"
        puts "$appointment has no confirmed result."
    }     else {
        if {[CkJsonObject_BoolOf $result "accepted"] == 1} then {
            CkJsonObject_UpdateString $results "appointments[i].outcome" "accepted"
            if {[CkJsonObject_JsonTypeOf $result "apiMessageId"] == 1} then {
                CkJsonObject_UpdateString $results "appointments[i].apiMessageId" [CkJsonObject_stringOf $result "apiMessageId"]
            }

            puts "$appointment accepted."
        }         else {
            CkJsonObject_UpdateString $results "appointments[i].outcome" "rejected"
            CkJsonObject_UpdateString $results "appointments[i].reason" [CkJsonObject_stringOf $result "error.description"]
            puts "$appointment rejected: [CkJsonObject_stringOf $result error.description]"
        }

    }

    set i [expr $i + 1]
}
#  This demo writes a snapshot. In an application, update the two appointment records.
set saved [CkJsonObject_WriteFile $results "appointment-send-results.json"]
if {$saved != 1} then {
    puts "Could not save the results. Reconcile the submitted messages before retrying."
    delete_CkHttp $http
    delete_CkJsonObject $body
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    delete_CkJsonObject $results
    delete_CkJsonObject $result
    exit
}

puts "Saved appointment-send-results.json"

delete_CkHttp $http
delete_CkJsonObject $body
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
delete_CkJsonObject $results
delete_CkJsonObject $result