python_往企业微信群中发送文件
这个是用企业微信群机器人的功能,没有用到后台应用。群机器人
python
#-*- coding:utf-8-*
import requests
#类型:voice,file
file_type="file"
file_path="D:\desktop\不过.jpg"
webhookkey="xxxx"
#1.上传临时素材
def get_media_id(path):
url=f"https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key={webhookkey}&type={file_type}"
data={file_type:open(path,"rb")}
response = requests.post(url=url,files=data)
print(response.json())
return response.json()["media_id"]
# get_media_id(file_path)
#2.推送消息
def send_message():
url=f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={webhookkey}"
headers={
"Content-Type":"application/x-www-form-urlencoded"
}
params={
"msgtype": "file",
"file": {
"media_id": get_media_id(file_path)
}
}
response = requests.post(url=url,headers=headers,json=params)
print(response.json())
return response.json()
send_message()
效果: