How to Make an Event Notification Service

This sample demonstrates how to make an SMS service which notifies users about events to which they have previously subscribed.

Files used in this sample are located in the folder:

    C:\Program Files\CodeSegment\SMS Studio\Samples\InfoService\EventNotification

This sample uses a database which keeps the record of events and users. Users can send SMS to subscribe and unsubscribe for receiving notifications about certain events. Info service will recognize two prefixes, SUB for subscribing and UNSUB for unsubscribing, and run the Subscribe.vbs script which will update the subscription table in the database.

Info service will also run the OnTimer.vbs script at regular time intervals to check the state of the events. When an event is triggered, all users subscribed for that event will receive a notification message.

The contents of the Subscribe.vbs file:

'-------------------------------------------------------------------------------------------------------------------------------
' This script demonstrates how to subscribe and unsubscribe a user from a notification event.
' WARNING: Error checking omitted for clarity.
'
' Use the following application parameters in the Info Rule dialog:
' COMMAND "%PipeName%" "%MsgID%" "%MsgFromPhone%" "%P1%"
' where the command can be either ADD or DEL
'-------------------------------------------------------------------------------------------------------------------------------

if WScript.Arguments.Count = 5 then
   
    ' Collect arguments
    Command = WScript.Arguments(0)                              ' Command
    PipeName = WScript.Arguments(1)                             ' Pipe name
    MsgID = WScript.Arguments(2)                                ' Message ID
    SenderPhone = WScript.Arguments(3)                          ' Sender phone
    EventName = Replace(WScript.Arguments(4),"'","''")          ' A quote character may render an SQL query invalid
   
    if PipeName <> "" then                                      ' Pipe name must be specified in the Info Setup dialog
       
        strDSN = "SMS Event Notification Database"
        strUser = ""
        strPass = ""
       
        set database = CreateObject("ADODB.Connection")
        database.Open strDSN, strUser, strPass                  ' Open the database
       
        set R = database.Execute("SELECT EventName FROM Events WHERE EventName = '" & EventName & "'")
        if R.EOF then
            ReplyText = "You have sent an invalid event name."
        else
            EventName = R("EventName")
           
            if Command = "ADD" then
                set R = database.Execute("SELECT * FROM Subscriptions WHERE Phone = '" & SenderPhone & "' AND EventName = '" & EventName & "'")
                if R.EOF then
                    database.Execute("INSERT INTO Subscriptions (EventName, Phone) VALUES('" & EventName & "','" & SenderPhone & "')")
                end if
                ReplyText = "You have been subscribed for the event: " & EventName & "."
            end if
           
            if Command = "DEL" then
                database.Execute("DELETE FROM Subscriptions WHERE Phone = '" & SenderPhone & "' AND EventName = '" & EventName & "'")
                ReplyText = "You have been unsubscribed for the event: " & EventName & "."
            end if
           
        end if
       
        database.Close                                          ' Close the database
       
        set fso = CreateObject("Scripting.FileSystemObject")
        set pipe = fso.CreateTextFile("\\.\pipe\" & PipeName)   ' Open the pipe instance
       
        pipe.WriteLine("ReplyToID:" & MsgID)
        pipe.WriteLine("Text:" & ReplyText)
        pipe.WriteLine("<Send>")                                ' Send the message
       
        pipe.Close                                              ' Close the pipe instance
       
        WScript.Quit(0)                                         ' Success
       
    else
       
        MsgBox "Error: Pipe name is missing.", vbCritical, "SMS Studio Event Notification Sample"
        WScript.Quit(1)                                         ' Error
       
    end if
   
else
   
    MsgBox "Error: Wrong number of arguments.", vbCritical, "SMS Studio Event Notification Sample"
    WScript.Quit(1)                                             ' Error
   
end if

'-------------------------------------------------------------------------------------------------------------------------------
' Copyright (c) 2002-2006 CodeSegment. All rights reserved.                                          http://www.codesegment.com/
'-------------------------------------------------------------------------------------------------------------------------------

The contents of the OnTimer.vbs file:

'-------------------------------------------------------------------------------------------------------------------------------
' This script demonstrates the on timer routine which checks a database
' for the triggered events and sends messages to the subscribed users.
' WARNING: Error checking omitted for clarity.
'
' Use the following application parameters in the Info Setup dialog - External tab:
' "%PipeName%"
'-------------------------------------------------------------------------------------------------------------------------------

if WScript.Arguments.Count = 1 then
   
    ' Collect arguments
    PipeName = WScript.Arguments(0)                             ' Pipe name
   
    if PipeName <> "" then ' Pipe name must be specified in the Info Setup dialog
       
        set fso = CreateObject("Scripting.FileSystemObject")
        set pipe = fso.CreateTextFile("\\.\pipe\" & PipeName)   ' Open the pipe instance
       
        strDSN = "SMS Event Notification Database"
        strUser = ""
        strPass = ""
       
        set database = CreateObject("ADODB.Connection")
        database.Open strDSN, strUser, strPass                  ' Open the database
       
        set R1 = database.Execute("SELECT EventName, EventMessage FROM Events WHERE EventTriggered = True")
        do while not R1.EOF
            pipe.WriteLine("Text:" & R1("EventMessage"))
            set R2 = database.Execute("SELECT Phone FROM Subscriptions WHERE EventName = '" & R1("EventName") & "'")
            do while not R2.EOF
                pipe.WriteLine("To:" & R2("Phone"))
                pipe.WriteLine("<Send>")                        ' Send the message
                R2.MoveNext
            loop
            R1.MoveNext
        loop
        database.Execute("UPDATE Events SET EventTriggered = False WHERE EventTriggered = True")
       
        database.Close                                          ' Close the database
        pipe.Close                                              ' Close the pipe instance
       
        WScript.Quit(0)                                         ' Success
       
    else
       
        MsgBox "Error: Pipe name is missing.", vbCritical, "SMS Studio Event Notification Sample"
        WScript.Quit(1)                                         ' Error
       
    end if
   
else
   
    MsgBox "Error: Wrong number of arguments.", vbCritical, "SMS Studio Event Notification Sample"
    WScript.Quit(1)                                             ' Error
   
end if

'-------------------------------------------------------------------------------------------------------------------------------
' Copyright (c) 2002-2006 CodeSegment. All rights reserved.                                          http://www.codesegment.com/
'-------------------------------------------------------------------------------------------------------------------------------

How to run this sample

  1. Create a new ODBC Data Source and set its name to SMS Event Notification Database. See How to Create an ODBC Data Source for detailed instructions.
  2. Open Windows Explorer and locate the CreateTables.vbs file. Double-click on this file to run the script. When asked for the name of the Data Source enter SMS Event Notification Database. New tables will be created in the database filled with some sample data.
  3. Click the New button on the main toolbar and select the Info service option in the New Service dialog.
  4. Click the Setup button on the main toolbar to display the Info Setup dialog.
  5. In the General tab of the Info Setup dialog use the Add button to display the Add Rule dialog.
  6. In the General tab of the Add Rule dialog enter the following parameters:
    Name: Subscribe
    Pattern: SUB *
    Required number of parameters: 1
    Action: Execute external application
  7. In the Execute tab of the Add Rule dialog enter the following parameters:
    External application: Subscribe.vbs - use the selection button ( ... ) to locate and select the external application.
    Application parameters: ADD "%PipeName%" "%MsgID%" "%MsgFromPhone%" "%P1%"
  8. Click the Add + button to enter this rule and to reuse the Add Rule dialog to add the second rule.
  9. In the General tab of the Add Rule dialog enter the following parameters:
    Name: Unsubscribe
    Pattern: UNSUB *
    Required number of parameters: 1
    Action: Execute external application
  10. In the Execute tab of the Add Rule dialog enter the following parameters:
    External application: Subscribe.vbs - use the selection button ( ... ) to locate and select the external application.
    Application parameters: DEL "%PipeName%" "%MsgID%" "%MsgFromPhone%" "%P1%"
  11. Click the Add button to close the Add Rule dialog.
  12. In the Advanced tab of the Info Setup dialog enter the name of the pipe:
    Pipe name: EventNotificationPipe
  13. In the External tab enter the following parameters:
    Periodically execute external application: Checked
    Every: 10 sec
    Application: OnTimer.vbs - use the selection button ( ... ) to locate and select the external application.
    Application parameters: %PipeName%
  14. Click the OK button to close the Info Setup dialog.
  15. Click the Start button on the main toolbar to start the Info service.
  16. Use Tools / Receive Message option in the main menu to display the Receive Message dialog. Fill in the Phone and Text fields (e.g. SUB E1 or UNSUB E2) and click the Receive button. The Info service will receive this message and run the Subscribe.vbs application which will subscribe or unsubscribe a user for the specified event.
  17. You can use the AddEvent.vbs and TriggerEvent.vbs scripts from Windows Explorer to add a new event to the database and to trigger an existing event.

Related topics

Named Pipe

 

Copyright © 2002-2007 CodeSegment. All rights reserved.

   www.codesegment.com