import os import django from sixGuoDjango.settings import BASE_DIR from wechat_token.token工具包 import sixToken os.environ.setdefault(key="DJANGO_SETTINGS_MODULE", value=f"{BASE_DIR.name}.settings") django.setup() from pathlib import Path import pydash import requests from django.conf import settings class wecom消息推送: pass @classmethod def 上传图片文件(cls): uri = '' filePath = Path(settings.BASE_DIR).joinpath("django_static_directory/media/app.png") data = requests.post(url=uri, files={"media": open(file="文件路径", mode="rb", )}) @classmethod def 欢迎语类型IMG(cls): pass IMG = { "msgtype": "link", "link": { "title": "消息标题", "picurl": "https://example.pic.com/path", "desc": "消息描述", "url": "https://example.link.com/path" } } return IMG @classmethod def 欢迎语类型小程序(cls): 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 def 欢迎语类型LINK(cls): """ 欢迎语类型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 def 欢迎语类型VIDEO(cls): pass VIDEO = { "msgtype": "video", "video": { "media_id": "MEDIA_ID" } } return VIDEO @classmethod def 创建企业群发(cls): pass wecom_token = sixToken.get_wecom_token() 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", }, }, ], } data = requests.post(url=url, json=json_data, params={"access_token": wecom_token, }, ) print(data.json()) @classmethod def 提醒成员群发(cls): """ 提醒成员群发 """ pass @classmethod def 停止企业群发(cls): pass @classmethod def 发送新客户欢迎语(cls, CALLBACK_CODE: str): """发送客户添加好友时的 欢迎语 """ wecom_token = sixToken.get_wecom_token() url = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/send_welcome_msg?access_token={wecom_token}" miniprogram = cls.欢迎语类型小程序() json_data = { "welcome_code": CALLBACK_CODE, "text": { "content": """ 🎉恭喜!您已成功解锁 给【好友发红包】特权!🔓 🚀更多惊喜权益,开启您的收益之旅: 1️⃣ 双向红包💰:给好友发红包🧧,您也能获得现金奖励💸,分享快乐😊,双倍收获! 2️⃣ 加入赚佣金🤝:成为我们的一员👨‍👩‍👧‍👦,推荐心仪好物❤️,轻松赚取商品推荐分成📈。 3️⃣ 超越赢未来🏆:与我们并肩前行👣,共享海量商品🛍️、优质服务🛠️与庞大流量🌊,共创价值!✨ """, }, } data = requests.post(url=url, json=json_data) print("客户欢迎语发送成功了:", data.json()) errcode = pydash.get(obj=data.json(), path="errcode") if errcode == 41051: pass return data.json().get("errmsg") class wecom获取企业的全部群发记录: """ 企业跟第三方应用可通过该接口获取群发给客户的消息和群发到客户群的消息 1. 群发助手和客户群群发有以下两种类型 2. 企业发表 1. 管理员或者业务负责人创建内容,成员确认后,即可发送给客户或者客户群 3. 个人发表 1. 成员自己创建的内容,可直接发送给客户或客户群 :return: :rtype: """ @classmethod def 获取群发记录列表(cls): """ 企业和第三方应用可通过此接口获取企业与成员的群发记录。 :return: :rtype: """ wecom_token = sixToken.get_wecom_token() 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" } data = requests.post(url=url, json=请求数据示例).json() print(data) @classmethod def 获取群发成员发送任务列表(cls): """ :return: :rtype: """ wecom_token = sixToken.get_wecom_token() url = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_task?access_token={wecom_token}" 请求数据示例 = {"msgid": "msgGCAAAXtWyujaWJHDDGi0mACAAAA", "limit": 50, "cursor": "CURSOR"} data = requests.post(url=url, json=请求数据示例).json() print(data) @classmethod def 获取企业群发成员执行结果(cls): """ :return: :rtype: """ wecom_token = sixToken.get_wecom_token() 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" } data = requests.post(url=url, json=请求数据示例).json() print(data) class wecom入群欢迎语素材管理: """ 添加入群欢迎语素材 企业可通过此API向企业的入群欢迎语素材库中添加素材。每个企业的入群欢迎语素材库中,最多容纳100个素材。 """ @classmethod def 添加入群欢迎语素材(cls): wecom_token = sixToken.get_wecom_token() 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 } data = requests.post(url=url, json=请求数据示例).json() print(data) @classmethod def 编辑入群欢迎语素材(cls): wecom_token = sixToken.get_wecom_token() 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 } data = requests.post(url=uri, json=请求数据示例).json() print(data) @classmethod def 获取入群欢迎语素材(cls): wecom_token = sixToken.get_wecom_token() uri = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/get?access_token={wecom_token}" 请求数据示例 = { "template_id": "msgXXXXXXX" } data = requests.post(url=uri, json=请求数据示例).json() print(data) @classmethod def 删除入群欢迎语素材(cls): """ 删除入群欢迎语素材 """ wecom_token = sixToken.get_wecom_token() uri = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/del?access_token={wecom_token}" 请求数据示例 = { "template_id": "msgXXXXXXX", "agentid": 1000014 } data = requests.post(url=uri, json=请求数据示例).json() print(data) class uploadTempMedia上传附件资源: @classmethod def upload_file(cls): """ 参数 必须 说明 access_token 是 调用接口凭证 media_type 是 媒体文件类型,分别有图片(image)、视频(video)、普通文件(file) attachment_type 是 附件类型,不同的附件类型用于不同的场景。1:朋友圈;2:商品图册 :return: :rtype: """ wecom_token = sixToken.get_wecom_token() uri = f"https://qyapi.weixin.qq.com/cgi-bin/media/upload_attachment?access_token={wecom_token}&media_type=image&attachment_type=1" 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", )} data = requests.post(url=uri, files=file) media_id = pydash.get(obj=data.json(), path="media_id") print(media_id) @classmethod def 上传临时素材(cls): wecom_token = sixToken.get_wecom_token() uri = f"https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={wecom_token}&type=image" 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) if __name__ == '__main__': pass uploadTempMedia上传附件资源.上传临时素材()