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

效果:
在这里插入图片描述

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-23 12:06:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-23 12:06:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-23 12:06:01       82 阅读
  4. Python语言-面向对象

    2024-03-23 12:06:01       91 阅读

热门阅读

  1. SAP-FI配置与业务解析之代发(客户)销售作业

    2024-03-23 12:06:01       41 阅读
  2. 将uint8_t数组转成uint32_t

    2024-03-23 12:06:01       46 阅读
  3. C++继承

    2024-03-23 12:06:01       41 阅读
  4. html5&css&js代码 038 列表

    2024-03-23 12:06:01       44 阅读
  5. Ubuntu下轻松搭建Wordpress:舞动Docker的魔法

    2024-03-23 12:06:01       38 阅读
  6. 25.2 微服务Dubbo

    2024-03-23 12:06:01       40 阅读
  7. 富格林:增强防范杜绝虚假暗箱

    2024-03-23 12:06:01       41 阅读
  8. InnoDB存储引擎的工作原理

    2024-03-23 12:06:01       42 阅读