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失败")
相关推荐
想要狠赚笔的小燕12 小时前
vue项目的入口文件是什么 main.js还是index.html,他俩有啥区别
前端·javascript
Jiude12 小时前
AI面对真机调试也束手无策?我将方法论形成了一套SKILL 🛠️🤖
前端·后端·测试
Dxy123931021612 小时前
Python Tensor 向量入门:从零理解深度学习的“数据语言“
开发语言·python·深度学习
林森lsjs13 小时前
【日耕一题】3. 通过键盘输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
java·开发语言
之歆13 小时前
Day02_ES6+ 核心特性深度解析:现代 JavaScript 开发的基石
前端·javascript·es6
问心无愧051313 小时前
ctf show web入门71
android·前端·笔记
light blue bird13 小时前
支组汇总主子节点工序路径图表
前端·jvm·.net·桌面端·gdi绘图
夜勤月13 小时前
AQS 与 ThreadPoolExecutor 深度拆解:JDK 高并发底层设计精髓
android·java·开发语言
phltxy13 小时前
Spring AI 应用开发
java·人工智能·spring
码不停蹄的玄黓13 小时前
Arthas 线上问题排查实战:CPU过高、频繁GC
java