PureBasic Requires Chilkat v11.0.0+
PureBasic
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.
Chilkat PureBasic Downloads
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.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")
firstPhone.s = "FIRST_PHONE_NUMBER"
secondPhone.s = "SECOND_PHONE_NUMBER"
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",firstPhone)
CkJsonObject::ckUpdateString(body,"messages[0].content","Your appointment with Lee is at 10:00.")
CkJsonObject::ckUpdateString(body,"messages[0].clientMessageId","appt-1042")
CkJsonObject::ckUpdateString(body,"messages[1].channel","sms")
CkJsonObject::ckUpdateString(body,"messages[1].to",secondPhone)
CkJsonObject::ckUpdateString(body,"messages[1].content","Your appointment with Sam is at 14:30.")
CkJsonObject::ckUpdateString(body,"messages[1].clientMessageId","appt-1043")
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)
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) AND (CkHttpResponse::ckStatusCode(resp) <> 207))
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)
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)
CkJsonObject::ckDispose(body)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(reply)
ProcedureReturn
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.
results.i = CkJsonObject::ckCreate()
If results.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
i.i = 0
While (i < 2)
CkJsonObject::setCkI(body, i)
appointment.s = CkJsonObject::ckStringOf(body,"messages[i].clientMessageId")
phone.s = CkJsonObject::ckStringOf(body,"messages[i].to")
result.i = CkJsonObject::ckCreate()
If result.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
found.i = CkJsonObject::ckFindRecord2(reply,"messages","to",phone,1,result)
CkJsonObject::setCkI(results, i)
CkJsonObject::ckUpdateString(results,"appointments[i].clientMessageId",appointment)
If (found <> 1) OR (CkJsonObject::ckJsonTypeOf(result,"accepted") <> 5)
CkJsonObject::ckUpdateString(results,"appointments[i].outcome","unknown")
Debug appointment + " has no confirmed result."
Else
If CkJsonObject::ckBoolOf(result,"accepted") = 1
CkJsonObject::ckUpdateString(results,"appointments[i].outcome","accepted")
If CkJsonObject::ckJsonTypeOf(result,"apiMessageId") = 1
CkJsonObject::ckUpdateString(results,"appointments[i].apiMessageId",CkJsonObject::ckStringOf(result,"apiMessageId"))
EndIf
Debug appointment + " accepted."
Else
CkJsonObject::ckUpdateString(results,"appointments[i].outcome","rejected")
CkJsonObject::ckUpdateString(results,"appointments[i].reason",CkJsonObject::ckStringOf(result,"error.description"))
Debug appointment + " rejected: " + CkJsonObject::ckStringOf(result,"error.description")
EndIf
EndIf
i = i + 1
Wend
; This demo writes a snapshot. In an application, update the two appointment records.
saved.i = CkJsonObject::ckWriteFile(results,"appointment-send-results.json")
If saved <> 1
Debug "Could not save the results. Reconcile the submitted messages before retrying."
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(body)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(reply)
CkJsonObject::ckDispose(results)
CkJsonObject::ckDispose(result)
ProcedureReturn
EndIf
Debug "Saved appointment-send-results.json"
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(body)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(reply)
CkJsonObject::ckDispose(results)
CkJsonObject::ckDispose(result)
ProcedureReturn
EndProcedure