Anyway, I needed to connect to Twilio in order to send messages, of course, so I came up with this script.
EventHandlers.vbs:
Code: Select all
Private Const TwilioSID = "yourTwilioSID"
Private Const TwilioToken = "yourTwilioToken"
Private Const TwilioSMSFrom = "+12125551212" ' Your Twilio SMS from number
Function oLookup(strRegEx, strMatch, bGlobal)
If strRegEx = "" Then strRegEx = StrReverse(strMatch)
With CreateObject("VBScript.RegExp")
.Pattern = strRegEx
.Global = bGlobal
.MultiLine = True
.IgnoreCase = True
Set oLookup = .Execute(strMatch)
End With
End Function
Function jsonDecode(jsonString)
Dim oSCtrl
Set oSCtrl = CreateObject("ScriptControl") ' Required: https://tablacus.github.io/scriptcontrol_en.html
oSCtrl.Language = "JScript"
Set jsonDecode = oSCtrl.Eval("(" & jsonString & ")")
End Function
Function RemoveHTML(strHTML)
' .Pattern = "(<style[\s\S]*?style>)|(\/\*[\s\S]*?\*\/)|(<[\s\S]*?>)"
With CreateObject("VBScript.RegExp")
.Pattern = "<[^>]+>| |<|>|"|&|\s{2,}|(v|o|w)\\\:\*|\.shape|\{behavior:url\(\#default\#VML\)\;\}"
.Global = True
.MultiLine = True
.IgnoreCase = True
RemoveHTML = .Replace(strHTML, " ")
End With
End Function
Function SendSMS(SMSNumber, sMSG)
Dim rc, WshShell, strRegEx, Match, Matches, sMSGLen, Url, Request, postData, oResponse
If SMSNumber = Empty Then SMSNumber = "3475559876" ' Change this to YOUR mobile number
sMSG = Replace(sMSG, "<", "(")
sMSG = Replace(sMSG, ">", ")")
sMSG = RemoveHTML(sMSG)
sMSG = Replace(sMSG, Chr(34), "\" & Chr(34))
REM - replace line breaks
strRegEx = "(\r\n|\r|\n)"
Set Matches = oLookup(strRegEx, sMSG, False)
For Each Match In Matches
sMSG = Replace(sMSG, Match.Value, " ")
Next
REM - delete google calendar note
strRegEx = "(-::~.+~::-)"
Set Matches = oLookup(strRegEx, sMSG, False)
For Each Match In Matches
sMSG = Replace(sMSG, Match.Value, " ")
Next
REM - replace double/multiple spaces
strRegEx = "([\s]{2,})"
Set Matches = oLookup(strRegEx, sMSG, False)
For Each Match In Matches
sMSG = Replace(sMSG, Match.Value, " ")
Next
sMSGLen = len(sMSG)
Set Request = CreateObject("MSXML2.ServerXMLHTTP.6.0")
Url = "https://api.twilio.com/2010-04-01/Accounts/" & TwilioSID & "/Messages.json"
Request.Open "POST", Url, False, TwilioSID, TwilioToken
Request.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
postData = "From=" & TwilioSMSFrom & "&To=%2B1" & SMSNumber & "&Body=" & sMSG
Request.send postData
Set oResponse = jsonDecode(Request.responseText)
If (oResponse.status <> "queued" ) Then
EventLog.Write( "================== SMS ==================" )
EventLog.Write( ":Status : " & Request.status )
EventLog.Write( ":Error Code : " & oResponse.code )
EventLog.Write( ":Message : " & oResponse.message )
EventLog.Write( ":More Info : " & oResponse.more_info )
EventLog.Write( "=========================================" )
Else
EventLog.Write( "================== SMS ==================" )
EventLog.Write( ":Status : " & Request.status )
EventLog.Write( ":Recipient Number : " & oResponse.to )
EventLog.Write( ":Message : " & oResponse.body )
EventLog.Write( ":Segments : " & oResponse.num_segments )
EventLog.Write( "=========================================" )
End If
End Function
Sub OnAcceptMessage(oClient, oMessage)
Dim sMSG, SMSNumber
' Will send SMS message to default recipient
sMSG = "Notification: " & oMessage.FromAddress & " sent a message to " & oMessage.Recipients(0).OriginalAddress
Call SendSMS(SMSNumber, sMSG)
' Will send SMS message to 6465554321
SMSNumber = "6465554321"
sMSG = "Notification: " & oMessage.FromAddress & " sent a message to " & oMessage.Recipients(0).OriginalAddress
Call SendSMS(SMSNumber, sMSG)
End Sub
One thing to note: If you're not sending primarily to mobile numbers in North America, you'll need to delete the "1" in line 71 above.
Change:
Code: Select all
postData = "From=" & TwilioSMSFrom & "&To=%2B1" & SMSNumber & "&Body=" & sMSG
Code: Select all
postData = "From=" & TwilioSMSFrom & "&To=%2B" & SMSNumber & "&Body=" & sMSG