python调企业微信机器人API_发送文件

一、接口说明

发送.png
上传.png

二、企业机器人发送文件消息

代码:

# -*- coding:utf-8 -*-
import requests,os

#传入文件
def post_file(id_url,wx_url,file):
    data = {'file': open(file,'rb')}
    # 请求id_url(将文件上传微信临时平台),返回media_id
    # id_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=xxx&type=file'
    response = requests.post(url=id_url, files=data)
    json_res = response.json()
    media_id = json_res['media_id']

    data = {"msgtype": "file",
             "file": {"media_id": media_id}
            }
    result = requests.post(url=wx_url,json=data)
    return(result)

# 源文件
path_0 = os.path.dirname(__file__)
path_local = path_0 + '/ops/data.xlsx'

id_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=xxx&type=file'
wx_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx'
post_file(id_url, wx_url, path_local)

print('发送完成')

执行结果:

robot发送文件.png

你可能感兴趣的:(python调企业微信机器人API_发送文件)