链接:更新已发送的消息卡片
注意: 卡片结构要写全,因为不可以局部更新
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失败")