方法和属性
您可以在自定义应用程序或网页中包含以下方法和属性。
初始化
The init
method is optional. It returns the IRobotSDK instance, which enables you to store it as a variable for later use.
const robot = UiPathRobot.init();
设置
The settings
property of type Settings allows you to change the default port number, the poll interval time in milliseconds, or to disable sending telemetry data.
const robot = UiPathRobot.init();
robot.settings.portNumber = 1234;
robot.settings.pollTimeInterval = 1000;
robot.settings.disableTelemetry = true;
启动
on
方法用于将事件处理程序附加到 SDK。
可用的 SDK 事件为consent-prompt
和missing-components
。
同意提示
每次您的自定义应用程序或网页需要连接到机器人时,UiPath JavaScript SDK 都会显示内置的同意叠加层。
可以使用自定义处理程序覆盖此同意覆盖。
const robot = UiPathRobot.init();
robot.on('consent-prompt', consentCode => console.log(consentCode));
缺少组件
当机器人 JS 服务未运行时,将引发missing-components
事件。 在这种情况下,SDK 会显示一个错误,可以使用自定义处理程序覆盖该错误。
const robot = UiPathRobot.init();
robot.on('missing-components', () => console.log('Missing Components'));
获取进程
The getProcesses
method retrieves the list of available processes. Returns a Promise to an array of RobotProcess objects. If the Robot is connected to Orchestrator, it retrieves the processes from the environment and folder the Robot is a part of. Otherwise, local processes are retrieved.
const robot = UiPathRobot.init();
const processes = await robot.getProcesses();
for(let i=0; i<processes.length; i++){
console.log(processes[i].name + " (" + processes[i].description + ") - Id=" + processes[i].id);
}
获取输入参数架构
使用已安装流程的“inputArgumentsSchema”属性,您可以检索流程的输入参数。 在使用inputArgumentsSchema
属性之前,您需要先安装流程。
The installProcess
method installs a process and returns a Promise to an object of type InstallProcessResult, which contains an array of InputArgumentSchema objects.
const robot = UiPathRobot.init();
const processes = await robot.getProcesses();
const installedProcess = await robot.installProcess(processes[0].id);
for(let i=0; i<installedProcess.inputArgumentsSchema.length; i++){
console.log("Argument " + i + ":");
console.log(" name: " + installedProcess.inputArgumentsSchema[i].name)
console.log(" type: " + installedProcess.inputArgumentsSchema[i].type)
console.log(" isRequired: " + installedProcess.inputArgumentsSchema[i].isRequired)
console.log(" hasDefault: " + installedProcess.inputArgumentsSchema[i].hasDefault)
}
In TypeScript, the method
install
of a RobotProcess object can be used to install a process.
const robot = UiPathRobot.init();
const processes = await robot.getProcesses();
const myProcess = processes.find(p => p.name === "MyProcess");
const installedProcess = await myProcess.install();
Start Job
The startJob
method receives an already created Job
and starts it. It returns a Promise to an object of type JobResult, which contains the output arguments.
The createJob
method creates a robot Job using the Process Id. It can optionally receive the job input arguments. It returns a Job object.
注意
从 v2022.10 开始,流程列表会在流程执行的同时刷新,在此之前,流程列表会在流程启动之前刷新。这意味着,在运行有可用更新的流程时,作业可能会使用该流程的旧版本运行。
robot = UiPathRobot.init();
processes = await robot.getProcesses();
const calculatorProcess = processes.find(p => p.name.includes('Calculator'));
const arguments = {
"input1" : 23,
"input2" : 42,
"operation" : "add"
};
const job = robot.createJob(calculatorProcess.id, arguments);
const result = await robot.startJob(job);
console.log(result.Sum);
In TypeScript, a Job can be created by using the Job class constructor.
robot = UiPathRobot.init();
processes = await robot.getProcesses();
const calculatorProcess = processes.find(p => p.name.includes('Calculator'));
const arguments = {
"input1" : 23,
"input2" : 42,
"operation" : "add"
};
const job = new Job(calculatorProcess.id, arguments);
const result = await robot.startJob(job);
console.log(result.Sum);
启动作业
Job
对象的on
方法用于将事件处理程序附加到作业。 可用的事件为status
和workflow-event
。
状态
每次作业状态发生更改时,都会在作业执行期间引发status
事件。
This event is also raised each time the ReportStatus activity is executed.
const robot = UiPathRobot.init();
const job = robot.createJob('processId');
job.on('status', robotStatus => console.log(robotStatus));
await robot.startJob(job);
工作流事件
The workflow-event
event is raised during the job execution each time an activity that sends a workflow event is executed. It is the support for implementing partial results with the help of send interim results activity. The event handler has an argument of type WorkflowEventData.
const robot = UiPathRobot.init();
const job = robot.createJob('processId');
job.on('workflow-event', e => console.log(e.name));
await robot.startJob(job);
启动进程
The method start
of the object RobotProcess is used to start a process by passing it input arguments, if available.
It returns an object of type JobPromise.
const robot = UiPathRobot.init();
const processes = await robot.getProcesses();
const myProcess = processes.find(p => p.name === "MyProcess");
const arguments = {
"a" : 41,
"b" " 27
};
const result = await myProcess.start(arguments);
流程开启状态
onStatus
方法用于将事件处理程序附加到作业的“状态”事件。
const robot = UiPathRobot.init();
const processes = await robot.getProcesses();
const myProcess = processes.find(p => p.name === "MyProcess");
const result = await myProcess.start().onStatus(status => console.log(status));
处理工作流事件
onWorkflowEvent
方法用于将事件处理程序附加到作业的“workflow-event”事件。
const robot = UiPathRobot.init();
const processes = await robot.getProcesses();
const myProcess = processes.find(p => p.name === "MyProcess");
const result = await myProcess.start().onWorkflowEvent(e => console.log(e.name));
Stop Process
stopProcess
方法用于停止正在执行的机器人流程。
它返回一个承诺,该承诺在成功取消正在运行的机器人流程后解决。
const robot = UiPathRobot.init();
const processes = await robot.getProcesses();
const myProcess = processes.find(p => p.name === "MyProcess");
const result = await myProcess.start();
await robot.stopProcess(myProcess);
模型定义
IRobotSDK
interface IRobotSDK {
settings: Settings;
getProcesses(): Promise<Array<RobotProcess>>;
init(): IRobotSDK;
on(eventName: string, callback: (argument?: any) => void): void;
startJob(job: Job): Promise<JobResult>;
stopProcess(process: RobotProcess): Promise<void>;
installProcess(processId: string): Promise<InstallProcessResult>;
}
设置
class Settings {
portNumber: number;
pollTimeInterval: number;
disableTelemetry: boolean;
}
机器人流程
class RobotProcess {
id: string;
name: string;
description?: string;
start: (inArguments?: any) => JobPromise;
install: () => Promise<InstallProcessResult>;
}
安装流程结果
class InstallProcessResult {
inputArgumentsSchema: InputArgumentSchema[]
}
输入参数架构
class InputArgumentSchema {
name: string;
type: string;
isRequired: boolean;
hasDefault: boolean;
}
工作承诺
class JobPromise {
onStatus(eventHanlder: (argument?: any) => void): JobPromise;
onWorkflowEvent(eventHanlder: (argument?: any) => void): JobPromise;
}
作业
class Job {
processId: string;
argument?: any;
jobId: string;
on(eventName: string, eventHanlder: (argument?: any) => void): void;
}
作业结果
class JobResult {
[Key: string]: any;
}
工作流事件数据
class WorkflowEventData {
name: string;
payload: string;
}
Updated 26 days ago