From 98d78455af125f543fb8dbaef515ef5c3ce11c82 Mon Sep 17 00:00:00 2001
From: q629988171 <113960096@qq.com>
Date: Sun, 12 Sep 2021 12:18:22 +0800
Subject: [PATCH] Update README.md
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
将3个类似功能的函数写成一个功能函数(提高代码利用率)
---
README.md | 108 +++++++++++++++++++++++++-----------------------------
1 file changed, 50 insertions(+), 58 deletions(-)
diff --git a/README.md b/README.md
index 60b8b5f..499f13b 100644
--- a/README.md
+++ b/README.md
@@ -119,72 +119,61 @@ print_r( $ret );
PYTHON版:
```python
-import json,requests,base64
-def send_to_wecom(text,wecom_cid,wecom_aid,wecom_secret,wecom_touid='@all'):
+import base64
+import json
+
+import requests
+
+
+def send_to_wecom(wecom_mtype, wecom_content, wecom_cid, wecom_aid, wecom_secret, wecom_touid='@all'):
get_token_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={wecom_cid}&corpsecret={wecom_secret}"
response = requests.get(get_token_url).content
access_token = json.loads(response).get('access_token')
+
if access_token and len(access_token) > 0:
send_msg_url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}'
data = {
- "touser":wecom_touid,
- "agentid":wecom_aid,
- "msgtype":"text",
- "text":{
- "content":text
+ "touser": wecom_touid,
+ "agentid": wecom_aid,
+ "msgtype": "text",
+ "text": {
+ "content": wecom_content
},
- "duplicate_check_interval":600
+ "duplicate_check_interval": 600
}
- response = requests.post(send_msg_url,data=json.dumps(data)).content
- return response
- else:
- return False
-def send_to_wecom_image(base64_content,wecom_cid,wecom_aid,wecom_secret,wecom_touid='@all'):
- get_token_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={wecom_cid}&corpsecret={wecom_secret}"
- response = requests.get(get_token_url).content
- access_token = json.loads(response).get('access_token')
- if access_token and len(access_token) > 0:
- upload_url = f'https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={access_token}&type=image'
- upload_response = requests.post(upload_url, files={
- "picture": base64.b64decode(base64_content)
- }).json()
- if "media_id" in upload_response:
- media_id = upload_response['media_id']
+ if wecom_mtype == "text":
+ pass
+ elif wecom_mtype == "markdown":
+ data["msgtype"] = "markdown"
+ del data["text"]
+ data["markdown"] = {}
+ data["markdown"]["content"] = wecom_content
+ elif wecom_mtype == "image":
+ upload_url = f'https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={access_token}&type=image'
+ upload_response = requests.post(
+ upload_url,
+ files={
+ "picture": base64.b64decode(wecom_content)
+ }
+ ).json()
+ if "media_id" in upload_response:
+ media_id = upload_response['media_id']
+ else:
+ return False
+
+ data["msgtype"] = "image"
+ del data["text"]
+ data["image"] = {}
+ data["image"]["media_id"] = media_id
+
else:
return False
- send_msg_url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}'
- data = {
- "touser":wecom_touid,
- "agentid":wecom_aid,
- "msgtype":"image",
- "image":{
- "media_id": media_id
- },
- "duplicate_check_interval":600
- }
- response = requests.post(send_msg_url,data=json.dumps(data)).content
- return response
- else:
- return False
-
-def send_to_wecom_markdown(text,wecom_cid,wecom_aid,wecom_secret,wecom_touid='@all'):
- get_token_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={wecom_cid}&corpsecret={wecom_secret}"
- response = requests.get(get_token_url).content
- access_token = json.loads(response).get('access_token')
- if access_token and len(access_token) > 0:
- send_msg_url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}'
- data = {
- "touser":wecom_touid,
- "agentid":wecom_aid,
- "msgtype":"markdown",
- "markdown":{
- "content":text
- },
- "duplicate_check_interval":600
- }
- response = requests.post(send_msg_url,data=json.dumps(data)).content
+ response = requests.post(
+ send_msg_url,
+ data=json.dumps(data)
+ ).content
return response
else:
return False
@@ -193,13 +182,16 @@ def send_to_wecom_markdown(text,wecom_cid,wecom_aid,wecom_secret,wecom_touid='@a
使用实例:
```python
-ret = send_to_wecom("推送测试\r\n测试换行", "企业ID③", "应用ID①", "应用secret②");
+ret = send_to_wecom("text", "推送测试\r\n测试换行", "企业ID③", "应用ID①", "应用secret②");
print( ret );
-ret = send_to_wecom('文本中支持超链接', "企业ID③", "应用ID①", "应用secret②");
+
+ret = send_to_wecom("text", '文本中支持超链接', "企业ID③", "应用ID①", "应用secret②");
print( ret );
-ret = send_to_wecom_image("此处填写图片Base64", "企业ID③", "应用ID①", "应用secret②");
+
+ret = send_to_wecom("image", "此处填写图片Base64", "企业ID③", "应用ID①", "应用secret②");
print( ret );
-ret = send_to_wecom_markdown("**Markdown 内容**", "企业ID③", "应用ID①", "应用secret②");
+
+ret = send_to_wecom("markdown", "**Markdown 内容**", "企业ID③", "应用ID①", "应用secret②");
print( ret );
```