How to Return a Random Reply from an External Database
This sample demonstrates how to return a random reply from an external database.
Files
used in this sample are located in the folder:
C:\Program Files\CodeSegment\SMS Studio\Samples\InfoService\Jokes
Upon receiving an incoming message, the script
GetRandomJoke.vbs selects a random joke from the jokes database and sends it back to the user. You can use the AddJoke.vbs script to enter a new joke into the
database.
The contents of the GetRandomJoke.vbs file:
if WScript.Arguments.Count = 2 then
PipeName = WScript.Arguments(0)
MsgID = WScript.Arguments(1)
if PipeName <> "" then
strDSN = "SMS Jokes Database"
strUser = ""
strPass = ""
set database = CreateObject("ADODB.Connection")
database.Open strDSN, strUser, strPass
JokeText = "Sorry, no jokes today."
set R = database.Execute("SELECT MAX(ID) AS MaxID FROM Jokes")
if not R.EOF then
MaxID = R("MaxID")
Randomize
Success = false
do
JokeID = Int(1+Rnd*MaxID)
set R = database.Execute("SELECT JokeText FROM Jokes WHERE ID = " & JokeID)
if not R.EOF then
JokeText = R("JokeText")
Success = true
end if
loop while not Success
end if
database.Close
set fso = CreateObject("Scripting.FileSystemObject")
set pipe = fso.CreateTextFile("\\.\pipe\" & PipeName)
pipe.WriteLine("ReplyToID:" & MsgID)
pipe.WriteLine("Body:" & JokeText)
pipe.WriteLine("<Send>")
pipe.Close
WScript.Quit(0)
else
MsgBox "Error: Pipe name is missing.", vbCritical, "SMS Studio Jokes Sample"
WScript.Quit(1)
end if
else
MsgBox "Error: Wrong number of arguments.", vbCritical, "SMS Studio Jokes Sample"
WScript.Quit(1)
end if
How to run this sample
- Create a new ODBC Data Source and set its name to SMS Jokes 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
Jokes Database. A new table 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 click the Add button to display the Add Rule dialog.
- In the General tab of the Add Rule dialog enter the following parameters:
Name: Random Joke
Pattern: Joke
Action: Execute external application
- In the Execute tab of the Add Rule dialog enter the following parameters:
External application: GetRandomJoke.vbs - use the selection button ( ... ) to locate and select the external application.
Application parameters:
"%PipeName%" "%MsgID%"
- 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: JokesPipe
- 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
Text field with the text Joke and click the Receive
button. The Info
service will receive this message and run the GetRandomJoke.vbs
application which will select a random joke and send it back as a reply which will be
placed in the Outbox.
Related topics
Named Pipe