Extended Laiye RPA Commands
Many people will create plug-ins for IE/Chrome/Firefox browsers, Eclipse, Visual Studio, Sublime Text and other programming tools. These plug-ins at the application tool level rely on the original platform to run, but they expand and enrich the original tools, functions, and platforms. Based on plug-ins, users can even create personalized browsers and programming tools in a customized manner.
Let's go back to Laiye RPA again. As mentioned earlier, Laiye RPA is essentially a platform tool. This platform has several characteristics. The first characteristic is "powerful". Laiye RPA provides many components for building RPA processes, and a large set of function modules, such as basic keyboard and mouse operations, various interface element operations, common office software and browser automation operations, various data processing, file processing, and network and system operations. The second characteristic of this platform is "simple". Laiye RPA extracts the most versatile, common, basic, and core functions and integrates them into the platform to form a concise and capable core library. If you blindly pile up functions and integrate all functions, big and small, into Laiye RPA's frame, then Laiye RPA's frame will become very bloated, and the difficulty of learning will also increase significantly.
More interestingly, Laiye RPA also supports writing plugins in many different programming languages. Including Python language, Java language, Lua language, C# language and C++ language. You can choose your favorite language within this range, no matter which plugin is written in Laiye RPA, there is almost no difference in use.
Of course, due to the large differences between different programming languages, the method of writing plug-ins for Laiye RPA using different programming languages is also different. This article introduces how to write plug-ins for Laiye RPA using Python, Java, and C#. Considering that C++ language is more difficult to learn, there are few class libraries in Lua language. This article will not introduce the plug-in mechanism of these two languages.
In the following description, the file directory is often involved. Unless otherwise specified, it refers to the relative path relative to the Laiye RPA installation directory. In order to facilitate writing, we use the / symbol as the path separator, rather than the \ symbol used by Windows.
Writing plugins in Python
It is simplest to write a Laiye RPA plug-in in Python. You can just use any text editor to write a file with the extension .py (hereinafter referred to as py file), save it in UTF-8 format, and place it in the extend/python directory. You can directly call the function defined in this py file in the form of plugin function name.
Note that the plug-in name here refers to the part of the file name before the extension .py. For example, if the file name is test.py, the plug-in name is test.
Let's look at a complete example:
- Write plug-in source code. Open the extend/python directory, create a test.py file in this directory, and open the test.py file in the Notepad and write the following content:
def Add (n1, n2):
return n1 + n2
- Save the test.py file as UTF-8 encoding format, as shown below:
- Call the plug-in function. Open Laiye RPA, create a new process, and write the code in the code view:
Traceprint test.add (1, 1)
- Verify whether the plug-in function is correct. Running this process, if the results are as follows, it means that the plug-in call is working.
When writing a plug-in in Python, in addition to calling the functions of Python itself, the plug-in can call some functions of Laiye RPA in turn. These functions are called plugin APIs.
The calling method of the plugin API is as follows:
- Write the following code in the Python plugin:
- Directly call the plugin API, for example:
def CommanderInfo ():
return UiBot.GetCommanderInfo ()
Currently available plugin APIs in Python plugins include:
This function is used to detect whether the current process needs to stop immediately (for example, the user presses the "stop" button). When it needs to stop, it returns True; otherwise, it returns False.
When a plug-in function needs to be executed for a relatively long time, during the execution process, the process cannot be stopped immediately if the user decides to stop the process without executing the plug-in function. Therefore, it is recommended that when writing a plug-in, you take into account that the plug-in function has a longer execution time. It is also recommended to periodically call this plug-in API during the function execution to determine whether the process needs to stop. If you want to stop, you should exit the plug-in function immediately.
This function is used to obtain a certain string of the current language version. The parameter is a string path (explained below), and the return value is the obtained string.
We may use strings in plugins, and some string contents need to be distinguished by language versions. For example, let's say we need to prompt an error message in the plug-in. This error message should include the Chinese version, the English version or other language versions. If the user is using the Chinese version of Laiye RPA, then the Chinese error will be reported; if the user is using the English version of Laiye RPA, the English error will be reported.
How can we do this? We can see the two files lang/en-us/extend.json and lang/zh-cn/extend.json in the installation directory of Laiye RPA (other language versions also have similar paths, which will not be repeated), indicating plug-ins respectively English and Chinese character strings to be used in. You can write the different language versions of the string we want to use to these files, and then use UiBot.GetString() in the plug-in to get the required string.
Of course, there are many plug-ins for Laiye RPA, and there are also many strings in each plug-in. Since many strings are placed in a file, how can we ensure that there is no conflict? It is easy to see that this file is in JSON format, where multiple nested JSON Objects are used to distinguish different strings. When we need to use a string, we only need to fill in the string path in the parameters of UiBot.GetString(). The so-called string path refers to the combination of the Object where the string is located and the keys of the Objects at all levels above, separated by /. For example, UiBot.GetString('App/Prefix') obtains the character string in the JSON Object whose key is "App" in this file, and the key is "Prefix".
When the Laiye RPA Worker is running the process and establishes a connection with the Laiye RPA Commander, you can get some information of the Commander through this API, such as a URL. In addition to the official Laiye RPA, the plug-in for general users will not use Laiye RPA Commander, so it is not necessary to use this API.
Import Module of Plugin
A simple py file often has limited functions. Only through the import statement in the py file to import some other Python modules, its functions are more abundant.
In fact, under the lib/site-packages path of the Laiye RPA installation directory, a lot of Python modules (and Python packages) have been preset. (For the definitions and differences of Python packages and modules, please refer to the relevant descriptions, which are not explained in this article.) These modules can be used directly in the Python plugin. If we need to import other modules in the plugin, one way is to place it under the lib/site-packages path, and another way is to place it under the extend/python/\<plugin name\>.lib path. Note that the \<plugin name\>.lib is also a directory. If we have a Python plugin and the file name is test.py, this directory is test.lib.
When writing a plug-in, we recommend putting the modules imported in the plug-in (assuming that these modules are not preset by Laiye RPA itself) under the path of extend/python/\<plug-in name\>.lib instead of under the path of lib/site-packages. Because lib/site-packages are a public directory, when we delete a plug-in, it is difficult to tell which modules were used by the plug-in and are no longer needed. But if you put these modules in the extend/python/\<plugin name\>.lib path, it will be very clear because when deleting the plugin, you only need to delete the directory with the same name and the extension of .lib. It is useful to guarantee the whole process.
In addition, it is worth noting that some py files will import some modules with the extension pyd. These modules are actually dynamic link libraries in binary format. Please note that the dynamic link library distinguishes between 32-bit version and 64-bit version. If the Laiye RPA you use is a 32-bit version, then these pyd modules should also be 32-bit versions; otherwise, the pyd module should be a 64-bit version. Currently, the community version of Laiye RPA is a 32-bit version.
Hide Source Code
For py files, the source code is completely public. What if we want other people to use the Python plugin we wrote and don't want others to see the source code of the plugin?
We only need to call this plugin at least once in Laiye RPA Creator, and we will see that an extend/python/_ _ pycache_ _ directory has been created. Check this directory and there are files that start with the plug-in and end with things like.cpython-37 and the extension.pyc. For example, if our py file is test.py, a file like this will be created automatically: extend/python/ pycache/test.cpython-37.pyc.
Rename this file to test.pyc, and put it in the extend/python directory, and delete the original test.py (please back up before deleting). We can still use the test plugin in Laiye RPA, and the usage remains unchanged. Because its code has been saved in test.pyc in binary format, we only need to send this file to other people to use, and we can avoid being directly read the source code.
Of course, test.pyc is not actually encrypted, and it is still possible that some source code will be decompiled. If you want a more thorough encryption, you need to cooperate with other means that will not be discussed in this article.
If N parameters are defined in the function of the Python plug-in, then when called in Laiye RPA, you can pass in less than N parameters, and the extra parameters will be automatically filled to None. However, no more than N parameters can be passed in.
You can pass the array or dictionary type in Laiye RPA as a parameter to the Python plug-in, which corresponds to the list or dict type in Python. You can also return the list, tuple, or dict types in Python to Laiye RPA, and the first two are converted to array types and the last one to a dictionary type. Regardless of incoming parameters or return values, these composite types are passed by value between the Python plugin and Laiye RPA, rather than by reference.
An exception can be thrown in the function of the Python plugin. The exception may or may not be caught by the Python plugin itself. If the Python plug-in does not catch the exception, it will be automatically transferred to Laiye RPA, where it will be caught. If Laiye RPA does not catch the exception, then the process will quit with an error, and the error message will indicate that it is caused by an exception in the Python plug-in, which will help troubleshoot the problem.
Laiye RPA has a built-in Python running environment, so there is no need to install additional Python. Even if it is installed, Laiye RPA will not use the installed Python. The current Python built into Laiye RPA is version 3.7.1.
Variables and functions in Python are case sensitive, but when using the Python plugin in Laiye RPA, you can call the functions in a case -insensitive manner. For example, in the previous example, you can write test.add(1,1) in Laiye RPA or Test.ADD(1,1) and they share the same effect.
You can use global variables in Python, which means you can write variables outside the function. The values of global variables are shared by all functions in the plugin, but different plugins do not share global variables.
It is easy to write Laiye RPA plug-ins using Python, but Python itself is an independent programming language. It is inconvenient to develop and debug using a text editor. Therefore, it is recommended to use an integrated development environment, such as Visual Studio Code for Python plug-in development.
Writing plugins in Java
Starting from Laiye RPA version 5.0, writing Laiye RPA plugins in Java language is supported. Readers who have used Java know that Java source code files generally end with a .java extension and need to be compiled into a byte code (Byte Code) file with the extension .class using JDK (Java Development Kit) before they can run. When running, it is not necessary to install the JDK, so just install the JRE (Java Runtime Environment).
Due to copyright restrictions, there is no built-in JDK in Laiye RPA, but there is a built-in version of JRE 1.7 released by Oracle. Therefore, in order to write plug-ins in Java, you need to download and install the Oracle JDK 1.7 version yourself. There are a lot of information on the download and installation process on the internet, so this article will not discuss it.
In order to provide convenience to write the plug-in for Laiye RPA in Java language, we have designed an example of the plug-in and put its source code on GitHub, and you can click here to get it. If you are used to using git, you can also pull from this URL: http://github.com/Laiye-UiBot/extend-example.
Subsequent content will focus on this example.
According to the specifications of the Java language, first we need to design a plug-in name, and then name the source code file \<plug-in name\>.java, and write a Java class in the file. The name of this class must also be the plug-in name. In the example, we can see that the plug-in is named JavaPlugin, so the source code file name must be JavaPlugin.java, and a class named JavaPlugin will be defined in this file:
public class JavaPlugin
In order for Laiye RPA to use this class normally, this class must be public and cannot be included in any package. Classes can define public, private or protected functions, but only public functions can be directly called by Laiye RPA. For example, we defined a function called Add in the example. This function is public, so we can call it in Laiye RPA.
How do we call it? You need to use the javac program in the hands of JDK to compile this source file and enter it on the command line:
javac -encoding utf8 JavaPlugin.java
Of course, the javac program needs to be in the current search path. In addition, the JavaPlugin.java in the example is UTF-8 encoded, and there are Chinese characters in it, so you need to add the option -encoding utf8. If there are no Chinese characters, this option can be omitted.
If the compilation is successful, it will automatically generate a file named JavaPlugin.class. Put this file in the extend/java directory, and then you can use it like a Python plugin. For example, we can open Laiye RPA, create a new process, and write code in the code view:
Traceprint javaPlugin.add(1, 1)
After running this process, if the result is as follows, then the plug-in call is normal.
Similar to the Python plug-in, in the Java plug-in, you can also use the plug-in API, which in turn calls some of the functions of Laiye RPA. If you want to call the plugin API, there is need to import any packages. You only need to copy the Laiye RPA directory in the plugin example to the directory where the Java plugin source code is located when compiling the Java plugin.
The plugin APIs currently available in the Java plugin include:
This is used to detect whether the current process needs to stop immediately (for example, the user presses the stop button). When it needs to stop, it returns True; otherwise it returns False.
For the specific function, please refer to the UiBot.IsStop function used in the Python plugin.
This is used to obtain a string in the current language version. In addition, the parameter is a string path (explained below), and the return value is the obtained string.
For its specific function, please refer to the UiBot.GetString() function used in the Python plugin. In addition, in the plug-in example, we also used this API to obtain the string with the string path of 'Excel/SaveBook', that is, the file named 'SaveBook' in the JSON Object named 'Excel' in the extend.json file string.
The following Laiye RPA source code will first call the GetString() function in the Java plug-in, and then call UiBot.API.GetString() in Laiye RPA in turn. You can enter this code in Laiye RPA and run it to see what results you can get.
When Laiye RPA Worker runs the process and establishes a connection with the Laiye RPA Commander, you can get some information of Commander through this API. Unlike Laiye RPA official, Laiye RPA Commander will not be used in the plug-ins of general users, so it is not necessary to use this API.
Transfer of Variables
Java is a static programming language; that is, variables need to be defined before use, and the type of variable (integer, floating point, string, etc.) must be specified during the definition. At run time, the variable can only be used as this type. Moreover, arrays can usually only contain one type of data.
But this is very different from Laiye RPA. The variables of Laiye RPA are dynamic types. You do not need to specify the type, you can change the type at runtime, and you can put various types of data in arrays.
Therefore, in order to successfully use the Java plug-in in Laiye RPA, the following requirements must be met:
• If the parameters of the Java plug-in are integers, floating-point numbers, strings, or Boolean types, the parameters passed by Laiye RPA must also be of the same type (except for the exceptions described in the following items); otherwise an error will occur
• If the parameter of the Java plug-in is a floating point number, you can pass in an integer without error. But the converse is not true; that is, if the parameter of the Java plug-in is an integer, floating point numbers cannot be passed in
• If the parameter of the Java plug-in is long, you can pass in an integer less than 2^31 without error. But the opposite is not true. In other words, if the parameter of the Java plug-in is an integer (int), you cannot pass in an integer greater than or equal to 2^31
• If you need to transfer the dictionary or array type from Java to the Laiye RPA plug-in, the parameter type in the Java plug-in can only use org.json.JSONArray (corresponding array) or org.json.JSONObject (corresponding dictionary)
• If you need to transfer the dictionary or array type from the Java plug-in to Laiye RPA, the return value type in the Java plug-in can only use org.json.JSONArray or org.json.JSONObject. Laiye RPA will automatically convert the return value of type org.json.JSONArray into an array, and the return value of type org.json.JSONObject into a dictionary
• Regardless of incoming parameters or return values, these composite types are passed by value between the Java plug-in and Laiye RPA, rather than by reference
• Import org.json.*; can be written in the Java source code, so that you can directly use the JSONArray or JSONObject type to avoid the org.json prefix. This is how it is written in the plugin example. In addition, the org.json package has been included in the operating environment by Laiye RPA, without additional downloading and installation.
In the plug-in example, there is a Concat function, which is used to demonstrate how to transfer two arrays from Laiye RPA to the Java plug-in, and how to return the result of connecting the two arrays to Laiye RPA. We recommend reading this section carefully.
Import Module of Plugin
Similar to Python, a simple Java file often has limited functions. It has a large set of functions only through the import statement in the source code, which imports other Java packages.
We have built-in Oracle JRE 1.7 in Laiye RPA. Of course, it also includes the packages that comes with JRE, such as com.sun.javafx. In addition, we also built the org.json package to be used in Laiye RPA. In addition to the above, other third-party Java packages need to be placed in the extend/java directory for normal use. The placement rules are as follows:
• If we need to use a class named C in our plugin, we only need to put the C.class file in the extend/java directory
• If our plugin needs to use a package named B, which contains a class named C, you can put the C.class file in the extend/java/B directory, or you can package the C.class file to generate the name of the B.jar file which is placed in the extend/java directory
• If our plug-in needs to use a package named AB, which contains a class named C and the previous rule's file, you can put the C.class file in the extend/java/A/B directory, or pack the C.class file and the B directory to generate a file named A.jar and place it in the extend/java directory
The above rules actually follow the rules of class path specified by Java. Readers who are familiar with the Java language should find the above rules to be a review.
The functions in the Java plug-in do not support variable parameters or default parameters. When calling, you must pass in the specified number and type of parameters.
An exception can be thrown in the function of the Java plug-in. The exception may or may not be caught by the Java plug-in itself. If the Java plug-in does not catch the exception, it will be automatically transferred to Laiye RPA, where it will be caught. If Laiye RPA does not catch the exception, then the process will quit with an error, and the error message will indicate that it is caused by an exception in the Java plug-in, which will help troubleshoot the problem.
Variables and functions in Java are case sensitive, but when using the Java plug-in in Laiye RPA, you can call the functions in a case-insensitive manner. For example, you can write javaPlugin.add(1,1) in Laiye RPA or JavaPlugin.ADD(1,1), and the effect is exactly the same.
When writing a Java plug-in, a Java class is actually defined, and the public function in the class is called to Laiye RPA. This class can have a constructor or member variables, and its initialization will be completed automatically when the process starts running.
Oracle JRE version 1.7 is built into Laiye RPA. You need to download Oracle JDK version 1.7 to compile a Java plugin. Although the versions of JDK and JRE are sometimes inconsistent, it can still work. However, in order to reduce the trouble, it is recommended to use the same version. In addition, it is also recommended to use an integrated development environment to develop Java plug-ins, such as Eclipse or IntelliJ IDEA.
Write Plugins in C#.Net
Part of the code of Laiye RPA itself is based on Microsoft's .Net framework and written in C# language. Therefore, Laiye RPA plug-ins (hereinafter referred to as .Net plug-ins) can also be written in C# language. In fact, Microsoft's .Net framework supports multiple programming languages, including VB.Net, C++/CLI, etc. These programming languages all follow the specifications of the .Net framework, and they can all be used to write .Net plugins. However, C# is the programming language mainly promoted by Microsoft, so this article uses C# as an example, and experienced readers can also port it to other languages in the .Net framework. In addition, Laiye RPA's support for .Net plug-ins is also constantly being upgraded. This article uses Laiye RPA version 5.0 as an example. If some examples do not work properly on the old version of Laiye RPA, please upgrade Laiye RPA.
In order to facilitate you in writing .Net plug-ins in C# language, we have designed a plug-in template and put its source code on GitHub. Click here to get it. If you are used to using git, you can also pull from this URL:http://github.com/Laiye-UiBot/extend-example.
It is recommended that you write directly on the basis of this template when writing .Net plug-ins without starting from scratch. The follow-up content will also focus on the examples in this template.
Similar to the Java plug-in, the .Net plug-in also needs to be compiled into a file with the extension .dll in order to be used by Laiye RPA. Microsoft's integrated development environment Visual Studio has both writing and compiling functions, and also provides a free community version, which is the one recommended to download and use. The template we provide is based on the Visual Studio 2015 version. You can choose this version or a higher version of Visual Studio, but it is not recommended to use Visual Studio lower than the 2015 version.
After installing Visual Studio and downloading our .Net template, you can double-click the UiBotPlugin.sln file. This is a "solution", whose name is a bit deceiving. But it is actually a collection of multiple related files. After opening this solution with Visual Studio, it contains a lot of content as you can see. The only thing we need to modify is the UiBotPlugin.cs file. Other files, references, properties, etc. can be left untouched. As shown below:
In the UiBotPlugin.cs file, there is a namespace called UiBotPlugin, which contains an interface (interface) and a class (class). To avoid confusion, we recommend changing the name of this namespace to the plug-in name. For example, if the final plug-in file is DotNetPlugin.dll, then the plug-in name is DotNetPlugin, and the name of this namespace should be changed to DotNetPlugin.
It can be seen from the template: three functions are declared in the interface, and the implementation of these three functions is written in the class. These three functions are examples. You can delete their declarations and implementations at any time and add your own plug-in functions. But please pay special attention that when adding functions, you should also maintain a similar writing, which needs to be declared in the interface and implemented in the class. Otherwise, Laiye RPA cannot normally recognize this plug-in function.
We will use the Add function from earlier as an example to compile the plugin, and call this function in Laiye RPA:
Select the "Build" menu item in Visual Studio. After compiling this solution, you will see a directory called Release under the plug-in directory, which generates a file called UiBotPlugin.dll.
Manually rename this file to your plug-in name, and retain the extension of .dll. Let's say we rename it to "DotNetPlugin.dll".
Put this file in the extend/DotNet directory of Laiye RPA.
Open Laiye RPA, create a new process, and write the code in the code view:
Traceprint DotNetPlugin.add(1, 1)
After running this process, if the result is as follows, then the plug-in call is normal.
You may have noticed that in the previous examples of the add function in Python plug-ins and Java plug-ins, Laiye RPA returns the same result regardless of which function it calls. However, the internal implementation of different plug-ins is very different. For example, in Python language, UTF-8 encoding is used to save strings, while in .Net, UTF-8 is used by default. But Laiye RPA has helped you smooth out these differences, so that you do not have to care about these details during use.
Similar to the Python and Java plug-ins, in the .Net plug-in, you can also use the plug-in API, which calls part of Laiye RPA's functions in turn. If you want to call the plug-in API, you only need to write a plug-in based on our template, without any other settings. The names, parameters and meanings of the plug-in APIs that can be used in the .Net plug-in are exactly the same as the Java plug-in. For example, UiBot.IsStop() can be used to detect whether the current process needs to stop immediately. Please refer to the explanation of the plug-in in the Java plug-in for more details.
Transfer of Variables
Similar to Java, C#.Net is also a static programming language. Variables need to be defined with their types before use. Moreover, arrays can usually only contain the same type of data. This is very different from Laiye RPA's dynamic type.
Therefore, when writing and using .Net plug-ins, the following requirements must be met:
• For basic type parameters such as integers, floating-point numbers, strings, and Boolean types, Laiye RPA does not strictly check the type of the .Net plug-in. And even if the conversion is unsuccessful, it will not report an error. Therefore, please pay special attention to the type of each parameter when using it to avoid incorrect values being passed in and not found in time.
• If you need to transfer the dictionary or array type from Laiye RPA to the .Net plug-in, the parameter type in the .Net plug-in can only use Newtonsoft.Json.Ling.JArray (corresponding to the array) or Newtonsoft.Json.Ling.JObject (corresponding to the dictionary). In the template, since we have imported the class Newtonsoft.Json.Linq, the prefix can be omitted and abbreviated as JArray (corresponding array) or JObject (corresponding dictionary), which is also used in the following simplified writing.
• If you need to pass the dictionary or array type from the .Net plug-in to Laiye RPA, the return value type in the .Net plug-in can only use JArray (corresponding array) or JObject (corresponding array). Laiye RPA will automatically convert the return value of type JArray into an array and the return value of type JObject into a dictionary.
• Regardless of incoming parameters or return values, these composite types are passed by value between .Net plug-ins and Laiye RPA, rather than by reference.
In the plug-in template, there is a Concat function as an example to demonstrate how to transfer two arrays from Laiye RPA to the .Net plug-in, and how to return the result of connecting the two arrays to Laiye RPA. We recommend reading this section carefully.
JArray and JObject are not built into the .Net Framework, but are implemented using open source Json.Net. When compiling and running, you need to rely on a file called Newtonsoft.Json.dll. You may have noticed that as long as you put this file in the directory where the plug-in source code is located before compiling, and put this file in the extend/DotNet directory before running, you can compile and use it normally. We have put in Newtonsoft.Json.dll for you in advance. If your plug-in still needs to rely on other files, just follow this procedure.
The function in the .Net plug-in supports default parameters. When calling, if some parameters have default values, you do not need to pass the value, since this parameter will automatically take the default value.
An exception can be thrown in the function of the .Net plug-in. The exception may or may not be caught by the .Net plug-in. If the .Net plug-in does not catch the exception, it will be automatically transferred to Laiye RPA, which it will be caught. If Laiye RPA does not catch the exception either, then the operation of the process will exit with an error, and the error message will say that there was an exception in the .Net file, which will help you troubleshoot the problem.
The variables and functions in .Net are case sensitive, but when using the .Net plug-in in Laiye RPA, you can call the functions in a case-insensitive manner. For example, you can write DotNet.add(1,1) or dotnet.ADD(1,1), and the effect is exactly the same.