diff --git a/ANotify/Nfeishu.py b/ANotify/Nfeishu.py index f16fc2c..0c4b7da 100644 --- a/ANotify/Nfeishu.py +++ b/ANotify/Nfeishu.py @@ -162,7 +162,7 @@ def send_msg(self, msg): CHAT_ID = '' feishu = FeishuNotify(appid=APPID, appsecret=APPSECRET) - # print(feishu.send_file(ReceiverType.OPEN_ID, OPEN_ID, "E:/Desktop/gpt/Cursor_Demo/test.png")) + # print(feishu.send_file(ReceiverType.OPEN_ID, OPEN_ID, "test.png")) # print(feishu.send_msg(ReceiverType.OPEN_ID, OPEN_ID, "Hello World!")) # print(feishu.send_msg(ReceiverType.UINION_ID, UNION_ID, "Hello World!")) # print(feishu.send_msg(ReceiverType.USER_ID, USER_ID, "Hello World!")) diff --git a/ANotify/Nwecom.py b/ANotify/Nwecom.py index 83cc0d9..1daa316 100644 --- a/ANotify/Nwecom.py +++ b/ANotify/Nwecom.py @@ -1,6 +1,8 @@ import requests from enum import Enum import json +import base64 +import hashlib class MediaType(Enum): @@ -166,6 +168,78 @@ def send_template_card_multiple_interaction(self): resp.raise_for_status() return resp.json() +class WxWebhookNotify: + def __init__(self, webhook_url): + self.webhook_url = webhook_url + + def send_msg(self, msg): + url = self.webhook_url + playload = { + "msgtype": "text", + "text": { + "content": msg, + # "mentioned_list":["@all"], + # "mentioned_mobile_list":["@all"] + } + } + + res = requests.post(url, data=json.dumps(playload)) + return res.json() + + def send_msg_markdown(self, msg): + url = self.webhook_url + playload = { + "msgtype": "markdown", + "markdown": { + "content": msg, + # "mentioned_list":["@all"], + # "mentioned_mobile_list":["@all"] + } + } + + res = requests.post(url, data=json.dumps(playload)) + return res.json() + + def send_img(self, img_path): + url = self.webhook_url + img_base64, img_md5 = self.get_img_info(img_path) + playload = { + "msgtype": "image", + "image": { + "base64": img_base64, + "md5": img_md5 + } + } + + res = requests.post(url, data=json.dumps(playload)) + return res.json() + def send_file(self, file_path): + url = self.webhook_url + playload = { + "msgtype": "file", + "file": { + "media_id": self.upload_media(file_path) + } + } + + res = requests.post(url, data=json.dumps(playload)) + return res.json() + + def upload_media(self, file_path): + key = self.webhook_url.split("key=")[1] + url = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key={key}&type=file" + + files = {'file': open(file_path, 'rb')} + result = requests.post(url, files=files) + response = json.loads(result.text) + return response['media_id'] + + def get_img_info(self, img_path): + with open(img_path, 'rb') as f: + img = f.read() + md5 = hashlib.md5(img).hexdigest() + img_base64 = base64.b64encode(img).decode('utf-8') + return img_base64, md5 if __name__ == "__main__": # 企业ID @@ -175,5 +249,11 @@ def send_template_card_multiple_interaction(self): # 应用ID AgentId = '' - wn = WxNotify(corpid=CORPID, corpsecret=CORPSECRET, agentid=AgentId) - print(wn.send_msg("test message")) + # wn = WxNotify(corpid=CORPID, corpsecret=CORPSECRET, agentid=AgentId) + # print(wn.send_msg("test message")) + + wn_webhook = WxWebhookNotify("") + # wn_webhook.send_file("E:/Desktop/gpt/Cursor_Demo/test.png") + # wn_webhook.send_msg("Hello") + # print(wn_webhook.send_msg_markdown("**Hello**\n- test1\n- [ANotify](https://www.baidu.com)")) + # wn_webhook.send_img("E:/Desktop/gpt/Cursor_Demo/test.png") diff --git a/README.md b/README.md index be3d258..2e5e122 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,14 @@ wn.send_msg("test message") wn.send_text_card("test title", "test content", "https://www.example.com") wn.send_file("./test.txt") wn.send_img("./test.png") + +# Webhook +WEB_HOOK = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxx" +wn_webhook = Nwecom.WxWebhookNotify(WEB_HOOK) +wn_webhook.send_msg("Hello") +wn_webhook.send_msg_markdown("**Hello**\n- test1\n- [ANotify](https://github.com/TommyMerlin/ANotify)") +wn_webhook.send_img("E:/Desktop/gpt/Cursor_Demo/test.png") +wn_webhook.send_file("test.png") ``` ### 飞书 diff --git a/setup.py b/setup.py index 8eee35b..b596572 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='anotify', - version='0.0.8', + version='0.0.9', packages=find_packages(), install_requires=[ 'requests>=2.15.1',