UiPath Documentation
robot
2022.10
false
Robot 开发者指南
  • Robot JavaScript SDK
    • 关于 Robot JavaScript SDK
    • 配置步骤
重要 :
请注意,此内容已使用机器翻译进行了部分本地化。 新发布内容的本地化可能需要 1-2 周的时间才能完成。

SDK 规范

方法和属性

您可以在自定义应用程序或网页中包含以下方法和属性。

初始化

init方法是可选的。 它返回 IRobotSDK 实例,您可以将其存储为变量以供以后使用。

const robot = UiPathRobot.init();
const robot = UiPathRobot.init();

设置

“设置”类型的 settings 属性让您可以更改默认端口号、轮询间隔时间(以毫秒为单位),或禁用发送遥测数据。

const robot = UiPathRobot.init();
robot.settings.portNumber = 1234;
robot.settings.pollTimeInterval = 1000;
robot.settings.disableTelemetry = true;
const robot = UiPathRobot.init();
robot.settings.portNumber = 1234;
robot.settings.pollTimeInterval = 1000;
robot.settings.disableTelemetry = true;

启动

on方法用于将事件处理程序附加到 SDK。

可用的 SDK 事件为 consent-promptmissing-componentsopen-uipath-protocol

每次您的自定义应用程序或网页需要连接到机器人时,UiPath JavaScript SDK 都会显示内置的同意叠加层。

可以使用自定义处理程序覆盖此同意覆盖。

const robot = UiPathRobot.init();
robot.on('consent-prompt', consentCode => console.log(consentCode));
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'));
const robot = UiPathRobot.init();
robot.on('missing-components', () => console.log('Missing Components'));

open-uipath-protocol

在显示同意提示之前,系统可能会要求您允许浏览器在关联应用程序中打开 uipath-web URL。

默认情况下,RobotJS 会自动尝试打开此 URL,但用户可以通过提供自定义事件处理程序来覆盖此行为。

const robot = UiPathRobot.init();
robot.on('open-uipath-protocol', (protocolUrl) => console.log('RobotJS protocol URL: ' + protocolUrl));
const robot = UiPathRobot.init();
robot.on('open-uipath-protocol', (protocolUrl) => console.log('RobotJS protocol URL: ' + protocolUrl));

获取进程

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);
}
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)
}
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();
const robot = UiPathRobot.init();
const processes = await robot.getProcesses();
const myProcess = processes.find(p => p.name === "MyProcess");
const installedProcess = await myProcess.install();

开始作业

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);
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);
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方法用于将事件处理程序附加到作业。 可用的事件为statusworkflow-event

状态

每次作业状态发生更改时,都会在作业执行期间引发status事件。

每次执行“报告状态”活动时,也会引发此事件。

const robot = UiPathRobot.init();
const job = robot.createJob('processId');
job.on('status', robotStatus => console.log(robotStatus));
await robot.startJob(job);
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);
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);
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));
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));
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));

停止流程

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);
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>;
}
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 Settings {
    portNumber: number;
    pollTimeInterval: number;
    disableTelemetry: boolean;
}

机器人流程

class RobotProcess {
    id: string;
    name: string;
    description?: string;
    start: (inArguments?: any) => JobPromise;
    install: () => Promise<InstallProcessResult>;
}
class RobotProcess {
    id: string;
    name: string;
    description?: string;
    start: (inArguments?: any) => JobPromise;
    install: () => Promise<InstallProcessResult>;
}

安装流程结果

class InstallProcessResult {
    inputArgumentsSchema: InputArgumentSchema[]
}
class InstallProcessResult {
    inputArgumentsSchema: InputArgumentSchema[]
}

输入参数架构

class InputArgumentSchema {
    name: string;
    type: string;
    isRequired: boolean;
    hasDefault: boolean;
}
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 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 Job {
    processId: string;
    argument?: any;
    jobId: string;
    on(eventName: string, eventHanlder: (argument?: any) => void): void;
}

作业结果

class JobResult {
    [Key: string]: any;
}
class JobResult {
    [Key: string]: any;
}

工作流事件数据

class WorkflowEventData {
    name: string;
    payload: string;
}
class WorkflowEventData {
    name: string;
    payload: string;
}

此页面有帮助吗?

连接

需要帮助? 支持

想要了解详细内容? UiPath Academy

有问题? UiPath 论坛

保持更新