当前位置: 首页 > news >正文

python企微发私信

import requests
import json

企业微信配置

CORP_ID = ‘ww9’ #企业号
CORP_SECRET = ‘Lc0XxbHS’ #管理企业–应用管理–程序号
AGENT_ID = ‘100003’ #管理企业–应用管理–程序号
TO_USER = ‘用户账号’ #管理企业–账号–想发的账号

获取access_token

def get_access_token():
url = f’https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={CORP_ID}&corpsecret={CORP_SECRET}’
resp = requests.get(url)
return resp.json().get(‘access_token’)

上传文件(临时素材,有效期为3天)

def upload_file(access_token, file_path):
url = f’https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={access_token}&type=file’
files = {‘media’: open(file_path, ‘rb’)}
resp = requests.post(url, files=files)
return resp.json().get(‘media_id’)

发送文件消息

def send_file_message(access_token, media_id, to_user):
url = f’https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}’
data = {
“touser”: to_user,
“msgtype”: “file”,
“agentid”: AGENT_ID,
“file”: {
“media_id”: media_id
},
“safe”: 0
}
resp = requests.post(url, data=json.dumps(data, ensure_ascii=False).encode(‘utf-8’))
return resp.json()

主流程

if name == ‘main’:
token = get_access_token()
if not token:
print(“获取access_token失败”)
else:
media_id = upload_file(token, ‘C:\Users\Administrator\Downloads\111.xlsx’) # 替换为实际文件路径

    if media_id:result = send_file_message(token, media_id, TO_USER)print(result)else:print("文件上传失败")media_id2 = upload_file(token, 'C:\\Users\\Administrator\\Downloads\\222.xlsx')  # 替换为实际文件路径if media_id:result = send_file_message(token, media_id2, TO_USER)print(result)else:print("文件上传失败")media_id3 = upload_file(token, 'C:\\Users\\Administrator\\Downloads\\333.xlsx')  # 替换为实际文件路径if media_id:result = send_file_message(token, media_id3, TO_USER)print(result)else:print("文件上传失败")media_id4 = upload_file(token, 'C:\\Users\\Administrator\\Downloads\\444.xlsx')  # 替换为实际文件路径if media_id:result = send_file_message(token, media_id4, TO_USER)print(result)else:print("文件上传失败")

使用pyarmor对python项目进行licence加密

pip install pyarmor

pyarmor gen -e “2025-07-02T17:20:00” hello.py

目录多了个文件夹dist
pyarmor gen main.py --pack onefile -O dist01

http://www.dtcms.com/a/347186.html

相关文章:

  • Text2API与Text2SQL深度对比:自然语言驱动的数据交互革命
  • 【40页PPT】数据安全动态数据脱敏解决方案(附下载方式)
  • C/C++ 头文件命名约定
  • stack,queue以及deque的介绍
  • 【Java学习笔记】18.反射与注解的应用
  • [e3nn] 模型部署 | TorchScript JIT | `@compile_mode`装饰器 | Cython
  • TypeScript的构造函数constructor用法理解
  • 深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)第四章知识点问答补充及重新排版
  • 离线优先与冲突解决:ABP vNext + PWA 的边缘同步
  • SQL Server更改日志模式:操作指南与最佳实践!
  • 使用 Certbot 申请 Apache 证书配置棘手问题
  • UAD详解
  • 分库分表系列-核心内容
  • 知识蒸馏 Knowledge Distillation 概率链式法则(Probability Chain Rule)
  • Class42时序模型
  • 深度学习开篇
  • 【通俗易懂】TypeScript 的类型守卫 (Type Guards)作用理解
  • iperf2 vs iperf3:UDP 发包逻辑差异与常见问题
  • [新启航]白光干涉仪与激光干涉仪的区别及应用解析
  • ubuntu 新登录修改root密码
  • 【攻防世界】Web_php_include
  • 力扣热题之动态规划
  • CryptSIPVerifyIndirectData函数分析
  • 鸿蒙开发进阶(HarmonyOS)
  • STM32 外设驱动模块八:红外反射式光电模块
  • 【大语言模型 15】因果掩码与注意力掩码实现:深度学习中的信息流控制艺术
  • 2-5.Python 编码基础 - 键盘输入
  • 2025钉钉十周年新品发布会,新品 “蕨”命名,到底是什么?
  • vue3 - 组件间的传值
  • nodejs和vue安装步骤记录