python_【更新已发送的消息卡片】

链接:更新已发送的消息卡片

注意: 卡片结构要写全,因为不可以局部更新

python 复制代码
import requests
import json

def get_tenant_access_token(app_id, app_secret):
    """获取 tenant_access_token"""
    url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"
    resp = requests.post(url, json={"app_id": app_id, "app_secret": app_secret})
    result = resp.json()
    return result.get("tenant_access_token") if result.get("code") == 0 else None

def update_message_card(message_id, card_content, app_token):
    """
    更新已发送的消息卡片
    :param message_id: 消息ID
    :param card_content: 卡片内容(字典或JSON字符串)
    :param app_token: tenant_access_token
    """
    url = f"https://open.feishu.cn/open-apis/im/v1/messages/{message_id}"
    headers = {
        "Authorization": f"Bearer {app_token}",
        "Content-Type": "application/json; charset=utf-8"
    }
    # 转换卡片内容为JSON字符串
    if isinstance(card_content, dict):
        card_content = json.dumps(card_content, ensure_ascii=False)
    
    body = {"content": card_content}
    response = requests.patch(url, json=body, headers=headers)
    return response.json()

# 使用示例
if __name__ == "__main__":
    APP_ID = "XXX"
    APP_SECRET = "XXX"
    
    token = get_tenant_access_token(APP_ID, APP_SECRET)
    if token:
        # 卡片内容(完整的新卡片)
        card_json = {
            "schema": "2.0",  
            "config": {
                "update_multi": True  # 必须显式设置为True
            },
            "body": {
                "elements": [
                    {
                        "tag": "button",
                        "element_id": "status",
                        "text": {
                            "tag": "plain_text",
                            "content": "已处理"
                        },
                        "type": "primary",
                        "disabled": True
                    }
                ]
            }
        }
        
        result = update_message_card(
            message_id="XXX",
            card_content=card_json,
            app_token=token
        )
        print(result)
    else:
        print("获取token失败")
相关推荐
2601_9628857224 分钟前
如何用 Python 计算 TRIX 三重指数平滑均线指标?
开发语言·python
还卿一钵无情泪44 分钟前
Unsloth 微调 构建自己的大模型 没有GPU也能微调
linux·开发语言·人工智能·python·大模型·nlp·unsloth
小π军2 小时前
操作系统面试题
java·linux·服务器
倔强的石头1062 小时前
应用账号最小权限实践:读写账号、报表账号、运维账号分层
java·运维·开发语言
Shaoshing3 小时前
ThreadLocal
java·jvm·threadlocal
用户033307413953 小时前
TypeScript using 与 AsyncDisposableStack:在真实服务端代码中协调多资源清理
前端
写后端的胖头鱼3 小时前
一文详解CAS
java·jvm·spring·cas·乐观锁·自旋
daisychey3 小时前
用 Playwright 做多平台内容分发,我踩过的 8 个坑
前端
鱼宵3 小时前
Spring AI 提示词模板:{变量} 参数化 + few-shot,一条提示词反复用
java·人工智能·spring·few-shot·提示词工程·springai