【事件】更新部门
飞书人事中「部门信息被更新」时将触发此事件。
Error: - v2版本事件支持上级部门的创建事件先于下级部门更新事件推送,但由于事件不稳定的特性,无法保证100%的事件有序,请确保集成时做好兼容处理(有序事件相较于无序事件时效性有所下降,若不依赖事件有序且对事件时效有强诉求,可使用v1版本事件【事件】更新部门)
Warning: - 【搜索部门信息】【获取父部门信息】 等接口数据查询存在一定延迟,不建议收到事件后立即查询以上接口。
Tip: - 该接口只会推送当前生效对象的变更事件。
- 未来生效的版本数据,会在生效日期当天凌晨推送事件。例如:今天为1月1日,修改对象名称并填写1月10日生效,则1月10日凌晨发送该对象变更事件。
- 收到事件后,可以立即通过【批量查询部门】查询到最新变更数据。
- 以下字段变更会收到事件:organization_id.name(部门名称)、organization_id.code(部门编码)、organization_id.description(部门描述)、organization_id.superior_org(上级部门)、organization_id.active(部门状态)、subtype(部门类型)、manager(部门负责人)、is_confidential(是否保密)、effective_time(生效时间)、cost_center(成本中心)、staffing_model(岗职模式)、lark_chat_id(部门群ID)、自定义字段(不包括其他部门角色)
- 以下字段变更不会收到事件:list_order(列表排序)、tree_order(树状排序)
事件
| 项目 | 值 |
|---|---|
| 事件类型 | corehr.department.updated_v2 |
| 支持的应用类型 | custom,isv |
| 权限要求 订阅该事件所需的权限,开启其中任意一项权限即可订阅 开启任一权限即可 | corehr:department:read 获取部门信息 corehr:department:write 读写部门信息 |
| 推送方式 | Webhook |
事件体
| 名称 | 类型 | 描述 |
|---|---|---|
schema | string | 事件模式 |
header | event_header | 事件头 |
└ event_id | string | 事件 ID |
└ event_type | string | 事件类型 |
└ create_time | string | 事件创建时间戳(单位:毫秒) |
└ token | string | 事件 Token |
└ app_id | string | 应用 ID |
└ tenant_key | string | 租户 Key |
event | \- | - |
└ department_id | string | 部门id |
└ field_changes | string\[\] | 发生变更的字段 数据校验规则: - 长度范围: 0 ~ 1000 |
事件体示例
json
{
"schema": "2.0",
"header": {
"event_id": "5e3702a84e847582be8db7fb73283c02",
"event_type": "corehr.department.updated_v2",
"create_time": "1608725989000",
"token": "rvaYgkND1GOiu5MM0E1rncYC6PLtF7JV",
"app_id": "cli_9f5343c580712544",
"tenant_key": "2ca1d211f64f6438"
},
"event": {
"department_id": "7043711774159341101",
"field_changes": [
"manager"
]
}
}事件订阅示例代码
订阅方式
长连接方式(推荐):无需发布到公网地址,在本地开发环境中即可接收事件回调,且无需处理加解密逻辑。 发送至开发者服务器:需要提供服务器公网地址。
package main
import (
"context"
"fmt"
larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
larkevent "github.com/larksuite/oapi-sdk-go/v3/event"
"github.com/larksuite/oapi-sdk-go/v3/event/dispatcher"
"github.com/larksuite/oapi-sdk-go/v3/service/corehr/v2"
larkws "github.com/larksuite/oapi-sdk-go/v3/ws"
)
// SDK 使用说明 SDK user guide:https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/server-side-sdk/golang-sdk-guide/preparations
func main() {
// 注册事件 Register event
eventHandler := dispatcher.NewEventDispatcher("", "").
OnP2DepartmentUpdatedV2(func(ctx context.Context, event *larkcorehr.P2DepartmentUpdatedV2) error {
fmt.Printf("[ OnP2DepartmentUpdatedV2 access ], data: %s\n", larkcore.Prettify(event))
return nil
})
// 构建 client Build client
cli := larkws.NewClient("YOUR_APP_ID", "YOUR_APP_SECRET",
larkws.WithEventHandler(eventHandler),
larkws.WithLogLevel(larkcore.LogLevelDebug),
)
// 建立长连接 Establish persistent connection
err := cli.Start(context.Background())
if err != nil {
panic(err)
}
}# SDK 使用说明 SDK user guide:https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/server-side-sdk/python--sdk/preparations-before-development
import lark_oapi as lark
def do_p2_corehr_department_updated_v2(data: lark.corehr.v2.P2CorehrDepartmentUpdatedV2) -> None:
print(f'[ do_p2_corehr_department_updated_v2 access ], data: {lark.JSON.marshal(data, indent=4)}')
# 注册事件 Register event
event_handler = lark.EventDispatcherHandler.builder("", "") \
.register_p2_corehr_department_updated_v2(do_p2_corehr_department_updated_v2) \
.build()
def main():
# 构建 client Build client
cli = lark.ws.Client("APP_ID", "APP_SECRET",
event_handler=event_handler, log_level=lark.LogLevel.DEBUG)
# 建立长连接 Establish persistent connection
cli.start()
if __name__ == "__main__":
main()package com.example.sample;
import com.lark.oapi.core.utils.Jsons;
import com.lark.oapi.service.corehr.CorehrService;
import com.lark.oapi.service.corehr.v2.model.P2DepartmentUpdatedV2;
import com.lark.oapi.event.EventDispatcher;
import com.lark.oapi.ws.Client;
// SDK 使用说明 SDK user guide:https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/server-side-sdk/java-sdk-guide/preparations
public class Sample {
// 注册事件 Register event
private static final EventDispatcher EVENT_HANDLER = EventDispatcher.newBuilder("", "")
.onP2DepartmentUpdatedV2(new CorehrService.P2DepartmentUpdatedV2Handler() {
@Override
public void handle(P2DepartmentUpdatedV2 event) throws Exception {
System.out.printf("[ onP2DepartmentUpdatedV2 access ], data: %s\n", Jsons.DEFAULT.toJson(event.getEvent()));
}
})
.build();
public static void main(String[] args) {
// 构建 client Build client
Client client = new Client.Builder("APP_ID", "APP_SECRET")
.eventHandler(EVENT_HANDLER)
.build();
// 建立长连接 Establish persistent connection
client.start();
}
}// SDK 使用说明 SDK user guide:https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/server-side-sdk/nodejs-sdk/preparation-before-development
import * as Lark from '@larksuiteoapi/node-sdk';
const baseConfig = {
appId: 'APP_ID',
appSecret: 'APP_SECRET'
}
// 构建 client Build client
const wsClient = new Lark.WSClient(baseConfig);
// 建立长连接 Establish persistent connection
wsClient.start({
// 注册事件 Register event
eventDispatcher: new Lark.EventDispatcher({}).register({
'corehr.department.updated_v2': async (data) => {
console.log(data);
}
})
});package main
import (
"context"
"fmt"
"net/http"
larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
"github.com/larksuite/oapi-sdk-go/v3/core/httpserverext"
larkevent "github.com/larksuite/oapi-sdk-go/v3/event"
"github.com/larksuite/oapi-sdk-go/v3/event/dispatcher"
"github.com/larksuite/oapi-sdk-go/v3/service/corehr/v2"
)
// SDK 使用说明 SDK user guide:https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/server-side-sdk/golang-sdk-guide/preparations
func main() {
// 注册事件 Register event
eventHandler := dispatcher.NewEventDispatcher("", "").
OnP2DepartmentUpdatedV2(func(ctx context.Context, event *larkcorehr.P2DepartmentUpdatedV2) error {
fmt.Printf("[ OnP2DepartmentUpdatedV2 access ], data: %s\n", larkcore.Prettify(event))
return nil
})
// 创建路由处理器 Create route handler
http.HandleFunc("/webhook/event", httpserverext.NewEventHandlerFunc(handler, larkevent.WithLogLevel(larkcore.LogLevelDebug)))
err := http.ListenAndServe(":7777", nil)
if err != nil {
panic(err)
}
}# SDK 使用说明 SDK user guide:https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/server-side-sdk/python--sdk/preparations-before-development
from flask import Flask
from lark_oapi.adapter.flask import *
import lark_oapi as lark
app = Flask(__name__)
def do_p2_corehr_department_updated_v2(data: lark.corehr.v2.P2CorehrDepartmentUpdatedV2) -> None:
print(f'[ do_p2_corehr_department_updated_v2 access ], data: {lark.JSON.marshal(data, indent=4)}')
# 注册事件 Register event
event_handler = lark.EventDispatcherHandler.builder("", "") \
.register_p2_corehr_department_updated_v2(do_p2_corehr_department_updated_v2) \
.build()
# 创建路由处理器 Create route handler
@app.route("/webhook/event", methods=["POST"])
def event():
resp = event_handler.do(parse_req())
return parse_resp(resp)
if __name__ == "__main__":
app.run(port=7777)package com.lark.oapi.sample.event;
import com.lark.oapi.core.utils.Jsons;
import com.lark.oapi.service.corehr.CorehrService;
import com.lark.oapi.service.corehr.v2.model.P2DepartmentUpdatedV2;
import com.lark.oapi.sdk.servlet.ext.ServletAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// SDK 使用说明 SDK user guide:https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/server-side-sdk/java-sdk-guide/preparations
@RestController
public class EventController {
// 注册事件 Register event
private static final EventDispatcher EVENT_HANDLER = EventDispatcher.newBuilder("verificationToken", "encryptKey")
.onP2DepartmentUpdatedV2(new CorehrService.P2DepartmentUpdatedV2Handler() {
@Override
public void handle(P2DepartmentUpdatedV2 event) throws Exception {
System.out.printf("[ onP2DepartmentUpdatedV2 access ], data: %s\n", Jsons.DEFAULT.toJson(event.getEvent()));
}
})
.build();
// 注入 ServletAdapter 实例 Inject ServletAdapter instance
@Autowired
private ServletAdapter servletAdapter;
// 创建路由处理器 Create route handler
@RequestMapping("/webhook/event")
public void event(HttpServletRequest request, HttpServletResponse response)
throws Throwable {
// 回调扩展包提供的事件回调处理器 Callback handler provided by the extension package
servletAdapter.handleEvent(request, response, EVENT_DISPATCHER);
}
}// SDK 使用说明 SDK user guide:https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/server-side-sdk/nodejs-sdk/preparation-before-development
import http from 'http';
import * as lark from '@larksuiteoapi/node-sdk';
// 注册事件 Register event
const eventDispatcher = new lark.EventDispatcher({
encryptKey: '',
verificationToken: '',
}).register({
'corehr.department.updated_v2': async (data) => {
console.log(data);
return 'success';
},
});
const server = http.createServer();
// 创建路由处理器 Create route handler
server.on('request', lark.adaptDefault('/webhook/event', eventDispatcher));
server.listen(3000);