【python_往飞书群或者话题下发送卡片和发送文件】

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()
    
    
    
相关推荐
jufeng13078 分钟前
【系列:手搓自主 AI Agent:Hermes 架构原理剖析 · 第 8 篇】
python·ai agent·配置系统
circuitsosk20 分钟前
跨境电商智能化实战:AI如何赋能客服自动回复、广告智能投放与供应链预测
大数据·人工智能·python·langchain·智能客服
2601_956319881 小时前
2026年零基础学量化:从看懂示例到写清条件和动作
人工智能·python
数智化管理手记1 小时前
海量数据如何沉淀有效数据资产?数据标准化治理方案如何搭建?
java·大数据·人工智能
过期的秋刀鱼!1 小时前
LangChain-D1-模型的工作流程
人工智能·python·langchain
萤火工厂目视化设计2 小时前
智能制造与企业文化目视化浪潮下:中小工厂的机遇与挑战
python·制造
cfm_29142 小时前
SpringAI + Ollama 本地大模型
java·开发语言·人工智能·语言模型
鲸采云SRM采购管理系统3 小时前
采购管理系统哪家好?2026最新测评对比
java·服务器·数据库
DeepVisionary3 小时前
从GPT-5.6 Sol的Ultrafast模式看推理速度竞赛:750 token/秒背后,Cerebras的晶圆级生意
python·自动化
leisoo80974 小时前
财报数据怎么排雷本地化Python构建财务异常预警系统
人工智能·python·算法