python_往飞书群或者话题下发送卡片和发送文件
python
import requests
import json
from requests_toolbelt import MultipartEncoder
def get_tenant_access_token():
"""
步骤一:获取 tenant_access_token
这是调用飞书服务端 API 的全局唯一凭证
"""
url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"
headers = {
"Content-Type": "application/json"
}
payload = {
"app_id": app_id,
"app_secret": app_secret
}
response = requests.post(url, headers=headers, data=json.dumps(payload))
result = response.json()
if result.get("code") == 0:
return result["tenant_access_token"]
else:
raise Exception(f"获取 Token 失败: {result}")
def send_card_message():
"""
步骤二:发送卡片消息
:param receive_open_id: 接收者的 Open ID
:param card_template_id: 在卡片搭建平台创建的卡片模板 ID
"""
# 构建请求 URL
url = "https://open.feishu.cn/open-apis/im/v1/messages"
# 查询参数:指定 ID 类型为 open_id
params = {
"receive_id_type": "chat_id"
}
# 请求头
headers = {
"Authorization": f"Bearer {tenant_access_token}",
"Content-Type": "application/json"
}
# 请求体:发送模板卡片
data = {
"receive_id": chat_id,
"msg_type": "interactive", # 卡片消息类型固定为 interactive
"content": json.dumps({
"type": "template",
"data": {
"template_id": card_template_id,
# 如果你的卡片绑定了变量,需要在这里传入 template_variable 字段
"template_variable": {
"submit_time": submit_time,
"rpa_coach_open_id": rpa_coach_open_id,
"customer_success_open_id": customer_success_open_id,
"record_id_in_aw": record_id_in_aw,
"enterprise_name": enterprise_name,
"start_and_end_time": start_and_end_time
}
}
})
}
# 发送 POST 请求
response = requests.post(url, params=params, headers=headers, data=json.dumps(data))
if response.status_code == 200:
return response.json()["data"]["message_id"]
else:
raise Exception(f"请求失败: {response.status_code} - {response.text}")
def upload():
url = "https://open.feishu.cn/open-apis/im/v1/files"
form = {'file_type': 'stream',
'file_name': file_name,
'file': (file_name, open(file_path, 'rb'), 'text/plain')}
multi_form = MultipartEncoder(form)
headers = {
'Authorization': f'Bearer {tenant_access_token}',
}
headers['Content-Type'] = multi_form.content_type
response = requests.request("POST", url, headers=headers, data=multi_form)
if response.status_code == 200:
return response.json()['data']['file_key']
else:
raise Exception(f"文件上传失败: {response.status_code} - {response.text}")
# 在指定话题中回复文件
def reply_feishu_file_in_thread():
# 接口地址与查询参数
url = f"https://open.feishu.cn/open-apis/im/v1/messages/{parent_message_id}/reply"
# 3. 请求头
headers = {
"Authorization": f"Bearer {tenant_access_token}",
"Content-Type": "application/json; charset=utf-8"
}
# 4. 请求体
body = {
"msg_type": "file",
"content": json.dumps({"file_key": file_key}),
"reply_in_thread": True
}
# 发送请求
response = requests.post(url, headers=headers, json=body)
print("响应结果:", response.json())
# 在指定话题中回复卡片
def reply_feishu_card_in_thread():
# 接口地址与查询参数
url = f"https://open.feishu.cn/open-apis/im/v1/messages/{parent_message_id}/reply"
# 3. 请求头
headers = {
"Authorization": f"Bearer {tenant_access_token}",
"Content-Type": "application/json; charset=utf-8"
}
# 4. 构建请求体
body = {
"msg_type": "interactive",
"content": json.dumps({
"type": "template",
"data": {
"template_id": card_template_id,
# 如果你的卡片绑定了变量,需要在这里传入 template_variable 字段
"template_variable": {
"submit_time": submit_time,
"rpa_coach_open_id": rpa_coach_open_id,
"customer_success_open_id": customer_success_open_id,
"record_id_in_aw": record_id_in_aw,
"enterprise_name": enterprise_name,
"start_and_end_time": start_and_end_time
}
}
}),
"reply_in_thread": True
}
# 5. 发送 POST 请求
response = requests.post(url, headers=headers, json=body)
print("响应结果:", response.json())
# --- 执行代码 ---
if __name__ == "__main__":
# 1. 配置你的应用凭证 (请替换为你的实际 ID 和 Secret)
app_id = "XXX"
app_secret = "XXX"
# 替换为实际的接收者ID 和卡片 ID
chat_id = "XXX"
card_template_id = "XXX"
#卡片里面的参数
submit_time = "2024-06-01 09:00:00"
rpa_coach_open_id= "XXX" # 替换为实际的 RPA 教练 Open ID
customer_success_open_id="XXX" # 替换为实际的客户成功经理 Open ID
record_id_in_aw="1234567890"
enterprise_name="飞书科技有限公司"
start_and_end_time="2024-06-01 10:00:00 ~ 2024-06-01 11:00:00"
#文件信息
file_name = "药业.md"
file_path = "D:/Desktop/药业.md"
# 获取凭证
tenant_access_token = get_tenant_access_token()
print("获取到的 tenant_access_token:", tenant_access_token)
file_key=upload()
#消息ID
parent_message_id = "XXX"
if parent_message_id == "":
parent_message_id=send_card_message() #先发送卡片消息
reply_feishu_file_in_thread() #在上面的卡片消息中回复文件
else:
reply_feishu_card_in_thread()
reply_feishu_file_in_thread()