【python_往企业微信群中发送文件】

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()

效果:

相关推荐
查古穆10 分钟前
python进阶-Pydantic模型
开发语言·python
沐知全栈开发12 分钟前
Bootstrap4 导航栏
开发语言
kyriewen1115 分钟前
异步编程:从“回调地狱”到“async/await”的救赎之路
开发语言·前端·javascript·chrome·typescript·ecmascript·html5
AI+程序员在路上18 分钟前
嵌入式软件技术大全
linux·开发语言·arm开发·单片机
吴声子夜歌18 分钟前
JavaScript——数据类型
开发语言·javascript·ecmascript
佳木逢钺26 分钟前
PyQt界面美化系统高级工具库:打造现代化桌面应用的完整指南
python·pyqt
2401_8796938738 分钟前
C++中的观察者模式实战
开发语言·c++·算法
工頁光軍1 小时前
基于Python的Milvus完整使用案例
开发语言·python·milvus
Csvn1 小时前
特殊方法与运算符重载
python
wregjru1 小时前
【网络】8.五种 I/O 模型与多路转接详解
开发语言·php