Using Twilio with Classic ASP and VBScript

April 14, 2010
Written by
John Sheehan
Contributor
Opinions expressed by Twilio contributors are their own

Twilio Bug Logo

Asp-logo
We’ve recently had an influx of requests for examples showing how to use Twilio with Classic ASP and VBScript. Here are two short examples to demonstrate how to initiate call and send a text message. You can download both files here.

Placing a Call via the REST API with Classic ASP

Let’s start off with an example for placing a call via the REST API. Initiating a call requires making a simple POST request. Let’s take a look at the code to make that happen.

<%
' replace with your account's settings
' available in the Dashboard
accountSid = "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
authToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

' setup the URL
baseUrl = "https://api.twilio.com"
callUrl = baseUrl & "/2008-08-01/Accounts/" & accountSid & "/Calls"

' setup the request and authorization
Set http = Server.CreateObject("MSXML2.ServerXMLHTTP")
http.open "POST", callUrl, False, accountSid, authToken
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

' call parameters
caller = "your-number-here" ' the number to call from
called = "recipient-number-here" ' the number to dial
url = "call-handler-url-here" ' URL handler for the call, should return TwiML

postData = "Caller=" & Server.URLEncode(caller)
postData = postData & "&Called=" & Server.URLEncode(called)
postData = postData & "&Url=" & Server.URLEncode(url) 

' send the POST data
http.send postData

' optionally write out the response if you need to check if it worked
' Response.Write http.responseText

' clean up
Set http = Nothing
%>

Sending an SMS text message via the REST API

The Coding is Fun YouTube channel wrote in with an update to use our 2010 API endpoint. The following code is untested by Twilio, but should work with our newer API:

<%
Sub SendSMS()

'credit to Coding is Fun
'https://www.youtube.com/channel/UCZjRcM1ukeciMZ7_fvzsezQ


'Authentication
ACCOUNTSID = ActiveSheet.Range("C4").Value
AUTHTOKEN = ActiveSheet.Range("C5").Value

'Variables
fromNumber = "%2B" & ActiveSheet.Range("C6").Value   'use as variable or something else to call number DO NOT ADD leading +
toNumber = ActiveSheet.Range("C8").Value 'use as variable or something else to call number DO NOT ADD leading +
BodyText = ActiveSheet.Range("C9").Value

'Use XML HTTP
Set Request = CreateObject("MSXML2.ServerXMLHTTP.6.0")

'Specify URL
Url = "https://api.twilio.com/2010-04-01/Accounts/" & ACCOUNTSID & "/Messages.json"

'Post URL with authentication
Request.Open "POST", Url, False, ACCOUNTSID, AUTHTOKEN

'Request Header
Request.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

'Concatenate PostData
postData = "From=" & fromNumber & "&To=%2B" & toNumber & "&Body=" & BodyText

'Send the Post Data
Request.send postData

'[OPTIONAL] Get response text (result)
MsgBox Request.responseText

End Sub
Options
%>

Following this notice is the original function from the article.

Sending an SMS is straightforward as well:

<%
' replace with your account's settings
' available in the Dashboard
accountSid = "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
authToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
' setup the URL
baseUrl = "https://api.twilio.com"
smsUrl = baseUrl & "/2008-08-01/Accounts/" & accountSid & "/SMS/Messages"
' setup the request and authorization
Set http = Server.CreateObject("MSXML2.ServerXMLHTTP")
http.open "POST", smsUrl, False, accountSid, authToken
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
' message parameters
from = "your-number-here" ' the number to send the message from
recipient = "recipient-number-here" ' the number to send the message to
body = "Sending SMS is easy with Twilio!" ' message contents
postData = "From=" & Server.URLEncode(from)
postData = postData & "&To=" & Server.URLEncode(recipient)
postData = postData & "&Body=" & Server.URLEncode(body) 
' send the POST data
http.send postData
' optionally write out the response if you need to check if it worked
' Response.Write http.responseText
' clean up
Set http = Nothing
%>

There you have it! If you have any questions about these examples, head on over to our forums or send us an email at help@twilio.com and we’ll be glad to help!