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:
if WScript.Arguments.Count = 5 then
Command = WScript.Arguments(0)
PipeName = WScript.Arguments(1)
MsgID = WScript.Arguments(2)
SenderPhone = WScript.Arguments(3)
EventName = Replace(WScript.Arguments(4),"'","''")
if PipeName <> "" then
strDSN = "SMS Event Notification Database"
strUser = ""
strPass = ""
set database = CreateObject("ADODB.Connection")
database.Open strDSN, strUser, strPass
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
set fso = CreateObject("Scripting.FileSystemObject")
set pipe = fso.CreateTextFile("\\.\pipe\" & PipeName)
pipe.WriteLine("ReplyToID:" & MsgID)
pipe.WriteLine("Text:" & ReplyText)
pipe.WriteLine("<Send>")
pipe.Close
WScript.Quit(0)
else
MsgBox "Error: Pipe name is missing.", vbCritical, "SMS Studio Event Notification Sample"
WScript.Quit(1)
end if
else
MsgBox "Error: Wrong number of arguments.", vbCritical, "SMS Studio Event Notification Sample"
WScript.Quit(1)
end if
The contents of the OnTimer.vbs file:
if WScript.Arguments.Count = 1 then
PipeName = WScript.Arguments(0)
if PipeName <> "" then ' Pipe name must be specified in the Info Setup dialog
set fso = CreateObject("Scripting.FileSystemObject")
set pipe = fso.CreateTextFile("\\.\pipe\" & PipeName)
strDSN = "SMS Event Notification Database"
strUser = ""
strPass = ""
set database = CreateObject("ADODB.Connection")
database.Open strDSN, strUser, strPass
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>")
R2.MoveNext
loop
R1.MoveNext
loop
database.Execute("UPDATE Events SET EventTriggered = False WHERE EventTriggered = True")
database.Close
pipe.Close
WScript.Quit(0)
else
MsgBox "Error: Pipe name is missing.", vbCritical, "SMS Studio Event Notification Sample"
WScript.Quit(1)
end if
else
MsgBox "Error: Wrong number of arguments.", vbCritical, "SMS Studio Event Notification Sample"
WScript.Quit(1)
end if
How to run this sample
- 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.
- 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.
- Click the New button on the main toolbar and select the Info service option in the New Service dialog.
- Click the Setup button on the main toolbar to display the Info Setup dialog.
- In the General tab of the Info Setup dialog use the Add button to display the Add Rule dialog.
- 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
- 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%"
- Click the Add + button to enter this rule and to reuse the Add Rule dialog to add the second rule.
- 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
- 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%"
- Click the Add button to close the Add Rule dialog.
- In the Advanced tab of the Info Setup dialog enter the name of the pipe:
Pipe name: EventNotificationPipe
- 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%
- Click the OK button to close the Info Setup dialog.
- Click the Start button on the main toolbar to start the Info service.
- 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.
- 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