robot
latest
false
  • Robot JavaScript SDK
    • 关于 Robot JavaScript SDK
    • 配置步骤
UiPath logo, featuring letters U and I in white

Robot developer guide

上次更新日期 2024年12月17日

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-components

缺少组件

当机器人 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'));

获取进程

getProcesses 方法检索可用流程的列表。 将 Promise 返回给 RobotProcess 对象数组。 如果机器人已连接到 Orchestrator,则会从机器人所属的环境和文件夹中检索流程。 否则,将检索本地流程。
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);
}

获取输入参数架构

Using the inputArgumentsSchema property of an installed process, you can retrieve the input arguments of a process. Before using the inputArgumentsSchema property, you need to first install the process.
installProcess 方法会安装流程并将 Promise 返回给 InstallProcessResult 类型的对象,其中包含 InputArgumentSchema 对象数组。
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)
}
注意:在 TypeScript 中,RobotProcess 对象的 install 方法可用于安装流程。
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();

开始作业

startJob 方法接收并启动已创建的 Job 。 它将 Promise 返回给 JobResult类型的对象,其中包含输出参数。
createJob 方法使用流程 ID 创建机器人作业。 它可以选择接收作业输入参数。 它返回一个 作业 对象。
注意:从 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);
注意:在 TypeScript 中,可以使用 Job 类构造函数创建作业。
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事件。

每次执行 ReportStatus 活动时,也会发生此事件。

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);

工作流事件

每次执行发送工作流事件的活动时,在作业执行期间都会引发 workflow-event 事件。 它支持在“ 发送临时结果 ”活动的帮助下实施部分结果。 事件处理程序具有类型为 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);

启动进程

对象 RobotProcessstart 方法用于通过向流程传递输入参数(如果可用)来启动流程。

该方法会返回一个 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方法用于停止正在执行的机器人流程。

It returns a promise which is resolved on successful cancellation of the running robot process.

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;
}

此页面有帮助吗?

获取您需要的帮助
了解 RPA - 自动化课程
UiPath Community 论坛
Uipath Logo White
信任与安全
© 2005-2024 UiPath。保留所有权利。