如何快速上下架机器人Action

基础概念

  • action 是机器人执行过程中的节点,最简单的理解就是他是一个函数。最简单的实现也是云函数。

    • 一个函数只专注于干一件事情。

    • action 可以组合起来,编排流程,完成一系列的工作。

  • service 服务,action 是归属于一个服务的。想要提供 action 必须先注册一个服务。

    • 例如:服务「 维格表」提供了「创建记录」、「查询记录」的 action。


前提条件


上架流程

sequenceDiagram
    Dev->>+Service(automaition): 1. create automaition service
    Service(automaition)-->>+ Dev: 2. response Ok and return serviceId
    Dev->>+Action(automaition): 3. create automation action relate to serviceId(step2)
    Action(automaition)-->>-Dev: 4. response Ok and return actionId
    Dev-->>- UI: 5.test automation


下架流程

sequenceDiagram
    Dev->>+Action(automaition): 1. delete automation action
    Action(automaition)-->>-Dev: 2. response Ok
    Dev->>+Service(automaition): 3. delete automaition service
    Service(automaition)-->>+ Dev: 4. response Ok
    Dev-->>- UI: 5.test automation



如何新建一个新机器人Action

  1. 准备一个要调用的api接口,有如下形式的:

    1. 请求地址:{your_api_url}

    2. 请求方法:POST

    3. 请求头使用API Token

    4. 请求体:json字符串,自定义键值对,值类型:字符串、数组、Object。

    5. 响应体结构要求:

{
  success: boolean,
  code: number,
  data?: object, // 现阶段不会被使用。
}

上述描述了一个需要调用的 API 接口的格式和要求:

该 API 接口是一个 POST 请求,需要符合特定的请求体和响应体格式。

其中,请求体的具体内容未给出,而响应体包括一个布尔类型的 success 字段表示请求是否成功,一个数字类型的 code 字段表示请求的返回码,以及一个 data 字段表示请求返回的数据。

为了准备调用该 API 接口的环境,可以根据自己熟悉的编程语言选择相应的框架或工具来实现。

  1. 如果你熟悉 Java,则可以新建一个 Spring Boot 项目,添加 Spring Web 相关依赖,然后添加一个符合要求的 API 接口,并将其部署到服务器上。

  2. 如果你熟悉 Node.js,则可以新建一个 Express 项目,添加一个符合要求的 API 接口,并将其部署到服务器上。

  3. 另外,也可以使用云函数来实现该 API 接口。

总之,根据上述要求准备一个可调用的 API 接口,是即将新增的 action 执行所必需的。

  1. 调用vika的api接口,向vika添加service:

post /api/v1/automation/service/create
request body {
  serviceId: string, // 这个是service的唯一标识id,自定义
  slug: string, // service的标识,自定义
  name: string, // service的名称,自定义
  description: string, // service的描述,自定义
  logo: string, // service的logo,一个图片地址,自定义
  // 如果第一步中准备的api接口为:https://service.xxx.tencentcs.com/release/sendMessage
  // 则这里的值为https://service.xxx.tencentcs.com/release/。
  baseUrl: string, 
  i18n: string, // json,多语言,值可以为:"{\"en\": {\"$name\": \"测试机器人\"}, \"zh\": {\"$name\": \"测试机器人\"}}"
}
  1. 调用vika的api接口,向vika添加action_type:

post /api/v1/automation/actionType/create
request body {
  serviceId: string, // 第二步中的serviceId
  actionTypeId: string, // 自定义
  name: string, // 自定义
  description: string, // 自定义
  inputJsonSchema: string, // api请求体的json字符串的JsonSchame描述。
  outputJsonSchema: string, // api响应内容data属性的JsonSchame描述。
  // 如果第一步中准备的api接口为:https://service.xxx.tencentcs.com/release/sendMessage
  // 则这里的值为sendMessage。
  endpoint: string,
  i18n: string, // json,多语言,值可以为:"{ }"
}

inputJsonSchemaoutputJsonSchema 的定义:Action’s JsonSchema

例子:

下列是官网webhook action的添加过程。

  1. 有腾讯云函数:

post https://service.xxx.tencentcs.com/release/sendRequest 
request body {
  method: string, // 例如:'get'  
  url: string, // 例如:'https://www.baidu.com'
}
reponse body {
  success: boolean, // 例如:true
  code: number, // 例如:200
  data: {
    status: number;
    json?: any;
  }
}
  1. 向vika新增service:

post /api/v1/automation/service/create
header {
   Content-Type: application/json
   Authorization: Bearer {你的 API Token}
}
request body {
  serviceId: 'asvTsTRCNct0KiIrKQ',
  slug: 'webhook',
  name: '$webhook',
  description: '',
  logo: 'https://s1.apitable.com/xxx',
  baseUrl: 'https://service.xxx.tencentcs.com/release/', 
  i18n: '{"en": {"$webhook": "Webhook"}, "zh": {"$webhook": "网络请求"}}',
}
  1. 向vika新增action_type:

