VBScript Requires Chilkat v11.0.0+
VBScript
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 VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
' Assumes Chilkat is unlocked. See Global Unlock Sample.
' Load an API key for the Twilio account from private application configuration.
set http = CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.BasicAuth = 1
http.Login = "YOUR_API_KEY_SID"
http.Password = "YOUR_API_KEY_SECRET"
' The phone number is one URL path segment. Percent-encode spaces and any leading +.
set number = CreateObject("Chilkat.StringBuilder")
success = number.Append("020 7946 0018")
encoded = number.Encode("url_rfc3986","utf-8")
If (encoded <> 1) Then
outFile.WriteLine("Could not encode the phone number.")
WScript.Quit
End If
set url = CreateObject("Chilkat.StringBuilder")
success = url.Append("https://lookups.twilio.com/v2/PhoneNumbers/")
success = url.Append(number.GetAsString())
success = url.Append("?CountryCode=GB")
set resp = CreateObject("Chilkat.HttpResponse")
set reply = CreateObject("Chilkat.JsonObject")
success = http.HttpNoBody("GET",url.GetAsString(),resp)
If (resp.StatusCode = 0) Then
outFile.WriteLine("No HTTP response from Twilio.")
WScript.Quit
End If
parsed = reply.Load(resp.BodyStr)
If ((success <> 1) Or (resp.StatusCode <> 200)) Then
outFile.WriteLine("Twilio returned HTTP " & resp.StatusCode)
If ((parsed = 1) And (reply.JsonTypeOf("code") = 2)) Then
outFile.WriteLine("Twilio error: " & reply.IntOf("code") & " " & reply.StringOf("message"))
End If
WScript.Quit
End If
If (parsed <> 1) Then
outFile.WriteLine("Expected a JSON response from Twilio.")
WScript.Quit
End If
If (reply.JsonTypeOf("valid") <> 5) Then
outFile.WriteLine("The lookup response is missing the validity flag.")
WScript.Quit
End If
If (reply.BoolOf("valid") <> 1) Then
outFile.WriteLine("The number is invalid for the specified country.")
i = 0
Do While (i < reply.SizeOfArray("validation_errors"))
reply.I = i
outFile.WriteLine(reply.StringOf("validation_errors[i]"))
i = i + 1
Loop
WScript.Quit
End If
If (reply.JsonTypeOf("phone_number") <> 1) Then
outFile.WriteLine("The lookup response is missing the E.164 number.")
WScript.Quit
End If
outFile.WriteLine("E.164: " & reply.StringOf("phone_number"))
outFile.WriteLine("National format: " & reply.StringOf("national_format"))
outFile.Close