AutoIt Requires Chilkat v11.0.0+
AutoIt
Clickatell One API: Upload and send a WhatsApp PDF
See more Clickatell Examples
Send a local invoice.pdf during the customer’s open WhatsApp service window. Upload the binary PDF for one recipient, then reference the returned fileId in a separate message to the same recipient. The upload and message submission have separate acceptance results.
Chilkat AutoIt Downloads
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
; Load the key for this One API integration from your application configuration.
$oHttp.SetRequestHeader "Authorization","ONE_API_KEY"
$oHttp.SetRequestHeader "Accept","application/json"
Local $sRecipient = "PHONE_NUMBER"
$oPdf = ObjCreate("Chilkat.BinData")
Local $bLoaded = $oPdf.LoadFile("invoice.pdf")
If ($bLoaded <> True) Then
ConsoleWrite("Could not read invoice.pdf." & @CRLF)
Exit
EndIf
; Restrict this customer's invoice to that customer's number.
$oHttp.SetUrlVar("recipient",$sRecipient)
$oUpload = ObjCreate("Chilkat.HttpResponse")
Local $bUploaded = $oHttp.HttpBd("POST","https://platform.clickatell.com/v1/media?fileName=invoice.pdf&to={$recipient}",$oPdf,"application/pdf",$oUpload)
If (($bUploaded <> True) Or ($oUpload.StatusCode <> 201)) Then
ConsoleWrite("PDF upload failed. HTTP status " & $oUpload.StatusCode & @CRLF)
Exit
EndIf
$oMedia = ObjCreate("Chilkat.JsonObject")
Local $bDecoded = $oMedia.Load($oUpload.BodyStr)
If (($bDecoded <> True) Or ($oMedia.BoolOf("accepted") <> True)) Then
ConsoleWrite("Upload was not accepted: " & $oMedia.StringOf("error.description") & @CRLF)
Exit
EndIf
Local $sFileId = $oMedia.StringOf("fileId")
If (($oMedia.JsonTypeOf("fileId") <> 1) Or ($sFileId = "")) Then
ConsoleWrite("Upload returned no file ID." & @CRLF)
Exit
EndIf
$oBody = ObjCreate("Chilkat.JsonObject")
$oBody.UpdateString("messages[0].channel","whatsapp")
$oBody.UpdateString("messages[0].to",$sRecipient)
$oBody.UpdateString("messages[0].media.fileId",$sFileId)
$oBody.UpdateString("messages[0].media.contentType","application/pdf")
$oBody.UpdateString("messages[0].media.fileName","invoice.pdf")
$oResp = ObjCreate("Chilkat.HttpResponse")
Local $bSent = $oHttp.HttpJson("POST","https://platform.clickatell.com/v1/message",$oBody,"application/json",$oResp)
If ($oResp.StatusCode = 0) Then
ConsoleWrite("No HTTP response. Check the message report before resending." & @CRLF)
Exit
EndIf
$oReply = ObjCreate("Chilkat.JsonObject")
Local $bParsed = $oReply.Load($oResp.BodyStr)
If (($bSent <> True) Or ($oResp.StatusCode <> 202)) Then
ConsoleWrite("Send returned HTTP " & $oResp.StatusCode & @CRLF)
If ($oReply.HasMember("error.description") = True) Then
ConsoleWrite($oReply.StringOf("error.description") & @CRLF)
EndIf
Exit
EndIf
If ($oReply.HasMember("error.code") = True) Then
If ($oReply.IsNullOf("error.code") <> True) Then
ConsoleWrite("Submission error " & $oReply.StringOf("error.code") & " " & $oReply.StringOf("error.description") & @CRLF)
Exit
EndIf
EndIf
; HTTP acceptance and the recipient result are separate checks.
If (($bParsed <> True) Or ($oReply.JsonTypeOf("messages[0].accepted") <> 5)) Then
ConsoleWrite("Missing recipient result. Check the message report." & @CRLF)
Exit
EndIf
If ($oReply.BoolOf("messages[0].accepted") <> True) Then
ConsoleWrite("Message rejected: " & $oReply.StringOf("messages[0].error.code") & " " & $oReply.StringOf("messages[0].error.description") & @CRLF)
Exit
EndIf
If ($oReply.JsonTypeOf("messages[0].apiMessageId") <> 1) Then
ConsoleWrite("Accepted without a message ID. Check the message report." & @CRLF)
Exit
EndIf
ConsoleWrite("Accepted. Message ID: " & $oReply.StringOf("messages[0].apiMessageId") & @CRLF)