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

Xbase++
LOCAL oHttp
LOCAL cFirstPhone
LOCAL cSecondPhone
LOCAL oBody
LOCAL oResp
LOCAL nSent
LOCAL oReply
LOCAL nParsed
LOCAL oResults
LOCAL i
LOCAL cAppointment
LOCAL cPhone
LOCAL oResult
LOCAL nFound
LOCAL nSaved

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
//  Load the key for this One API integration from your application configuration.
oHttp:SetRequestHeader("Authorization", "ONE_API_KEY")
oHttp:SetRequestHeader("Accept", "application/json")

cFirstPhone := "FIRST_PHONE_NUMBER"
cSecondPhone := "SECOND_PHONE_NUMBER"
oBody := CreateObject("Chilkat.JsonObject")
oBody:UpdateString("messages[0].channel", "sms")
oBody:UpdateString("messages[0].to", cFirstPhone)
oBody:UpdateString("messages[0].content", "Your appointment with Lee is at 10:00.")
oBody:UpdateString("messages[0].clientMessageId", "appt-1042")
oBody:UpdateString("messages[1].channel", "sms")
oBody:UpdateString("messages[1].to", cSecondPhone)
oBody:UpdateString("messages[1].content", "Your appointment with Sam is at 14:30.")
oBody:UpdateString("messages[1].clientMessageId", "appt-1043")

oResp := CreateObject("Chilkat.HttpResponse")
nSent := oHttp:HttpJson("POST", "https://platform.clickatell.com/v1/message", oBody, "application/json", oResp)
IF (oResp:StatusCode == 0)
    ? "No HTTP response. Check the message report before resending."
    oHttp:destroy()
    oBody:destroy()
    oResp:destroy()
    RETURN
ENDIF

oReply := CreateObject("Chilkat.JsonObject")
nParsed := oReply:Load(oResp:BodyStr)
IF ((nSent != 1) .OR. ((oResp:StatusCode != 202) .AND. (oResp:StatusCode != 207)))
    ? "Send returned HTTP " + Str(oResp:StatusCode)
    IF (oReply:HasMember("error.description") == 1)
        ? oReply:StringOf("error.description")
    ENDIF

    oHttp:destroy()
    oBody:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF (oReply:HasMember("error.code") == 1)
    IF (oReply:IsNullOf("error.code") != 1)
        ? "Submission error " + oReply:StringOf("error.code") + "  " + oReply:StringOf("error.description")
        oHttp:destroy()
        oBody:destroy()
        oResp:destroy()
        oReply:destroy()
        RETURN
    ENDIF

ENDIF

//  Look up each submitted appointment in the reply using its unique recipient.
//  This works even if the result order changes or clientMessageId is absent.
oResults := CreateObject("Chilkat.JsonObject")
i := 0
DO WHILE (i < 2)
    oBody:I := i
    cAppointment := oBody:StringOf("messages[i].clientMessageId")
    cPhone := oBody:StringOf("messages[i].to")
    oResult := CreateObject("Chilkat.JsonObject")
    nFound := oReply:FindRecord2("messages", "to", cPhone, 1, oResult)
    oResults:I := i
    oResults:UpdateString("appointments[i].clientMessageId", cAppointment)
    IF ((nFound != 1) .OR. (oResult:JsonTypeOf("accepted") != 5))
        oResults:UpdateString("appointments[i].outcome", "unknown")
        ? cAppointment + " has no confirmed result."
    ELSE
        IF (oResult:BoolOf("accepted") == 1)
            oResults:UpdateString("appointments[i].outcome", "accepted")
            IF (oResult:JsonTypeOf("apiMessageId") == 1)
                oResults:UpdateString("appointments[i].apiMessageId", oResult:StringOf("apiMessageId"))
            ENDIF

            ? cAppointment + " accepted."
        ELSE
            oResults:UpdateString("appointments[i].outcome", "rejected")
            oResults:UpdateString("appointments[i].reason", oResult:StringOf("error.description"))
            ? cAppointment + " rejected: " + oResult:StringOf("error.description")
        ENDIF

    ENDIF

    i := i + 1
ENDDO
//  This demo writes a snapshot. In an application, update the two appointment records.
nSaved := oResults:WriteFile("appointment-send-results.json")
IF (nSaved != 1)
    ? "Could not save the results. Reconcile the submitted messages before retrying."
    oHttp:destroy()
    oBody:destroy()
    oResp:destroy()
    oReply:destroy()
    oResults:destroy()
    oResult:destroy()
    RETURN
ENDIF

? "Saved appointment-send-results.json"

oHttp:destroy()
oBody:destroy()
oResp:destroy()
oReply:destroy()
oResults:destroy()
oResult:destroy()