Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.0.0+

Clickatell One API: Schedule a time-limited SMS reminder

See more Clickatell Examples

Schedule a reminder 15 minutes from now in UTC. The scheduledDeliveryTime includes its time zone and validityPeriod is expressed in minutes. Set both from the appointment and the period for which the reminder will still be useful. Use the returned message ID for a later status lookup.

Supply an international number as digits. Set the sender to the provisioned number when the integration requires it.

Clickatell API reference

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkDateTime.pb"

Procedure ChilkatExample()

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.
    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttp::setCkRequireSslCertVerify(http, 1)
    ;  Load the key for this One API integration from your application configuration.
    CkHttp::ckSetRequestHeader(http,"Authorization","ONE_API_KEY")
    CkHttp::ckSetRequestHeader(http,"Accept","application/json")

    schedule.i = CkDateTime::ckCreate()
    If schedule.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkDateTime::ckSetFromCurrentSystemTime(schedule)
    CkDateTime::ckAddSeconds(schedule,900)
    sendAt.s = CkDateTime::ckGetAsTimestamp(schedule,0)
    body.i = CkJsonObject::ckCreate()
    If body.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(body,"messages[0].channel","sms")
    CkJsonObject::ckUpdateString(body,"messages[0].to","PHONE_NUMBER")
    CkJsonObject::ckUpdateString(body,"messages[0].content","Appointment reminder: please check in at reception.")
    CkJsonObject::ckUpdateString(body,"messages[0].scheduledDeliveryTime",sendAt)
    CkJsonObject::ckUpdateInt(body,"messages[0].validityPeriod",60)
    sender.s = ""
    If sender <> ""
        CkJsonObject::ckUpdateString(body,"messages[0].from",sender)
    EndIf

    Debug "Requested send time (UTC): " + sendAt

    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    sent.i = CkHttp::ckHttpJson(http,"POST","https://platform.clickatell.com/v1/message",body,"application/json",resp)
    If CkHttpResponse::ckStatusCode(resp) = 0
        Debug "No HTTP response. Check the message report before resending."
        CkHttp::ckDispose(http)
        CkDateTime::ckDispose(schedule)
        CkJsonObject::ckDispose(body)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    reply.i = CkJsonObject::ckCreate()
    If reply.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    parsed.i = CkJsonObject::ckLoad(reply,CkHttpResponse::ckBodyStr(resp))
    If (sent <> 1) OR (CkHttpResponse::ckStatusCode(resp) <> 202)
        Debug "Send returned HTTP " + Str(CkHttpResponse::ckStatusCode(resp))
        If CkJsonObject::ckHasMember(reply,"error.description") = 1
            Debug CkJsonObject::ckStringOf(reply,"error.description")
        EndIf

        CkHttp::ckDispose(http)
        CkDateTime::ckDispose(schedule)
        CkJsonObject::ckDispose(body)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(reply)
        ProcedureReturn
    EndIf

    If CkJsonObject::ckHasMember(reply,"error.code") = 1
        If CkJsonObject::ckIsNullOf(reply,"error.code") <> 1
            Debug "Submission error " + CkJsonObject::ckStringOf(reply,"error.code") + "  " + CkJsonObject::ckStringOf(reply,"error.description")
            CkHttp::ckDispose(http)
            CkDateTime::ckDispose(schedule)
            CkJsonObject::ckDispose(body)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(reply)
            ProcedureReturn
        EndIf

    EndIf

    ;  HTTP acceptance and the recipient result are separate checks.
    If (parsed <> 1) OR (CkJsonObject::ckJsonTypeOf(reply,"messages[0].accepted") <> 5)
        Debug "Missing recipient result. Check the message report."
        CkHttp::ckDispose(http)
        CkDateTime::ckDispose(schedule)
        CkJsonObject::ckDispose(body)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(reply)
        ProcedureReturn
    EndIf

    If CkJsonObject::ckBoolOf(reply,"messages[0].accepted") <> 1
        Debug "Message rejected: " + CkJsonObject::ckStringOf(reply,"messages[0].error.code") + "  " + CkJsonObject::ckStringOf(reply,"messages[0].error.description")
        CkHttp::ckDispose(http)
        CkDateTime::ckDispose(schedule)
        CkJsonObject::ckDispose(body)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(reply)
        ProcedureReturn
    EndIf

    If CkJsonObject::ckJsonTypeOf(reply,"messages[0].apiMessageId") <> 1
        Debug "Accepted without a message ID. Check the message report."
        CkHttp::ckDispose(http)
        CkDateTime::ckDispose(schedule)
        CkJsonObject::ckDispose(body)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(reply)
        ProcedureReturn
    EndIf

    Debug "Accepted. Message ID: " + CkJsonObject::ckStringOf(reply,"messages[0].apiMessageId")


    CkHttp::ckDispose(http)
    CkDateTime::ckDispose(schedule)
    CkJsonObject::ckDispose(body)
    CkHttpResponse::ckDispose(resp)
    CkJsonObject::ckDispose(reply)


    ProcedureReturn
EndProcedure