python实现企业微信应用发送文件
以下是Python实现企业微信应用发送文件的示例代码:
import requests
import json
# 设置企业微信应用的相关参数
corpid = 'your_corpid'
corpsecret = 'your_corpsecret'
agentid = 'your_agentid'
# 获取access_token
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
params = {'corpid': corpid, 'corpsecret': corpsecret}
response = requests.get(url, params=params)
access_token = json.loads(response.text)['access_token']
# 上传文件
url = 'https://qyapi.weixin.qq.com/cgi-bin/media/upload'
params = {'access_token': access_token, 'type': 'file'}
files = {'file': open('your_file_path', 'rb')}
response = requests.post(url, params=params, files=files)
media_id = json.loads(response.text)['media_id']
# 发送文件
url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send'
params = {'access_token': access_token}
data = {
'touser': '@all',
'agentid': agentid,
'msgtype': 'file',
'file': {
'media_id': media_id
}
}
response = requests.post(url, params=params, data=json.dumps(data))
print(response.text)
其中,需要替换的参数包括:
your_corpid:企业微信的corpid。your_corpsecret:企业微信应用的secret。your_agentid:企业微信应用的agentid。your_file_path:待发送的文件路径。
注意事项:
- 上传文件的接口要求文件大小不超过20MB。
- 发送文件的接口要求文件类型为doc、docx、xls、xlsx、ppt、pptx、pdf、txt、zip、rar。
- 发送文件的接口的
msgtype参数必须为file
原文地址: https://www.cveoy.top/t/topic/eEIh 著作权归作者所有。请勿转载和采集!