post /api/v1/automation/actionType/create
request body {
  serviceId: 'asvTsTRCNct0KiIrKQ', // 上面service id
  actionTypeId: 'aatSSHdFkR7B7197Is',
  name: '$robot_action_send_web_request_title',
  description: '$robot_action_send_web_request_desc',
  inputJsonSchema: "{"schema": {"type": "object", "required": ["url", "method"],"properties": {"url": {"type": "string","title": "$robot_action_send_web_request_config_2"},"method": {"enum": ["GET","POST","PATCH","DELETE"],"type": "string","title": "$robot_action_send_web_request_config_1","default": "GET","description": "$robot_action_send_web_request_config_1_desc"}},"additionalProperties": false},"uiSchema": {"ui:order": ["method","url"]}}",
  outputJsonSchema: "{"schema": {"type": "object","required": ["status"],"properties": {"status": {"title": "状态码","type": "number"},"json": {"title": "消息","type": "object"}},"additionalProperties": false},"uiSchema": {"ui:order": ["status","json"]}}",
  endpoint: 'sendRequest',
  i18n: "{"en": {"$robot_action_send_web_request_desc": "When the robot starts working, it will automatically send a web request to a specific URL","$robot_action_send_web_request_title": "Send web request","$robot_action_send_web_request_config_1": "Request method","$robot_action_send_web_request_config_2": "Request address","$robot_action_send_web_request_config_1_desc": "Send web request via the GET, POST or other method"},"zh": {"$robot_action_send_web_request_desc": "机器人开始运行后,会自动向指定地址发送网络请求","$robot_action_send_web_request_title": "发送网络请求","$robot_action_send_web_request_config_1": "请求方法","$robot_action_send_web_request_config_2": "请求地址","$robot_action_send_web_request_config_1_desc": "通过 GET 或 POST 等方式向指定地址发送网络请求"}}"
}

inputJsonSchemaoutputJsonSchema 的定义:Action’s JsonSchema

inputJsonShcema格式化:

{
  "schema": {
    "type": "object",
    "required": [
      "url",
      "method"
    ],
    "properties": {
      "url": {
        "type": "string",
        "title": "$robot_action_send_web_request_config_2"
      },
      "method": {
        "enum": [
          "GET",
          "POST",
          "PATCH",
          "DELETE"
        ],
        "type": "string",
        "title": "$robot_action_send_web_request_config_1",
        "default": "GET",
        "description": "$robot_action_send_web_request_config_1_desc"
      }
    },
    "additionalProperties": false
  },
  "uiSchema": {
    "ui:order": [
      "method",
      "url"
    ],
  }
}

outputJsonSchema格式化:

{
  "schema": {
    "type": "object",
    "required": [
      "status"
    ],
    "properties": {
      "status": {
        "title": "状态码",
        "type": "number"
      },
      "json": {
        "title": "消息",
        "type": "object"
      }
    },
    "additionalProperties": false
  },
  "uiSchema": {
    "ui:order": [
      "status",
      "json"
    ]
  }
}

i18n格式化:

{
  "schema": {
    "type": "object",
    "required": [
      "status"
    ],
    "properties": {
      "status": {
        "title": "状态码",
        "type": "number"
      },
      "json": {
        "title": "消息",
        "type": "object"
      }
    },
    "additionalProperties": false
  },
  "uiSchema": {
    "ui:order": [
      "status",
      "json"
    ]
  }
}
  1. action上架完成后,就可以在vika上添加自定义的action,而后根据你添加的触发器(提交表单、更新记录还是记录符合条件),自动触发执行action。

前置条件

在config.yaml文件,设置「超级管理员」:

config:
  common:
  backend_server:
    SUPER_ADMINISTRATORS: {用户的uuid}

如何上架机器人?

1、创建机器人Service

新增Service

post /api/v1/automation/service/create
request body {
  serviceId: string,
  slug: string,
  name: string,
  description: string,
  logo: string,
  baseUrl: string,
  i18n: string,
}

更改Service

post /api/v1/automation/service/edit/{serviceId}
request body {
  name: string,
  description: string,
  logo: string,
  baseUrl: string,
  i18n: string,
}

删除Service

post /api/v1/automation/service/delete/{serviceId}

2、创建新机器人Action Type

上架 Action Type

post /api/v1/automation/actionType/create
request body {
  serviceId: string,
  actionTypeId: string,
  name: string,
  description: string,
  inputJsonSchema: string,
  outputJsonSchema: string,
  endpoint: string,
  i18n: string,
}

更改 Action Type

post /api/v1/automation/actionType/edit/{actionTypeId}
request body {
  name: string,
  description: string,
  inputJsonSchema: string,
  outputJsonSchema: string,
  endpoint: string,
  i18n: string,
}

下架 机器人Action Type

替换「actionTypeId」参数,下架对应的机器人,{actionTypeId}参考下方值 :

  • 发送网络请求:aatSSHdFkR7B7197Is

  • 飞书机器人: aatR7OliJQBNNBokvA

  • 钉钉机器人: aatjSOA2qjh0Q48oJL

  • 企微机器人: aat8sBe4F5TeooKFtl

post /api/v1/automation/actionType/delete/{actionTypeId}

例如,下架飞书机器人的API请求

post /api/v1/automation/actionType/delete/aatR7OliJQBNNBokvA