企業微信機器人使用python上傳文件並發送示例


def wx_post(file):
    id_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=機器人key&type=file'  # 上傳文件接口地址
    data = {'file': open(file, 'rb')}  # post jason
    response = requests.post(url=id_url, files=data)  # post 請求上傳文件
    json_res = response.json()  # 返回轉為json
    media_id = json_res['media_id']  # 提取返回ID
    wx_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=機器人key'  # 發送消息接口地址
    data = {"msgtype": "file", "file": {"media_id": media_id}}  # post json
    r = requests.post(url=wx_url, json=data)  # post請求消息
    return r  # 返回請求狀態


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM