Skip to content

Commit

Permalink
添加企业微信Webhook发送图片和文件
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyMerlin committed May 10, 2024
1 parent 88b69b9 commit 92ffaea
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ANotify/Nfeishu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!"))
Expand Down
84 changes: 82 additions & 2 deletions ANotify/Nwecom.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import requests
from enum import Enum
import json
import base64
import hashlib


class MediaType(Enum):
Expand Down Expand Up @@ -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
Expand All @@ -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")
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
```

### 飞书
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='anotify',
version='0.0.8',
version='0.0.9',
packages=find_packages(),
install_requires=[
'requests>=2.15.1',
Expand Down

0 comments on commit 92ffaea

Please sign in to comment.