How to Make a Reverse Auction

This sample demonstrates how to make a reverse auction, where the auction winner is the participant who placed the lowest unique bid.

Files used in this sample are located in the folder:

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

Upon receiving an incoming message, the script ProcessBid.vbs processes the bid sent by the user, updates the database and sends back appropriate replies. When you close the auction, you can use the script ShowWinner.vbs to display the phone number of the user who placed the lowest unique bid.

How to run this sample

  1. Create a new ODBC Data Source and set its name to SMS Reverse 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 Reverse Auction Database. A new table will be created in the database.
  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: 1
    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%"
  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: ReverseAuctionPipe
  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 253 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 affected users about their bid status by sending back appropriate replies.

Source code

The contents of the ProcessBid.vbs file:

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

if WScript.Arguments.Count = 4 then
    
    ' Collect arguments
    PipeName = WScript.Arguments(0)                                                    ' Pipe name
    SenderPhone = WScript.Arguments(1)                                                 ' Sender phone
    MsgID = WScript.Arguments(2)                                                       ' Message ID
    ItemBidStr = WScript.Arguments(3)                                                  ' Bid string  

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

        ReplyText = ""
        
        ReplyMsg2ID = 0
        ReplyMsg2Text = ""
        
        ReplyMsg3ID = 0
        ReplyMsg3Text = ""
           
        ItemBidAmount = -1
        if IsNumeric(ItemBidStr) then
            ItemBidAmount = CLng(ItemBidStr)
        end if
    
        if ItemBidAmount <=0 then
            ReplyText = "You have sent an invalid bid."
        else
    
            strDSN = "Reverse SMS Auction Database" 
            strUser = ""
            strPass = ""
    
            set database = CreateObject("ADODB.Connection")
            database.Open strDSN, strUser, strPass                                     ' Open the database
        
            set R0 = database.Execute("SELECT * FROM Bids WHERE BidAmount = " & ItemBidAmount & " AND UserPhone='" & SenderPhone & "'")
            if not R0.EOF then                                                         ' This bid already exists
                ReplyText = "Your bid already exists."
            else   
               
                LowestUniqueBid = -1
                set R1 = database.Execute("SELECT BidAmount, MsgID FROM Bids WHERE BidUnique = 1 ORDER BY BidAmount ASC")
                if not R1.EOF then   
                    LowestUniqueBid = R1("BidAmount")                                  ' Currently lowest unique bid
                end if      
                    
                set R2 = database.Execute("SELECT MsgID, BidUnique FROM Bids WHERE BidAmount = " & ItemBidAmount)
                if R2.EOF then
                
                    if LowestUniqueBid = -1 OR ItemBidAmount < LowestUniqueBid then    ' This is the first bid or this bid amount is less than the lowest unique bid
                        ReplyText = "Your bid of " & ItemBidAmount & " units is unique and the lowest."
                        if LowestUniqueBid <> -1 then
                            ReplyMsg2ID = R1("MsgID")
                            ReplyMsg2Text = "Your bid is not the lowest anymore."                                         
                        end if                          
                    else
                        ReplyText = "Your bid of " & ItemBidAmount & " units is unique but is not the lowest."                                        
                    end if
                    database.Execute("INSERT INTO Bids (UserPhone, MsgID, BidAmount, BidTime, BidUnique) VALUES('" & SenderPhone & "','" & MsgID & "','" & ItemBidAmount & "','" & Now() & "', 1)")
    
                else                                                                   ' This bid is not unique
    
                    ReplyText = "Your bid of "&ItemBidAmount&" units is not unique."
                    database.Execute("INSERT INTO Bids (UserPhone, MsgID, BidAmount, BidTime, BidUnique) VALUES('" & SenderPhone & "','" & MsgID & "','" & ItemBidAmount & "','" & Now() & "', 0)")
                    if R2("BidUnique") = 1 then                                        ' This bid was unique
                        ReplyMsg2ID = R2("MsgID")
                        ReplyMsg2Text = "Your bid is not unique anymore."
                        database.Execute("UPDATE Bids SET BidUnique = 0 WHERE BidAmount = " & ItemBidAmount)                  
                        set R3 = database.Execute("SELECT * FROM Bids WHERE BidUnique = 1 ORDER BY BidAmount ASC")
                        if not R3.EOF then                                             ' There is a new lowest unique bid
                            NewLowestUniqueBid = R3("BidAmount")
                            if LowestUniqueBid <> NewLowestUniqueBid then              ' Avoid sending this message if nothing was changed
                                ReplyMsg3ID = R3("MsgID")
                                ReplyMsg3Text = "Your bid of " & NewLowestUniqueBid & " units is now unique and the lowest."
                            end if
                        end if
                    end if
                
                end if
                
            end if
            database.Close                                                             ' Close the database
            
        end if
                                  
        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
        
        if ReplyMsg2ID <> 0 then
            pipe.WriteLine("<Clear>")
            pipe.WriteLine("ReplyToID:" & ReplyMsg2ID)
            pipe.WriteLine("Text:" & ReplyMsg2Text)
            pipe.WriteLine("<Send>")                                                   ' Send the message
        end if
        
        if ReplyMsg3ID <> 0 then
            pipe.WriteLine("<Clear>")
            pipe.WriteLine("ReplyToID:" & ReplyMsg3ID)
            pipe.WriteLine("Text:" & ReplyMsg3Text)
            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 Reverse Auction Sample"
        WScript.Quit(1)                                                                ' Error

    end if

else
    
    MsgBox "Error: Wrong number of arguments.", vbCritical, "SMS Studio Reverse 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