跳转至

callback

feishu.cards.callback

CardAction

card.action.trigger 事件的类型化只读视图。

将卡片交互回调事件包装为带类型属性的对象,便于读取触发者、回传值、表单值以及 更新卡片所需的 token 等字段。可接收完整事件载荷、已解包的事件节点,或暴露 .body 映射的事件对象。通常通过 feishu.cards.callback.parse_action 构造。

飞书文档

卡片回传交互

示例:

Python Console Session
>>> event = {
...     "event": {
...         "operator": {"open_id": "ou_1", "user_id": "u1", "union_id": "on_1"},
...         "token": "c-update-token",
...         "action": {"value": {"decision": "approve"}, "tag": "button", "name": "approve"},
...         "context": {"open_message_id": "om_1", "open_chat_id": "oc_1"},
...     }
... }
>>> action = CardAction(event)
>>> action.value
{'decision': 'approve'}
>>> action.open_id, action.message_id, action.token
('ou_1', 'om_1', 'c-update-token')
源代码位于: feishu/cards/callback.py
Python
class CardAction:
    r"""
    `card.action.trigger` 事件的类型化只读视图。

    将卡片交互回调事件包装为带类型属性的对象,便于读取触发者、回传值、表单值以及
    更新卡片所需的 `token` 等字段。可接收完整事件载荷、已解包的事件节点,或暴露
    `.body` 映射的事件对象。通常通过 [feishu.cards.callback.parse_action][] 构造。

    飞书文档:
        [卡片回传交互](https://open.feishu.cn/document/uAjLw4CM/ukzMukzMukzM/feishu-cards/card-callback-communication)

    Examples:
        >>> event = {
        ...     "event": {
        ...         "operator": {"open_id": "ou_1", "user_id": "u1", "union_id": "on_1"},
        ...         "token": "c-update-token",
        ...         "action": {"value": {"decision": "approve"}, "tag": "button", "name": "approve"},
        ...         "context": {"open_message_id": "om_1", "open_chat_id": "oc_1"},
        ...     }
        ... }
        >>> action = CardAction(event)
        >>> action.value
        {'decision': 'approve'}
        >>> action.open_id, action.message_id, action.token
        ('ou_1', 'om_1', 'c-update-token')
    """

    __slots__ = ("_node", "_action", "_operator", "_context", "raw")

    def __init__(self, event: Any) -> None:
        self.raw = event
        self._node = _node_from(event)
        action = self._node.get("action")
        self._action: Mapping[str, Any] = action if isinstance(action, Mapping) else {}
        operator = self._node.get("operator")
        self._operator: Mapping[str, Any] = operator if isinstance(operator, Mapping) else {}
        context = self._node.get("context")
        self._context: Mapping[str, Any] = context if isinstance(context, Mapping) else {}

    @property
    def value(self) -> dict[str, Any]:
        r"""按钮等组件回传的业务数据;缺失时返回空字典。"""
        v = self._action.get("value")
        return dict(v) if isinstance(v, Mapping) else {}

    @property
    def operator(self) -> dict[str, Any]:
        r"""触发者的 ID 集合,仅含存在的 `open_id`、`user_id`、`union_id` 字段。"""
        out: dict[str, Any] = {}
        for key in ("open_id", "user_id", "union_id"):
            if key in self._operator:
                out[key] = self._operator[key]
        return out

    @property
    def open_id(self) -> str | None:
        r"""触发者的 `open_id`;缺失时返回 `None`。"""
        return self._operator.get("open_id")

    @property
    def user_id(self) -> str | None:
        r"""触发者的 `user_id`;缺失时返回 `None`。"""
        return self._operator.get("user_id")

    @property
    def union_id(self) -> str | None:
        r"""触发者的 `union_id`;缺失时返回 `None`。"""
        return self._operator.get("union_id")

    @property
    def token(self) -> str | None:
        r"""
        更新卡片所需的凭证(有效期 30 分钟、最多更新 2 次),并非 Webhook 校验 token。

        飞书文档:
            [延时更新卡片](https://open.feishu.cn/document/uAjLw4CM/ukzMukzMukzM/feishu-cards/card-callback-communication/delay-update-card)
        """
        return self._node.get("token")

    @property
    def message_id(self) -> str | None:
        r"""承载该卡片的消息 ID(`open_message_id`);缺失时返回 `None`。"""
        return self._context.get("open_message_id")

    @property
    def chat_id(self) -> str | None:
        r"""承载该卡片的会话 ID(`open_chat_id`);缺失时返回 `None`。"""
        return self._context.get("open_chat_id")

    @property
    def tag(self) -> str | None:
        r"""触发交互的组件标签,如 `button`;缺失时返回 `None`。"""
        return self._action.get("tag")

    @property
    def name(self) -> str | None:
        r"""触发交互的组件名称;缺失时返回 `None`。"""
        return self._action.get("name")

    @property
    def form_value(self) -> dict[str, Any]:
        r"""表单容器提交时回传的各字段取值;缺失时返回空字典。"""
        fv = self._action.get("form_value")
        return dict(fv) if isinstance(fv, Mapping) else {}

