'
' Example script for 2BrightSparks SyncBackPro V8 that sends an SMS
' if a profile fails (if the run is simulated then it does not).
'
' IMPORTANT, READ THE FOLLOWING:
'
' - You must first create an account at http://www.clickatell.com/
' - You will receive an API ID. You must set SMSAppID below to that ID.
' - Set the connection details below
'
' SBLang=Basic
'
' http://www.2BrightSparks.com/
'

' The connection details for Clickatell to send an SMS
const SMSUsername = "xxx"
const SMSPassword = "xxx"
const SMSMobileNum = "xxx"    ' Recommended you include the country code
const SMSAppID = "xxx"        ' See the comments above on how to get this

'-----------------------------------------------------------------------------
'-DO NOT MODIFY ANYTHING BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING !-
'-----------------------------------------------------------------------------

'
' This is a runtime script 
'
Function Description(ByRef ScriptType)
  Description = "Send SMS on profile failure"
  ScriptType = SCRIPTTYPE_RUN
End Function

'
' This routined is called after the profile has finished.
'
Sub RunProfileResult(ProfileResult, ErrMsg)
  Dim sUrl
  Dim sText
  Dim oXMLHTTP, sPostData, sResult
  Dim SendSMS

  ' Check to see if the profile has run
  If SBRunning.Simulated then
    Exit Sub
  End If

  ' Failed?
  SendSMS = True
  Select Case ProfileResult
  Case 0
    ' 0 = No result (ELR_None)
    SendSMS = False
  Case 1
    ' 1 = Unknown profile (ELR_UnknownProfile)
    SendSMS = False
  Case 2
    ' 2 = Profile is already running (ELR_AlreadyRunning)
    SendSMS = False
  Case 3
    ' 3 = Profile has been imported and not run yet (ELR_Imported)
    SendSMS = False
  Case 4
    ' 4 = Profile is running (ELR_Running)
    SendSMS = False
  Case 11
    ' 11 = Restore was a success (ELR_RestSuccess)
    SendSMS = False
  Case 14
    ' 14 = Run was a success (ELR_Success)
    SendSMS = False
  End Select

  ' Are we going to send an SMS?
  If SendSMS = False then
    Exit Sub
  End If

  ' Send SMS
  sText = "Profile " & SBRunning.Name & " has failed (code " & ProfileResult & ")."
  if ErrMsg <> "" then
    sText = sText & " Error message: " & ErrMsg
  End If
  sUrl = "http://api.clickatell.com/http/sendmsg"
  sPostData = "api_id=" & SMSAppID
  sPostData = sPostData & "&user=" & SMSUsername
  sPostData = sPostData & "&password=" & SMSPassword
  sPostData = sPostData & "&to=" & SMSMobileNum
  sPostData = sPostData & "&text=" & sText
  Set oXMLHTTP = CreateObject("Microsoft.XMLHTTP")
  oXMLHTTP.Open("POST", sUrl, false)
  oXMLHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
  oXMLHTTP.Send(sPostData)
  sResult = oXMLHTTP.responseText
  SBRunning.DebugOut("SMSResult", sResult, 1)
  Set oXMLHTTP = nothing
End Sub
