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, START for subscribing and STOP 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.
How to run this sample
Source code
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%" "%MsgFrom%" "%MsgAccountID%" "%MsgTo%" "%P1%"
' where the command can be either ADD or DEL
'-------------------------------------------------------------------------------------------------------------------------------
if WScript.Arguments.Count = 7 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
AccountID = WScript.Arguments(4) ' Account ID
Address = WScript.Arguments(5) ' Address
EventName = Replace(WScript.Arguments(6),"'","''") ' 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 UserPhone = '" & SenderPhone & "' AND EventName = '" & EventName & "'")
if R.EOF then
database.Execute("INSERT INTO Subscriptions (EventName, UserPhone, AccountID, Address) VALUES('" & EventName & "','" & SenderPhone & "','" & AccountID & "','" & Address & "')")
end if
ReplyText = "You have been subscribed for the event: " & EventName & "."
end if
if Command = "DEL" then
database.Execute("DELETE FROM Subscriptions WHERE UserPhone = '" & 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-2009 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 UserPhone, AccountID, Address FROM Subscriptions WHERE EventName = '" & R1("EventName") & "'")
do while not R2.EOF
pipe.WriteLine("To:" & R2("UserPhone"))
pipe.WriteLine("AccountID:" & R2("AccountID"))
pipe.WriteLine("From:" & R2("Address"))
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-2009 CodeSegment. All rights reserved. http://www.codesegment.com/
'-------------------------------------------------------------------------------------------------------------------------------
| Copyright © 2002-2010 CodeSegment. All rights reserved. |