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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容