Example of using the VBScript package in a resilient bot

Build a bot that executes a Microsoft Visual Basic script. Use the Try/Catch package to gracefully end the bot if it fails, making it resilient to crashes. A resilient bot allows scheduled and queued bots to continue even if the current bot encounters an error.

In this bot, you ask the user to provide a number. The bot passes the number to a Microsoft Visual Basic script to multiply by pi and return the value. The bot then reports the result to the user. You can use the VBScript action to either import an external script or type it directly into your bot. In this example, you manually type the script. You also add resiliency to the bot by adding error handling.

Procedure

  1. Create a bot:
    1. On the left panel, click Automation.
      A list of available bots and forms is displayed.
    2. Click Create New > Bot.
    3. Provide a name for the bot: MultiplyPi
    4. Enter the folder location: Bots\TaskBotExample
    5. Click Create and Edit.
  2. Create a variable: nPromptAnswer:
    • Type: Number
    • Name: nPromptAnswer
  3. Create a variable: lNumbersForPi:
    • Type: List
    • Subtype: Number
    • Name: lNumbersForPi
  4. Click Save.
  5. Use the Prompt > For value action to ask the user for a number.
    1. Click the Show link on the Actions pane.
    2. Double-click or drag the Prompt For value action to add it to the bot.
    3. In the Prompt window caption field, type: Any Number
    4. In the Prompt message field, type: Enter any number to multiply by pi.
    5. In the Assign the value to a variable field, select SampleString.
  6. Click Save.
  7. Use the Error handler > Try action to gracefully end the bot if there was an error, such as the user not entering a number.
    1. Double-click or drag the Error handler > Try action.
    2. Drag the Prompt > For value action under the Error handler > Try action.
  8. Click Save.
  9. Convert the SampleString (string) to a number value.
    1. Double-click or drag the String > To number action, adding it as the last line before the Error handler: Catch action.
    2. In the Enter the string field, press F2 to open the list of your variables, and select SampleString.
    3. Click Yes, insert.
    4. In the Assign the output to variable field, select nPromptAnswer.
  10. Click Save.
  11. Copy the number value to the first item in the list of numbers to pass to VBScript.
    1. Double-click or drag the List > Add item action, adding it as the last line before the Error handler: Catch action.
    2. In the List variable field, select lNumbersForPi.
    3. In the Item to be added field, select nPromptAnswer.
  12. Click Save.
  13. Use the VBScript > Open action to provide your Visual Basic source code.
    1. Double-click or drag the VBScript Open action, adding it as the last line before the Error handler: Catch action.
    2. In the VBScript options, select Manual Input.
    3. Copy the following code and paste it into the Enter script here field:
      Dim Arg, var1, var2
      Set Arg = WScript.Arguments
      Function MultiplyPi(Arg)
        var1 = Arg(0)
        var2 = 3.14159
        MultiplyPi = cdbl(var1)*var2
      End Function
  14. Click Save.
  15. Use the VBScript > Run function action to execute the script.
    1. Double-click or drag the VBScript Run function action, adding it as the last line before the Error handler: Catch action.
    2. In the Enter the name of the function to be executed (optional) type MultiplyPi.
    3. In the Parameters (optional) field, select lNumbersForPi variable.
    4. In the Assign the output to variable (optional) field, select SampleString.
  16. Click Save.
  17. Use the VBScript > Close action to end the session.
    1. Double-click or drag the VBScript Close action, adding it as the last line before the Error handler: Catch action.
  18. Click Save.
  19. Print the answer generated in the VBScript on the screen.
    1. Double-click or drag the Message box action, adding it as the last line before the Error handler: Catch action.
    2. In the Enter the message to display field, type Pi times your number is: , then press F2 to open the list of your variables. Select SampleString.
    3. Click Yes, insert.
  20. Click Save.
  21. Notify the user if an error occurred and end the bot gracefully.
    1. Click the Error handler: Catch AllErrors action once so that it is highlighted in your bot.
    2. Double-click or drag the Message box action to insert it as the last bot action.
    3. In the Enter the message to display field, type Bot failed. Please be sure to enter only numbers..
  22. Click Save.
  23. Click the List tab to view your code as text only.
    Your code should look like this:
    Start
    Error handler: Try
        Prompt: For value during bot execution and assign the value $SampleString$
        String: To number Convert string $SampleString$ to a number and assign it to number variable $nPromptAnswer$
        List: Add item $nPromptAnswer$ to $lNumbersForPi$
        VBScript: Open VBScript manual script of 7 lines
        VBScript: Run function "MultiplyPi"
        VBScript: Close VBScript "Default"
        Message box "Pi times your number is: $SampleString$"
    Error handler: Catch AllErrors
        Message box "Bot failed.  Please be sure to enter only numbers."
    End
  24. Run your bot.
  25. When the bot runs successfully, the prompt window opens where you enter the value, and then the message box displays the calculated number. View the message and click Close.
    Entering the value in the Prompt window
    Calculated values displayed in the message box
    You can test the resiliency of your bot by running the bot again and entering a letter instead of a number.