value property

Python
value: dict[str, Any]

按钮等组件回传的业务数据;缺失时返回空字典。

operator property

Python
operator: dict[str, Any]

触发者的 ID 集合,仅含存在的 open_iduser_idunion_id 字段。

open_id property

Python
open_id: str | None

触发者的 open_id;缺失时返回 None

user_id property

Python
user_id: str | None

触发者的 user_id;缺失时返回 None

union_id property

Python
union_id: str | None

触发者的 union_id;缺失时返回 None

token property

Python
token: str | None

更新卡片所需的凭证(有效期 30 分钟、最多更新 2 次),并非 Webhook 校验 token。

飞书文档

延时更新卡片

message_id property

Python
message_id: str | None

承载该卡片的消息 ID(open_message_id);缺失时返回 None

chat_id property

Python
chat_id: str | None

承载该卡片的会话 ID(open_chat_id);缺失时返回 None

tag property

Python
tag: str | None

触发交互的组件标签,如 button;缺失时返回 None

name property

Python
name: str | None

触发交互的组件名称;缺失时返回 None

form_value property

Python
form_value: dict[str, Any]

表单容器提交时回传的各字段取值;缺失时返回空字典。

parse_action

Python
parse_action(event: Any) -> CardAction

card.action.trigger 事件解析为类型化的 feishu.cards.callback.CardAction 视图。

参数:

名称 类型 描述 默认

event

Any

卡片交互回调事件,可为完整事件载荷、已解包的事件节点,或暴露 .body 映射的事件对象。

必需

返回:

类型 描述
CardAction

包装该事件的 feishu.cards.callback.CardAction 实例。

引发:

类型 描述
TypeError

当无法从 event 中提取卡片交互节点时抛出。

飞书文档

卡片回传交互

示例:

Python Console Session
1
2
3
4
5
>>> action = parse_action({"event": {"action": {"value": {"decision": "approve"}}}})
>>> action.value
{'decision': 'approve'}
>>> parse_action({"event": {"action": {}}}).tag is None
True
源代码位于: feishu/cards/callback.py
Python
def parse_action(event: Any) -> CardAction:
    r"""
    将 `card.action.trigger` 事件解析为类型化的 [feishu.cards.callback.CardAction][] 视图。

    Args:
        event: 卡片交互回调事件,可为完整事件载荷、已解包的事件节点,或暴露 `.body` 映射的事件对象。

    Returns:
        包装该事件的 [feishu.cards.callback.CardAction][] 实例。

    Raises:
        TypeError: 当无法从 `event` 中提取卡片交互节点时抛出。

    飞书文档:
        [卡片回传交互](https://open.feishu.cn/document/uAjLw4CM/ukzMukzMukzM/feishu-cards/card-callback-communication)

    Examples:
        >>> action = parse_action({"event": {"action": {"value": {"decision": "approve"}}}})
        >>> action.value
        {'decision': 'approve'}
        >>> parse_action({"event": {"action": {}}}).tag is None
        True
    """
    return CardAction(event)