Clickatell One API: Send an SMS and get its message ID
See more Clickatell Examples
Send one text using an SMS-enabled One API integration. Supply an international phone number starting with the country code, using digits only. For a US two-way integration, set from to its provisioned two-way number. With a test integration, use a registered test phone.
HTTP 202 means the submission was processed. Inspect accepted for each message. Save the returned apiMessageId with the application record and use it to look up delivery status. When a multi-message submission returns HTTP 207, check each result before retrying any rejected message.
Already using the SMS-only REST integration? Its documented request is POST /messages with the integration key in Authorization and a body such as {"content":"Test message","to":["PHONE_NUMBER"]}. One API has a different request body and a separate integration key. SMS API reference
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_Body
string ls_Sender
oleobject loo_Resp
oleobject loo_Reply
long li_Parsed
long li_Count
long i
long li_ResultNumber
string ls_MessageId
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
destroy loo_Http
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Http.RequireSslCertVerify = 1
// Supply the key for this One API integration from application configuration.
loo_Http.SetRequestHeader("Authorization","ONE_API_KEY")
loo_Http.SetRequestHeader("Accept","application/json")
loo_Body = create oleobject
li_rc = loo_Body.ConnectToNewObject("Chilkat.JsonObject")
loo_Body.UpdateString("messages[0].channel","sms")
loo_Body.UpdateString("messages[0].to","PHONE_NUMBER")
loo_Body.UpdateString("messages[0].content","Test message: your order is ready.")
// For a two-way sender, supply the number provisioned on the integration.
ls_Sender = ""
if ls_Sender <> "" then
loo_Body.UpdateString("messages[0].from",ls_Sender)
end if
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
li_Success = loo_Http.HttpJson("POST","https://platform.clickatell.com/v1/message",loo_Body,"application/json",loo_Resp)
if loo_Resp.StatusCode = 0 then
Write-Debug "The send outcome is unknown. Check the message report before resending."
destroy loo_Http
destroy loo_Body
destroy loo_Resp
return
end if
loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")
li_Parsed = loo_Reply.Load(loo_Resp.BodyStr)
if (li_Success <> 1) OR ((loo_Resp.StatusCode <> 202) AND (loo_Resp.StatusCode <> 207)) then
Write-Debug "Clickatell returned HTTP " + string(loo_Resp.StatusCode)
if li_Parsed = 1 then
if loo_Reply.HasMember("error.description") = 1 then
Write-Debug loo_Reply.StringOf("error.description")
end if
end if
destroy loo_Http
destroy loo_Body
destroy loo_Resp
destroy loo_Reply
return
end if
if li_Parsed <> 1 then
Write-Debug "Expected a JSON response."
destroy loo_Http
destroy loo_Body
destroy loo_Resp
destroy loo_Reply
return
end if
// A request-level error applies to the submission as a whole.
if loo_Reply.HasMember("error.code") = 1 then
if loo_Reply.IsNullOf("error.code") <> 1 then
Write-Debug "Submission error " + loo_Reply.StringOf("error.code") + " " + loo_Reply.StringOf("error.description")
destroy loo_Http
destroy loo_Body
destroy loo_Resp
destroy loo_Reply
return
end if
end if
li_Count = loo_Reply.SizeOfArray("messages")
if li_Count <= 0 then
Write-Debug "No recipient results returned. Check the message report before resending."
destroy loo_Http
destroy loo_Body
destroy loo_Resp
destroy loo_Reply
return
end if
i = 0
do while (i < li_Count)
loo_Reply.I = i
li_ResultNumber = i + 1
// JsonTypeOf returns 5 for a JSON boolean.
if loo_Reply.JsonTypeOf("messages[i].accepted") <> 5 then
Write-Debug "Result " + string(li_ResultNumber) + " has no acceptance flag."
else
if loo_Reply.BoolOf("messages[i].accepted") = 1 then
ls_MessageId = loo_Reply.StringOf("messages[i].apiMessageId")
if (loo_Reply.JsonTypeOf("messages[i].apiMessageId") <> 1) OR (ls_MessageId = "") then
Write-Debug "Result " + string(li_ResultNumber) + " was accepted without a tracking ID."
else
Write-Debug "Result " + string(li_ResultNumber) + " accepted. Message ID: " + ls_MessageId
end if
else
Write-Debug "Result " + string(li_ResultNumber) + " rejected: " + loo_Reply.StringOf("messages[i].error.code") + " " + loo_Reply.StringOf("messages[i].error.description")
end if
end if
i = i + 1
loop
destroy loo_Http
destroy loo_Body
destroy loo_Resp
destroy loo_Reply