Files

355 lines
13 KiB
Python
Raw Permalink Normal View History

2026-08-02 14:07:04 +08:00
import os
import django
from sixGuoDjango.settings import BASE_DIR
from wechat_token.token工具包 import sixToken
2026-08-06 20:39:45 +08:00
os.environ.setdefault(key="DJANGO_SETTINGS_MODULE", value=f"{BASE_DIR.name}.settings")
django.setup()
2026-08-02 14:07:04 +08:00
from pathlib import Path
import pydash
import requests
from django.conf import settings
class wecom消息推送:
pass
@classmethod
2026-08-06 20:39:45 +08:00
def 上传图片文件(cls):
2026-08-02 14:07:04 +08:00
uri = ''
2026-08-06 20:39:45 +08:00
filePath = Path(settings.BASE_DIR).joinpath("django_static_directory/media/app.png")
data = requests.post(url=uri, files={"media": open(file="文件路径", mode="rb", )})
2026-08-02 14:07:04 +08:00
@classmethod
2026-08-06 20:39:45 +08:00
def 欢迎语类型IMG(cls):
2026-08-02 14:07:04 +08:00
pass
IMG = {
"msgtype": "link",
"link": {
"title": "消息标题",
"picurl": "https://example.pic.com/path",
"desc": "消息描述",
"url": "https://example.link.com/path"
}
}
return IMG
@classmethod
2026-08-06 20:39:45 +08:00
def 欢迎语类型小程序(cls):
2026-08-02 14:07:04 +08:00
pass
miniprogram = {
"msgtype": "miniprogram", # 附件类型,可选image、link、miniprogram或者video
"miniprogram": {
"title": "小程序领现金红包", # 是 小程序消息标题,最长为 64 字节
"pic_media_id": "3AIBtUrVvuc2qwuSjJSWK9AXyQF_omNpWJqz_4onX_shiGW9U6OsWauNImkKVwJsON0xkcnywtAQJnaXfgmCbIA",
# 小程序消息封面的mediaid,封面图建议尺寸为520*416
"appid": "wx3acee999f873005a",
"page": "/tabPage/Home/Home",
},
}
return miniprogram
@classmethod
2026-08-06 20:39:45 +08:00
def 欢迎语类型LINK(cls):
2026-08-02 14:07:04 +08:00
"""
欢迎语类型LINK
"""
IMG = {
"msgtype": "link", # 附件类型,可选image、link、miniprogram或者video
"image": {
"media_id": "MEDIA_ID", # 图片的media_id,可以通过素材管理接口获得
"pic_url": "http://p.qpic.cn/pic_wework/3474110808/7a6344sdadfwehe42060/0" # 图片的链接,仅可使用上传图片接口得到的链接
}
}
@classmethod
2026-08-06 20:39:45 +08:00
def 欢迎语类型VIDEO(cls):
2026-08-02 14:07:04 +08:00
pass
VIDEO = {
"msgtype": "video",
"video": {
"media_id": "MEDIA_ID"
}
}
return VIDEO
@classmethod
2026-08-06 20:39:45 +08:00
def 创建企业群发(cls):
2026-08-02 14:07:04 +08:00
pass
2026-08-06 20:39:45 +08:00
wecom_token = sixToken.get_wecom_token()
2026-08-02 14:07:04 +08:00
url = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_msg_template"
json_data = {
"chat_type": "single",
"external_userid": [
"wmj4zANAAAGlCZ2zNRhu1qzEMSBF0a9g", "wmj4zANAAA-lsKNm2U2U-EBCcVGuFGIA",
],
"attachments": [
{
"msgtype": "link",
"link": {
"title": "消息标题",
"desc": "消息描述",
"url": "https://www.sixgou.com/",
"picurl": "https://fastly.picsum.photos/id/82/200/200.jpg?hmac"
"=ATNAhTLN2dA0KmTzSE5D9XiPe3GMX8uwxpFlhU7U5OY",
},
},
],
}
2026-08-06 20:39:45 +08:00
data = requests.post(url=url, json=json_data, params={"access_token": wecom_token, }, )
print(data.json())
2026-08-02 14:07:04 +08:00
@classmethod
2026-08-06 20:39:45 +08:00
def 提醒成员群发(cls):
2026-08-02 14:07:04 +08:00
"""
提醒成员群发
"""
pass
@classmethod
2026-08-06 20:39:45 +08:00
def 停止企业群发(cls):
2026-08-02 14:07:04 +08:00
pass
@classmethod
2026-08-06 20:39:45 +08:00
def 发送新客户欢迎语(cls, CALLBACK_CODE: str):
"""发送客户添加好友时的 欢迎语 """
wecom_token = sixToken.get_wecom_token()
2026-08-02 14:07:04 +08:00
url = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/send_welcome_msg?access_token={wecom_token}"
2026-08-06 20:39:45 +08:00
miniprogram = cls.欢迎语类型小程序()
2026-08-02 14:07:04 +08:00
json_data = {
"welcome_code": CALLBACK_CODE,
"text": {
"content": """
🎉恭喜!您已成功解锁 给【好友发红包】特权!🔓
🚀更多惊喜权益,开启您的收益之旅:
1️⃣ 双向红包💰:给好友发红包🧧,您也能获得现金奖励💸,分享快乐😊,双倍收获!
2️⃣ 加入赚佣金🤝:成为我们的一员👨‍👩‍👧‍👦,推荐心仪好物❤️,轻松赚取商品推荐分成📈。
3️⃣ 超越赢未来🏆:与我们并肩前行👣,共享海量商品🛍️、优质服务🛠️与庞大流量🌊,共创价值!✨
""",
},
}
2026-08-06 20:39:45 +08:00
data = requests.post(url=url, json=json_data)
print("客户欢迎语发送成功了:", data.json())
errcode = pydash.get(obj=data.json(), path="errcode")
2026-08-02 14:07:04 +08:00
if errcode == 41051:
pass
2026-08-06 20:39:45 +08:00
return data.json().get("errmsg")
2026-08-02 14:07:04 +08:00
class wecom获取企业的全部群发记录:
"""
企业跟第三方应用可通过该接口获取群发给客户的消息和群发到客户群的消息
1. 群发助手和客户群群发有以下两种类型
2. 企业发表
1. 管理员或者业务负责人创建内容,成员确认后,即可发送给客户或者客户群
3. 个人发表
1. 成员自己创建的内容,可直接发送给客户或客户群
:return:
:rtype:
"""
@classmethod
2026-08-06 20:39:45 +08:00
def 获取群发记录列表(cls):
2026-08-02 14:07:04 +08:00
"""
企业和第三方应用可通过此接口获取企业与成员的群发记录。
:return:
:rtype:
"""
2026-08-06 20:39:45 +08:00
wecom_token = sixToken.get_wecom_token()
2026-08-02 14:07:04 +08:00
url = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_list_v2?access_token={wecom_token}"
请求数据示例 = {
"chat_type": "single",
"start_time": 1605171726,
"end_time": 1605172726,
"creator": "zhangshan",
"filter_type": 1,
"limit": 50,
"cursor": "CURSOR"
}
2026-08-06 20:39:45 +08:00
data = requests.post(url=url, json=请求数据示例).json()
print(data)
2026-08-02 14:07:04 +08:00
@classmethod
2026-08-06 20:39:45 +08:00
def 获取群发成员发送任务列表(cls):
2026-08-02 14:07:04 +08:00
"""
:return:
:rtype:
"""
2026-08-06 20:39:45 +08:00
wecom_token = sixToken.get_wecom_token()
2026-08-02 14:07:04 +08:00
url = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_task?access_token={wecom_token}"
2026-08-06 20:39:45 +08:00
请求数据示例 = {"msgid": "msgGCAAAXtWyujaWJHDDGi0mACAAAA", "limit": 50, "cursor": "CURSOR"}
data = requests.post(url=url, json=请求数据示例).json()
print(data)
2026-08-02 14:07:04 +08:00
@classmethod
2026-08-06 20:39:45 +08:00
def 获取企业群发成员执行结果(cls):
2026-08-02 14:07:04 +08:00
"""
:return:
:rtype:
"""
2026-08-06 20:39:45 +08:00
wecom_token = sixToken.get_wecom_token()
2026-08-02 14:07:04 +08:00
url = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_send_result?access_token={wecom_token}"
请求数据示例 = {
"msgid": "msgGCAAAXtWyujaWJHDDGi0mACAAAA",
"userid": "zhangsan ",
"limit": 50,
"cursor": "CURSOR"
}
2026-08-06 20:39:45 +08:00
data = requests.post(url=url, json=请求数据示例).json()
print(data)
2026-08-02 14:07:04 +08:00
class wecom入群欢迎语素材管理:
"""
添加入群欢迎语素材
企业可通过此API向企业的入群欢迎语素材库中添加素材。每个企业的入群欢迎语素材库中,最多容纳100个素材。
"""
@classmethod
2026-08-06 20:39:45 +08:00
def 添加入群欢迎语素材(cls):
wecom_token = sixToken.get_wecom_token()
2026-08-02 14:07:04 +08:00
url = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/add?access_token={wecom_token}"
请求数据示例 = {
"text": {
"content": "亲爱的%NICKNAME%用户,你好"
},
"image": {
"media_id": "MEDIA_ID",
"pic_url": "http://p.qpic.cn/pic_wework/3474110808/7a6344sdadfwehe42060/0"
},
"link": {
"title": "消息标题",
"picurl": "https://example.pic.com/path",
"desc": "消息描述",
"url": "https://example.link.com/path"
},
"miniprogram": {
"title": "消息标题",
"pic_media_id": "MEDIA_ID",
"appid": "wx8bd80126147dfAAA",
"page": "/path/index"
},
"file": {
"media_id": "1Yv-zXfHjSjU-7LH-GwtYqDGS-zz6w22KmWAT5COgP7o"
},
"video": {
"media_id": "MEDIA_ID"
},
"agentid": 1000014,
"notify": 1
}
2026-08-06 20:39:45 +08:00
data = requests.post(url=url, json=请求数据示例).json()
print(data)
2026-08-02 14:07:04 +08:00
@classmethod
2026-08-06 20:39:45 +08:00
def 编辑入群欢迎语素材(cls):
wecom_token = sixToken.get_wecom_token()
2026-08-02 14:07:04 +08:00
uri = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/edit?access_token={wecom_token}"
请求数据示例 = {
"template_id": "msgXXXXXXX",
"text": {
"content": "文本消息内容"
},
"image": {
"media_id": "MEDIA_ID",
"pic_url": "http://p.qpic.cn/pic_wework/3474110808/7a6344sdadfwehe42060/0"
},
"link": {
"title": "消息标题",
"picurl": "https://example.pic.com/path",
"desc": "消息描述",
"url": "https://example.link.com/path"
},
"miniprogram": {
"title": "消息标题",
"pic_media_id": "MEDIA_ID",
"appid": "wx8bd80126147df384",
"page": "/path/index"
},
"file": {
"media_id": "1Yv-zXfHjSjU-7LH-GwtYqDGS-zz6w22KmWAT5COgP7o"
},
"video": {
"media_id": "MEDIA_ID"
},
"agentid": 1000014
}
2026-08-06 20:39:45 +08:00
data = requests.post(url=uri, json=请求数据示例).json()
print(data)
2026-08-02 14:07:04 +08:00
@classmethod
2026-08-06 20:39:45 +08:00
def 获取入群欢迎语素材(cls):
wecom_token = sixToken.get_wecom_token()
2026-08-02 14:07:04 +08:00
uri = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/get?access_token={wecom_token}"
请求数据示例 = {
"template_id": "msgXXXXXXX"
}
2026-08-06 20:39:45 +08:00
data = requests.post(url=uri, json=请求数据示例).json()
print(data)
2026-08-02 14:07:04 +08:00
@classmethod
2026-08-06 20:39:45 +08:00
def 删除入群欢迎语素材(cls):
2026-08-02 14:07:04 +08:00
"""
删除入群欢迎语素材
"""
2026-08-06 20:39:45 +08:00
wecom_token = sixToken.get_wecom_token()
2026-08-02 14:07:04 +08:00
uri = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/del?access_token={wecom_token}"
请求数据示例 = {
"template_id": "msgXXXXXXX",
"agentid": 1000014
}
2026-08-06 20:39:45 +08:00
data = requests.post(url=uri, json=请求数据示例).json()
print(data)
2026-08-02 14:07:04 +08:00
class uploadTempMedia上传附件资源:
@classmethod
2026-08-06 20:39:45 +08:00
def upload_file(cls):
2026-08-02 14:07:04 +08:00
"""
参数 必须 说明
access_token 是 调用接口凭证
media_type 是 媒体文件类型,分别有图片(image)、视频(video)、普通文件(file)
attachment_type 是 附件类型,不同的附件类型用于不同的场景。1:朋友圈;2:商品图册
:return:
:rtype:
"""
2026-08-06 20:39:45 +08:00
wecom_token = sixToken.get_wecom_token()
2026-08-02 14:07:04 +08:00
uri = f"https://qyapi.weixin.qq.com/cgi-bin/media/upload_attachment?access_token={wecom_token}&media_type=image&attachment_type=1"
2026-08-06 20:39:45 +08:00
params = {"access_token": wecom_token, "media_type": "image", "attachment_type": "1"}
file_path = Path(settings.BASE_DIR).joinpath("django_环境/常用类/xiao小程序项目图片/ico运力管理系统.png")
print(file_path)
file = {"media": open(file=file_path, mode="rb", )}
2026-08-02 14:07:04 +08:00
2026-08-06 20:39:45 +08:00
data = requests.post(url=uri, files=file)
media_id = pydash.get(obj=data.json(), path="media_id")
print(media_id)
2026-08-02 14:07:04 +08:00
@classmethod
2026-08-06 20:39:45 +08:00
def 上传临时素材(cls):
wecom_token = sixToken.get_wecom_token()
2026-08-02 14:07:04 +08:00
uri = f"https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={wecom_token}&type=image"
2026-08-06 20:39:45 +08:00
file_path = Path(settings.BASE_DIR).joinpath("django_环境/常用类/xiao小程序项目图片/莫大帅分享主图.png")
file = {"media": open(file=file_path, mode="rb", )}
data = requests.post(url=uri, files=file)
media_id = pydash.get(obj=data.json(), path="media_id")
print(data.json())
print(media_id)
2026-08-02 14:07:04 +08:00
if __name__ == '__main__':
pass
2026-08-06 20:39:45 +08:00
uploadTempMedia上传附件资源.上传临时素材()