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失败")
相关推荐
蜡笔小谢1 小时前
DeepSeek Harness Java 实战笔记(五)|插件、MCP 与 Skill
java·笔记·ai·agent·deepseek·harness
2601_967979122 小时前
初学者必知的Python中优雅的用法
python·枚举·初学者·优雅用法·字典解析
FRing032 小时前
vue2干货总结12(loading效果、全局前置导航守卫、搜索历史管理)
开发语言·前端·javascript·vue.js·vue2
YIAN2 小时前
我踩完 DeepAgents 记忆模块的坑,才搞懂长期记忆和摘要不是一回事
前端·javascript·agent
唐老板3 小时前
Spring AI 2.0 只支持 Boot 4
java
AI创界者4 小时前
Python 进阶实战(五十八章):配置热更新、优雅停机排空与高并发压测方案
人工智能·python·aigc
map_3d_vis4 小时前
百度地图 JSAPI 4.0 是什么:版本说明与能力概览
前端·javascript·webgl·地图·百度地图
码云之上4 小时前
微信小程序设备指纹实践,删了重装还能认出这台手机吗?
前端·安全·微信小程序
JavaGuide5 小时前
刚刚,GLM-5.4 正式发布,实测来了:看图用 Blender 建凉亭、做技术视频,再画一辆真能传动的自行车
前端·后端
琳少_6 小时前
java程序设计笔记
java·开发语言·数据库·笔记