How to Make an Auction

This sample demonstrates how to make an auction.

Files used in this sample are located in the folder:

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

Upon receiving an incoming message, the script ProcessBid.vbs processes the bid sent by the user, updates the database accordingly and sends back appropriate replies. When you close the auction, the highest bid for each item in the auction will be registered in the database.

How to run this sample

  1. Create a new ODBC Data Source and set its name to SMS Auction 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 Auction Database. A new table 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 click the Add button to display the Add Rule dialog.
  6. In the General tab of the Add Rule dialog enter the following parameters:
    Name: Bid
    Pattern: Bid *
    Required number of parameters: 2
    Action: Execute external application
  7. In the Execute tab of the Add Rule dialog enter the following parameters:
    External application: ProcessBid.vbs - use the selection button ( ... ) to locate and select the external application.
    Application parameters: "%PipeName%" "%MsgFrom%" "%MsgID%" "%P1%" "%P2%"
  8. Click the Add button to close the Add Rule dialog.
  9. In the Advanced tab of the Info Setup dialog enter the name of the pipe:
    Pipe name: AuctionPipe
  10. Click the OK button to close the Info Setup dialog.
  11. Click the Start button on the main toolbar to start the Info service.
  12. Use Tools / Receive Message option in the main menu to display the Receive Message dialog. Fill in the Text field with the text Bid MP9 250 and click the Receive button. The Info service will receive this message and run the ProcessBid.vbs application which will update the database with the new bid and inform the user about its bid status by sending back a reply which will be placed in the Outbox.

Source code

The contents of the ProcessBid.vbs file:

'-------------------------------------------------------------------------------------------------------------------------------
' This script demonstrates how to process an auction bid.
' WARNING: Error checking omitted for clarity.
'
' Use the following application parameters in the Info Rule dialog:
' "%PipeName%" "%MsgFrom%" "%MsgID%" "%P1%" "%P2%"
'-------------------------------------------------------------------------------------------------------------------------------

if WScript.Arguments.Count = 5 then
    
    ' Collect arguments
    PipeName = WScript.Arguments(0)                             ' Pipe name
    UserPhone = WScript.Arguments(1)                            ' Sender phone
    MsgID = WScript.Arguments(2)                                ' Message ID
    ItemID = Replace(WScript.Arguments(3),"'","''")             ' A quote character may render an SQL query invalid
    BidStr = WScript.Arguments(4)                               ' Bid string

    if PipeName <> "" then                                      ' Pipe name must be specified in the Info Setup dialog

        strDSN = "SMS Auction Database" 
        strUser = ""
        strPass = ""
        
        set database = CreateObject("ADODB.Connection")
        database.Open strDSN, strUser, strPass                  ' Open the database

        set R = database.Execute("SELECT * FROM Items WHERE ItemID = '" & ItemID & "'")
        if R.EOF then 
            ReplyText = "You have sent an invalid item id."
        else 
            ItemID = R("ItemID")
            ItemName = R("ItemName")
            Price = R("Price")
            MaxRaise = R("MaxRaise")

            if IsNumeric(BidStr) then
                Bid = CInt(BidStr)
                if Bid > Price and Bid <= Price + MaxRaise then
                    ReplyText = "Your bid has been accepted!"
                    Price = Bid
                    database.Execute("UPDATE Items SET Price = '" & Price & "', UserPhone = '" & UserPhone & "', MsgID = '" & MsgID & "', BidTime = '" & Now() & "' WHERE ItemID = '" & ItemID & "'")
                    OldUserPhone = R("UserPhone")
                    OldMsgID = R("MsgID")
                else
                    ReplyText = "Your bid has not been accepted."
                end if
            else
                ReplyText = "You have sent an ivalid bid."
            end if
            
            PriceText = " The current price for '" & ItemName & "' is " & Price & "."
        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 & PriceText)
        pipe.WriteLine("<Send>")                                ' Send the message
        
        if OldUserPhone <> "" then
            pipe.WriteLine("ReplyToID:" & OldMsgID)
            pipe.WriteLine("Text:" & "We are sorry to inform you that your bid is not the highest anymore." & PriceText)
            pipe.WriteLine("<Send>")                            ' Send the message
        end if

        pipe.Close                                              ' Close the pipe instance
        
        WScript.Quit(0)                                         ' Success

    else

        MsgBox "Error: Pipe name is missing.", vbCritical, "SMS Studio Auction Sample"
        WScript.Quit(1)                                         ' Error

    end if
    
else
    
    MsgBox "Error: Wrong number of arguments.", vbCritical, "SMS Studio Auction Sample"
    WScript.Quit(1)                                             ' Error
    
end if

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

Related topics

Info Rules, Named Pipe

 

Copyright © 2002-2010 CodeSegment. All rights reserved.

   www.codesegment.com