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.
程序
-
Create a bot:
-
On the left panel, click Automation.
A list of available bots and forms is displayed.
- Click Create New > Bot.
- Provide a name for the bot: MultiplyPi
- Enter the folder location: Bots\TaskBotExample
- Click Create and Edit.
-
On the left panel, click Automation.
-
Create a variable: nPromptAnswer:
- Type: Number
- Name: nPromptAnswer
-
Create a variable:
lNumbersForPi:
- Type: List
- Subtype: Number
- Name: lNumbersForPi
- Click Save.
-
Use the Prompt > For value
action to ask the user for a number.
- Click the Show link on the Actions pane.
- Double-click or drag the Prompt For value action to add it to the bot.
- In the Prompt window caption field, type: Any Number
- In the Prompt message field, type: Enter any number to multiply by pi.
- In the Assign the value to a variable field, select prompt-assignment.
- Click Save.
-
Use the Error handler > Try
action to gracefully end the bot if there was an error, such as
the user not entering a number.
- Double-click or drag the Error handler > Try action.
- Drag the Prompt > For value action under the Error handler > Try action.
- Click Save.
-
Convert the prompt-assignment (string) to a number
value.
- Double-click or drag the String > To number action, adding it as the last line before the Error handler: Catch action.
- In the Enter the string field, press F2 to open the list of your variables, and select prompt-assignment.
- Click Yes, insert.
- In the Assign the output to variable field, select nPromptAnswer.
- Click Save.
-
Copy the number value to the first item in the list of numbers to pass to
VBScript.
- Double-click or drag the List > Add item action, adding it as the last line before the Error handler: Catch action.
- In the List variable field, select lNumbersForPi.
- In the Item to be added field, select nPromptAnswer.
- Click Save.
-
Use the VBScript > Open
action to provide your Visual Basic source code.
- Double-click or drag the VBScript Open action, adding it as the last line before the Error handler: Catch action.
- In the VBScript options, select Manual Input.
-
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
- Click Save.
-
Use the VBScript > Run function
action to execute the script.
- Double-click or drag the VBScript Run function action, adding it as the last line before the Error handler: Catch action.
- In the Enter the name of the function to be executed (optional) type MultiplyPi.
- In the Parameters (optional) field, select lNumbersForPi variable.
- In the Assign the output to variable (optional) field, select prompt-assignment.
- Click Save.
-
Use the VBScript > Close
action to end the session.
- Double-click or drag the VBScript Close action, adding it as the last line before the Error handler: Catch action.
- Click Save.
-
Print the answer generated in the VBScript on the screen.
- Double-click or drag the Message box action, adding it as the last line before the Error handler: Catch action.
- 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 prompt-assignment.
- Click Yes, insert.
- Click Save.
-
Notify the user if an error occurred and end the bot gracefully.
- Click the Error handler: Catch AllErrors action once so that it is highlighted in your bot.
- Double-click or drag the Message box action to insert it as the last bot action.
- In the Enter the message to display field, type Bot failed. Please be sure to enter only numbers..
- Click Save.
-
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 $prompt-assignment$ String: To number Convert string $prompt-assignment$ 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: $prompt-assignment$" Error handler: Catch AllErrors Message box "Bot failed. Please be sure to enter only numbers." End
- Run your bot.
- When the bot runs successfully, a message box displays the calculated number. View the message and click Close. You can test the resiliency of your bot by running the bot again and entering a letter instead of a number.