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

效果:

相关推荐
吾爱星辰43 分钟前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin
ChinaDragonDreamer43 分钟前
Kotlin:2.0.20 的新特性
android·开发语言·kotlin
FreakStudio44 分钟前
全网最适合入门的面向对象编程教程:56 Python字符串与序列化-正则表达式和re模块应用
python·单片机·嵌入式·面向对象·电子diy
IT良44 分钟前
c#增删改查 (数据操作的基础)
开发语言·c#
丶21361 小时前
【CUDA】【PyTorch】安装 PyTorch 与 CUDA 11.7 的详细步骤
人工智能·pytorch·python
Kalika0-02 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
_.Switch2 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
代码雕刻家2 小时前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
一个闪现必杀技2 小时前
Python入门--函数
开发语言·python·青少年编程·pycharm
Fan_web2 小时前
jQuery——事件委托
开发语言·前端·javascript·css·jquery