python_获取飞书卡片交互和审批任务状态变更事件信息

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()
相关推荐
向日的葵00612 分钟前
langchain的Tools教程(三)
python·langchain·tools
言乐62 小时前
Python实现可运行解密游戏游戏框架
python·游戏·小程序·游戏程序·关卡设计
YUS云生2 小时前
Python学习笔记·第31天:FastAPI入门——路由、路径参数、查询参数与请求体
笔记·python·学习
智写-AI2 小时前
真实有效的免费降英文AI工具服务商
人工智能·python
yuhuofei20213 小时前
【Python入门】了解掌握Python中函数的基本使用
python
白帽小阳4 小时前
2026前端面试题!(附答案及解析)
javascript·网络·python·安全·web安全·网络安全·护网行动
乱写代码5 小时前
Python开发技巧--类型注解Literal
python
卷无止境5 小时前
Python FFI 技术深度解析:ctypes、cffi 与 pybind11 的性能差异与实践挑战
后端·python
郝学胜-神的一滴5 小时前
算法实战:最小k个数——大顶堆的优雅解法
开发语言·数据结构·c++·python·程序人生·算法·排序算法