Skip to content
108 changes: 50 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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('<a href="https://www.github.com/">文本中支持超链接</a>', "企业ID③", "应用ID①", "应用secret②");

ret = send_to_wecom("text", '<a href="https://www.github.com/">文本中支持超链接</a>', "企业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 );
```

Expand Down