Created
May 8, 2018 17:03
-
-
Save walkthelot/81c91ed797a289e0b996f6702bdc1666 to your computer and use it in GitHub Desktop.
VB Script for sending Push Notifications with Titanium
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' VB Script for sending Push Notifications with Titanium | |
Const fsoForReading = 1 | |
Const fsoForWriting = 2 | |
Const errorsTo = "[email protected]" | |
Const errorsFrom = "[email protected]" | |
Const myChannel = "MyAppsChannelName" | |
' Get the login and password from your TiApp.xml and or from within the Titanium Platform Dashboard (Select App, Manage Data, Users. Make sure they are an role=appc-Admin) | |
Const myLogin = "appc_whatever" | |
Const myPassword = "XQAz7VnlSlVgGBl6Rr" | |
' Get from Platform Tools, App, Configuration, App Key (be sure to note the dropdown whether Development or Production) | |
Const appKey = "LEzbz28c9MMxo81uu0Dqu8KtfuH1EkDc" | |
' Where to store the text files for getString and setString. Generally the same location as the VBScript | |
' It needed to be set, otherwise the files would get saved to C:\Windows\System32 or C:\Windows\SysWow64. | |
BasePath = "D:\BatchFiles\MyScript\" | |
Sub SendEmail(EmailFrom, EmailTo, EmailContent, EmailSubject, MailHTML) | |
Err.Clear | |
WScript.Echo "" | |
WScript.Echo "SendEmail: " & EmailTo & ", " & EmailFrom & ", " & EmailSubject | |
Set ObjSendMail = CreateObject("CDO.Message") | |
Set iConf = CreateObject("CDO.Configuration") | |
Set Flds = iConf.Fields | |
Flds("http://schemas.microsoft.com/cdo/configuration/sendusing") = 1 | |
Flds("http://schemas.microsoft.com/cdo/configuration/smtpserverpickupdirectory") = "C:\Inetpub\mailroot\Pickup" | |
Flds.Update | |
Set ObjSendMail.Configuration = iConf | |
ObjSendMail.To = EmailTo | |
ObjSendMail.Subject = EmailSubject | |
ObjSendMail.From = EmailFrom | |
ObjSendMail.TextBody = EmailContent | |
ObjSendMail.Send | |
Set ObjSendMail = Nothing | |
If Err.Number <> 0 Then MsgBox "SendEmail Error: " & Err.Description | |
End Sub | |
Function CheckError(inErrorNumber, inErrDescription, inHalt) | |
If Err.Number <> 0 Then | |
WScript.Echo "ERROR: " & inErrDescription | |
If inHalt = True Then | |
SendEmail errorsFrom, errorsTo, inErrorNumber & ", " & inErrDescription, "MyScript.vbs HALT Error", False | |
MsgBox "Halt Error: " & inErrorNumber & " : " & inErrDescription | |
CheckError = True | |
WScript.Quit | |
Else | |
SendEmail errorsFrom, errorsTo, inErrorNumber & ", " & inErrDescription, "MyScript.vbs Error", False | |
CheckError = False | |
End If | |
WScript.Sleep 10000 | |
Err.Clear | |
Else | |
Err.Clear | |
CheckError = False | |
End If | |
End Function | |
Function sendNotification(strAlert, strTitle) | |
Set XMLHTTP = CreateObject("MSXML2.ServerXMLHTTP") | |
CheckError Err.Number, "sendNotifications: Create HTTP Object " & Err.Description, True | |
XMLHTTP.open "POST", "https://api.cloud.appcelerator.com/v1/users/login.json?key=" & appKey, False | |
CheckError Err.Number, "sendNotifications: Create HTTP Open (1) " & Err.Description, True | |
XMLHTTP.setRequestHeader "Accept","application/json" | |
XMLHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded" | |
strBody = "login=" & myLogin & "&password=" & myPassword | |
XMLHTTP.send strBody | |
CheckError Err.Number, "sendNotifications: HTTP Send (1) " & Err.Description, True | |
While XMLHTTP.readyState <> 4 | |
XMLHTTP.waitForResponse 1000 | |
Wend | |
strHeaders = XMLHTTP.getAllResponseHeaders() | |
WScript.Echo "strHeaders: " & strHeaders | |
WScript.Echo "XMLHTTP.responsetext: " & XMLHTTP.responseText | |
' You need to download and install on the server the free Chilkat component in order to parse JSON with VB | |
Set jsonObject = CreateObject("Chilkat_9_5_0.JsonObject") | |
CheckError Err.Number, "sendNotifications: Created Chilkat JsonObject (1) " & Err.Description, True | |
success = jsonObject.Load(XMLHTTP.responseText) | |
If (success <> 1) Then | |
WScript.Echo jsonObject.LastErrorText | |
CheckError Err.Number, "sendNotifications: jsonObject.Load(XMLHTTP.responseText) (1) " & Err.Description, True | |
End If | |
Set jsonObj = jsonObject.ObjectAt(0) | |
session_id = jsonObj.StringOf("session_id") | |
WScript.Echo "session_id=" & session_id | |
Set jsonObj = Nothing | |
Set jsonObject = Nothing | |
hArr = split(strHeaders,"Set-Cookie: ") | |
For kk = 1 to ubound(hArr) | |
theCookie = Left(hArr(kk),instr(hArr(kk),"path=/")-2) | |
myCookie = myCookie & " " & theCookie | |
Next | |
' Get and store the login cookie for step two, otherwise you are not logged in and can not send the notification. | |
XMLHTTP.open "POST", "https://api.cloud.appcelerator.com/v1/push_notification/notify.json?key=LEzbz28c9MMxo81uu0Dqu8KtfuH1EkDc", False | |
CheckError Err.Number, "sendNotifications: Create HTTP Open (2) " & Err.Description, True | |
XMLHTTP.setRequestHeader "Accept","application/json" | |
XMLHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded" | |
XMLHTTP.setRequestHeader "Cookie", "excuse the Microsoft bug" | |
XMLHTTP.setRequestHeader "Cookie", myCookie | |
WScript.Echo "strAlert=" & strAlert | |
WScript.Echo "strTitle=" & strTitle | |
strPayload = "{" &_ | |
"""alert"": """ & strAlert & """," &_ | |
"""icon"": ""q""," &_ | |
"""badge"": ""+1""," &_ | |
"""sound"": ""default""," &_ | |
"""vibrate"": ""true""," &_ | |
"""title"": """ & strTitle & """," &_ | |
"""priority"": ""high""," &_ | |
"""content-available"": 1" &_ | |
"}" | |
WScript.Echo "Payload=" & strPayload | |
strBody = "channel=" & myChannel & "&payload=" & strPayload & "&to_ids=everyone" | |
CheckError Err.Number, "sendNotifications: HTTP Send (2) " & Err.Description, True | |
WScript.Echo strBody | |
XMLHTTP.send strBody | |
While XMLHTTP.readyState <> 4 | |
XMLHTTP.waitForResponse 1000 | |
Wend | |
strHeaders = XMLHTTP.getAllResponseHeaders() | |
WScript.Echo "strHeaders: " & strHeaders | |
WScript.Echo "XMLHTTP.responsetext: " & XMLHTTP.responseText | |
Set XMLHTTP = nothing | |
End Function | |
Function getString(filename, default) | |
Dim fso, f | |
Set fso = CreateObject("Scripting.FileSystemObject") | |
If fso.FileExists(BasePath & filename & ".txt") Then | |
Set objFile = fso.GetFile(BasePath & filename & ".txt") | |
If objFile.Size > 0 Then | |
Set f = fso.OpenTextFile(BasePath & filename & ".txt", fsoForReading) | |
getString = f.ReadAll | |
f.Close | |
Else | |
getString = "" | |
End If | |
Set objFile = Nothing | |
Else | |
Set f = fso.CreateTextFile(BasePath & filename & ".txt", True) | |
f.Write default | |
f.Close | |
getString = default | |
End If | |
CheckError Err.Number, "getString " & Err.Description, True | |
End Function | |
Sub setString(filename, text) | |
Dim fso, f | |
Set fso = CreateObject("Scripting.FileSystemObject") | |
Set f = fso.CreateTextFile(BasePath & "QAlerts-" & filename & ".txt", True) | |
f.Write text | |
f.Close | |
CheckError Err.Number, "setString " & Err.Description, True | |
End Sub | |
theLoop = 0 | |
Do While (theLoop < 1) | |
WScript.Echo "==================================================" | |
WScript.Echo "Query Start: " & Now() | |
WScript.Echo "==================================================" | |
' Query a database recordset, json query, whatever. In this example we would | |
' grab an ID from the source (such as record ID) and compare it to the ID | |
' from the push that was last sent (stored to a file via the setString function). | |
Id = 1000 ' This Id would actually be obtained from a DB or JSON source. | |
lastNotifiedId = getString(lastNotifiedId, 0) | |
If CLng(Id) > CLng(lastNotifiedId) Then | |
WScript.Echo "NOT YET NOTIFIED... SENDING PUSH" | |
sendNotification "My alert content.", "My Title" | |
setString "lastNotifiedId", Id | |
Else | |
WScript.Echo "ALREADY NOTIFIED" | |
End If | |
WScript.Echo "==================================================" | |
WScript.Echo "Query End: " & Now() | |
WScript.Echo "==================================================" | |
WScript.Echo "Complete. Waiting 2.5 MINUTES (180000 milliseconds) to repeat." | |
WScript.Sleep 180000 | |
Loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment