python
import lark_oapi as lark
import requests
import json
from lark_oapi.event.callback.model.p2_card_action_trigger import (
P2CardActionTrigger,
P2CardActionTriggerResponse
)
APP_ID = "XXX"
APP_SECRET = "xxx"
# 接收卡片交互事件信息的Webhook
MY_CARD_WEBHOOK_URL = "xxx"
# 接收审批任务状态变更事件的Webhook
MY_APPROVE_WEBHOOK_URL = "xxx"
def push_all_event_info_to_webhook(event_json,WEBHOOK_URL):
payload = {
"data": event_json
}
res = requests.post(
url=WEBHOOK_URL,
json=payload,
timeout=10
)
# print("推送 Webhook 返回:\n", res.text)
def handle_card_action(data: P2CardActionTrigger) -> P2CardActionTriggerResponse:
full_event_json = lark.JSON.marshal(data.event)
# print("完整回调数据:\n", full_event_json)
# 将完整事件信息推送到你的 Webhook
push_all_event_info_to_webhook(full_event_json,MY_CARD_WEBHOOK_URL)
#把 JSON 字符串 解析为 Python 字典
full_event_dict = json.loads(full_event_json)
record_id = full_event_dict["action"]["value"]["record_id"]
submit_time = full_event_dict["action"]["value"]["submit_time"]
creator_open_id = full_event_dict["action"]["value"]["creator_open_id"]
enterprise_name = full_event_dict["action"]["value"]["enterprise_name"]
customer_problem = full_event_dict["action"]["value"]["customer_problem"]
on_duty_staff_at_str = full_event_dict["action"]["value"]["on_duty_staff_at_str"]
ai_feedback = full_event_dict["action"]["value"]["ai_feedback"]
record_link = full_event_dict["action"]["value"]["record_link"]
process_status = full_event_dict["action"]["value"]["process_status"]
new_process_status = ""
new_status_button_type = ""
is_disabled = False
if process_status == "点我开始处理":
# print("当前状态:待完成,更新为处理中")
new_process_status = "进行中|点我确认完成"
new_status_button_type = "danger"
is_disabled = False
elif process_status == "进行中|点我确认完成":
# print("当前状态:处理中,更新为已完成")
new_process_status = "✅已完成"
new_status_button_type = "primary"
is_disabled = True
resp ={
"toast": {
"type": "success",
"content": "处理状态已更新"
},
"card": {
"type": "raw",
"data": {
"schema": "2.0",
"config": {
"update_multi": True,
"style": {
"text_size": {
"normal_v2": {
"default": "normal",
"pc": "normal",
"mobile": "heading"
}
}
}
},
"body": {
"direction": "vertical",
"horizontal_spacing": "8px",
"vertical_spacing": "8px",
"horizontal_align": "left",
"vertical_align": "top",
"elements": [
{
"tag": "markdown",
"content": f"**记录ID**:{record_id}\n**提交时间**:{submit_time}\n**反馈人员**:<at id={creator_open_id}></at>\n**客户名称**:{enterprise_name}\n**客户问题**:{customer_problem}\n**值班人员**:{on_duty_staff_at_str}\n\n---\n**AI给出的解决方案**:{ai_feedback}",
"text_align": "left",
"text_size": "normal_v2",
"margin": "0px 0px 0px 0px"
},
{
"tag": "form",
"elements": [
{
"tag": "column_set",
"flex_mode": "flow",
"horizontal_spacing": "8px",
"horizontal_align": "left",
"columns": [
{
"tag": "column",
"width": "auto",
"elements": [
{
"tag": "column_set",
"horizontal_align": "left",
"columns": [
{
"tag": "column",
"width": "weighted",
"elements": [
{
"tag": "button",
"text": {
"tag": "plain_text",
"content": f"{new_process_status}"
},
"type": f"{new_status_button_type}",
"width": "default",
"size": "medium",
"disabled":is_disabled,
"behaviors": [
{
"type": "callback",
"value": {
"creator_open_id": f"{creator_open_id}",
"enterprise_name": f"{enterprise_name}",
"customer_problem": f"{customer_problem}",
"on_duty_staff_at_str": f"{on_duty_staff_at_str}",
"ai_feedback": f"{ai_feedback}",
"record_link": f"{record_link}",
"record_id": f"{record_id}",
"submit_time": f"{submit_time}",
"process_status":f"{new_process_status}"
}
}
],
"form_action_type": "submit",
"name": "Button_y4egdrrym28",
"margin": "0px 0px 0px 0px",
"element_id": "status"
}
],
"vertical_spacing": "8px",
"horizontal_align": "left",
"vertical_align": "top",
"weight": 1
},
{
"tag": "column",
"width": "auto",
"elements": [
{
"tag": "button",
"text": {
"tag": "plain_text",
"content": "查看详情"
},
"type": "primary",
"width": "default",
"size": "medium",
"behaviors": [
{
"type": "open_url",
"default_url": f"{record_link}",
"pc_url": f"{record_link}",
"ios_url": f"{record_link}",
"android_url": f"{record_link}"
}
],
"name": "Button_e2r07h7egr"
}
],
"vertical_spacing": "8px",
"horizontal_align": "left",
"vertical_align": "top"
}
]
}
],
"vertical_spacing": "8px",
"horizontal_align": "left",
"vertical_align": "top"
}
]
}
],
"direction": "vertical",
"horizontal_align": "left",
"vertical_align": "top",
"padding": "0px 0px 0px 0px",
"margin": "0px 0px 0px 0px",
"name": "approval_form"
},
{
"tag": "markdown",
"content": f"---\n来自 [值班-问题记录表](https://ying-dao.feishu.cn/wiki/GZlfwzHIDijkLNklIGNcxNo3nbd?table=tblAO3koPJNcP9z0&view=vewpDr4K5O)",
"text_align": "left",
"text_size": "normal_v2",
"margin": "0px 0px 0px 0px"
}
]
},
"header": {
"title": {
"tag": "plain_text",
"content": f"你的[值班问题单]已被创建,请查收~~"
},
"subtitle": {
"tag": "plain_text",
"content": ""
},
"template": "purple"
}
}
}
}
return P2CardActionTriggerResponse(resp)
# 处理审批任务状态变更
def on_approval_change(event):
approve_event_str=lark.JSON.marshal(event)
approve_event_dict = json.loads(approve_event_str)
# print(type(approve_event_dict))
if approve_event_dict["event"]["status"] == "PENDING" and approve_event_dict["event"]["user_id"] == "2gg4c54f":
#如果有需要XX审批的单子,那么需要转发到Webhook
# 将完整事件信息推送到你的 Webhook
push_all_event_info_to_webhook(approve_event_str,MY_APPROVE_WEBHOOK_URL)
print("已转发")
handler = (
lark.EventDispatcherHandler.builder("", "")
.register_p2_card_action_trigger(handle_card_action)
.register_p1_customized_event("approval_task", on_approval_change)
.build()
)
client = lark.ws.Client(
app_id=APP_ID,
app_secret=APP_SECRET,
event_handler=handler,
log_level=lark.LogLevel.INFO
)
client.start()