AutoIt Requires Chilkat v11.0.0+
AutoIt
Validate and format a phone number with Twilio Lookup
See more Twilio Examples
Use Basic Lookup v2 to check a national-format number and obtain its E.164 and national display formats. Supply the two-letter country code for a national number. The example requests only the basic fields.
Read the boolean valid field even when the HTTP request succeeds. When it is false, display any validation_errors that Twilio supplies. Numbering-plan validation does not establish reachability or control of a phone. To confirm control, send an SMS verification code.
Chilkat AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
; Load an API key for the Twilio account from private application configuration.
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.BasicAuth = True
$oHttp.Login = "YOUR_API_KEY_SID"
$oHttp.Password = "YOUR_API_KEY_SECRET"
; The phone number is one URL path segment. Percent-encode spaces and any leading +.
$oNumber = ObjCreate("Chilkat.StringBuilder")
$oNumber.Append("020 7946 0018")
Local $bEncoded = $oNumber.Encode("url_rfc3986","utf-8")
If ($bEncoded <> True) Then
ConsoleWrite("Could not encode the phone number." & @CRLF)
Exit
EndIf
$oUrl = ObjCreate("Chilkat.StringBuilder")
$oUrl.Append("https://lookups.twilio.com/v2/PhoneNumbers/")
$oUrl.Append($oNumber.GetAsString())
$oUrl.Append("?CountryCode=GB")
$oResp = ObjCreate("Chilkat.HttpResponse")
$oReply = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oHttp.HttpNoBody("GET",$oUrl.GetAsString(),$oResp)
If ($oResp.StatusCode = 0) Then
ConsoleWrite("No HTTP response from Twilio." & @CRLF)
Exit
EndIf
Local $bParsed = $oReply.Load($oResp.BodyStr)
If (($bSuccess <> True) Or ($oResp.StatusCode <> 200)) Then
ConsoleWrite("Twilio returned HTTP " & $oResp.StatusCode & @CRLF)
If (($bParsed = True) And ($oReply.JsonTypeOf("code") = 2)) Then
ConsoleWrite("Twilio error: " & $oReply.IntOf("code") & " " & $oReply.StringOf("message") & @CRLF)
EndIf
Exit
EndIf
If ($bParsed <> True) Then
ConsoleWrite("Expected a JSON response from Twilio." & @CRLF)
Exit
EndIf
If ($oReply.JsonTypeOf("valid") <> 5) Then
ConsoleWrite("The lookup response is missing the validity flag." & @CRLF)
Exit
EndIf
If ($oReply.BoolOf("valid") <> True) Then
ConsoleWrite("The number is invalid for the specified country." & @CRLF)
Local $i = 0
While ($i < $oReply.SizeOfArray("validation_errors"))
$oReply.I = $i
ConsoleWrite($oReply.StringOf("validation_errors[i]") & @CRLF)
$i = $i + 1
Wend
Exit
EndIf
If ($oReply.JsonTypeOf("phone_number") <> 1) Then
ConsoleWrite("The lookup response is missing the E.164 number." & @CRLF)
Exit
EndIf
ConsoleWrite("E.164: " & $oReply.StringOf("phone_number") & @CRLF)
ConsoleWrite("National format: " & $oReply.StringOf("national_format") & @CRLF)