2

I'm trying to send an SMS using Twilio. I found an article (https://www.twilio.com/blog/2010/04/using-twilio-with-classic-asp-and-vbscript.html) that shows how to do this by VBScript, but I was wondering how to amend the script when sending a POST request from a client computer and not a server.

I tried to give this a first pass, but I'm not too familiar with VBScript. I found some articles saying to use the System.Web.HttpUtility to run URLEncode()

Any help would be much appreciated.

accountSid = "XXXX"
authToken = "XXXX"

baseUrl = "https://api.twilio.com"
smsUrl = baseUrl & "/2010-04-01/Accounts/" & accountSid & "/SMS/Messages"
' setup the request and authorization
Set http = CreateObject("MSXML2.XMLHTTP.6.0")
http.open "POST", smsUrl, False, accountSid, authToken
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

' message parameters
from = "XXX" ' the number to send the message from
recipient = "XXX" ' the number to send the message to
body = "Sending SMS is easy with Twilio!" ' message contents


Set sendbot=CreateObject(System.Web.HttpUtility)
postData = "From=" & sendbot.URLEncode(from)
postData = postData & "&To=" & sendbot.URLEncode(recipient)
postData = postData & "&Body=" &  sendbot.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
user692942
  • 16,398
  • 7
  • 76
  • 175
DLee
  • 7,856
  • 4
  • 19
  • 23

1 Answers1

2

As VBScript doesn't have a native function, you will have to either create your own or find another workaround (like a COM library that will do it for you).

In the past, I've used this code, which has worked well (originally taken from this gist, which was inspired by a VBA solution on StackOverflow).

' Encode special characters of a string
' this is useful when you want to put a string in the URL
' inspired by https://stackoverflow.com/questions/218181/how-can-i-url-encode-a-string-in-excel-vba
Public Function URLEncode( StringVal )
  Dim i, CharCode, Char, Space
  Dim StringLen
  StringLen = Len(StringVal)
  ReDim result(StringLen)

  Space = "+"
  'Space = "%20"

  For i = 1 To StringLen
    Char = Mid(StringVal, i, 1)
    CharCode = AscW(Char)
    If 97 <= CharCode And CharCode <= 122 _
    Or 64 <= CharCode And CharCode <= 90 _
    Or 48 <= CharCode And CharCode <= 57 _
    Or 45 = CharCode _
    Or 46 = CharCode _
    Or 95 = CharCode _
    Or 126 = CharCode Then
      result(i) = Char
    ElseIf 32 = CharCode Then
      result(i) = Space
    Else
      result(i) = "&#" & CharCode & ";"
    End If
  Next
  URLEncode = Join(result, "")
End Function

Useful Links

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175