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

效果:

相关推荐
Jonathan Star27 分钟前
JavaScript 中,原型链的**最顶端(终极原型)只有一个——`Object.prototype`
开发语言·javascript·原型模式
普通网友33 分钟前
C++中的组合模式
开发语言·c++·算法
q***615034 分钟前
PHP进阶-在Ubuntu上搭建LAMP环境教程
开发语言·ubuntu·php
Dneccc36 分钟前
Qt5配置MSVC2017
开发语言·qt
江公望38 分钟前
Qt QByteArray类型,10分钟讲清楚
开发语言·c++·qt
小灰灰搞电子38 分钟前
Qt Sensors 传感器框架详解
开发语言·qt
LNN202243 分钟前
Qt 5.8 中的 Qt Test:轻松实现自动化测试
开发语言·qt
2501_9411114643 分钟前
C++中的组合模式变体
开发语言·c++·算法
啵啵鱼爱吃小猫咪1 小时前
机器人具身智能汇总
开发语言
想唱rap1 小时前
Linux下进程的状态和优先级
linux·运维·服务器·开发语言·数据结构·算法