python企微发私信
media_id = upload_file(token, ‘C:\Users\Administrator\Downloads\111.xlsx’)# 替换为实际文件路径。CORP_SECRET = ‘Lc0XxbHS’--管理企业–应用管理–程序号。AGENT_ID = ‘100003’—管理企业–应用管理–程序号。TO_USER = ‘用户账号’--管理企业–账号–想发的账号。CORP_ID
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
更多推荐
所有评论(0)