Skip to content

Commit

Permalink
Merge pull request #21 from zyd16888/main
Browse files Browse the repository at this point in the history
Fixed: 动态获取企业微信分发部门ID参数 #16
  • Loading branch information
howie6879 authored Dec 27, 2021
2 parents cdbb2b9 + 8950948 commit 0ce50b2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ CC_D_TOKEN=""
CC_WECOM_ID=""
CC_WECOM_AGENT_ID="-1"
CC_WECOM_SECRET=""
CC_WECOM_TO_USER=""
CC_WECOM_PARTY=""
CC_PROXY=""
CC_WECHAT_ACCOUNT="是不是很酷;老胡的储物柜"
6 changes: 5 additions & 1 deletion docs/02.2C环境变量.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ CC_WECHAT_ACCOUNT="是不是很酷;老胡的储物柜"
CC_SENDER_NAME="ding;wecom"
# 分发终端为钉钉钉必须配置的Token
CC_D_TOKEN=""
# 分发终端为企业微信的配置
# 分发终端为企业微信的配置,如果不配置分发用户与部门,则默认会发送给所有部门的所有用户
CC_WECOM_ID=""
CC_WECOM_AGENT_ID="-1"
CC_WECOM_SECRET=""
# 企业微信分发用户(填写用户帐号,不区分大小写),多个用户用;分割
CC_WECOM_TO_USER=""
# 企业微信分发部门(填写部门名称),多个部门用;分割
CC_WECOM_PARTY=""
```

4 changes: 4 additions & 0 deletions src/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class Config:
WECOM_ID = os.getenv("CC_WECOM_ID", "")
WECOM_AGENT_ID = int(os.getenv("CC_WECOM_AGENT_ID", "-1"))
WECOM_SECRET = os.getenv("CC_WECOM_SECRET", "")
# 企业微信分发部门,多个部门用;分割
WECOM_PARTY_LIST = os.getenv("CC_WECOM_PARTY", "").split(";")
# 企业微信分发用户,多个用户用;分割
WECOM_TO_USER = os.getenv("CC_WECOM_TO_USER", "").replace(";", "|")
# 订阅的公众号配置
WECHAT_LIST = os.getenv(
"CC_WECHAT_ACCOUNT",
Expand Down
40 changes: 38 additions & 2 deletions src/sender/wecom_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@ def __init__(self, send_config: dict):
self.wecom_id = send_config.get("wecom_id", Config.WECOM_ID)
self.wecom_agent_id = send_config.get("wecom_agent_id", Config.WECOM_AGENT_ID)
self.wecom_secret = send_config.get("wecom_secret", Config.WECOM_SECRET)
self.url = f"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={self.get_token()}"
self.access_token = self.get_token()
self.url = f"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={self.access_token}"
self.wecom_party_list = send_config.get("wecom_party_list", Config.WECOM_PARTY_LIST)
self.wecom_to_user = send_config.get("wecom_to_user", Config.WECOM_TO_USER)
self.wecom_party = ""
# 如果部门和用户都没有,则默认发送给所有人
if not self.wecom_party_list[0] and not self.wecom_to_user:
self.wecom_to_user = "@all"
# 其他情况,则按用户填写的发送(既发用户,也发部门)
else:
self.change_wecom_party_to_id()

def get_token(self):
"""
Expand All @@ -45,6 +55,29 @@ def get_token(self):
json_data = requests.get(token_url, params=data).json()
return json_data.get("access_token", "")

def get_party(self):
"""
获取部门列表
:return:
"""
data = {
"access_token": self.access_token,
}
url = "https://qyapi.weixin.qq.com/cgi-bin/department/list"
json_data = requests.get(url, params=data).json()
return json_data.get("department", [])

def change_wecom_party_to_id(self):
"""
将部门名称转换为部门ID
:return:
"""
party_list = self.get_party()
party_ids = [party_info["id"] for party_info in party_list if party_info["name"] in self.wecom_party_list]
for party_id in party_ids:
self.wecom_party += f"{party_id}|"
self.wecom_party = self.wecom_party[:-1]

def send_text_card(self, send_data):
"""
发送卡片消息
Expand All @@ -67,7 +100,8 @@ def send_text_card(self, send_data):
doc_des = f'<div class="black">{doc_date} | {doc_cus_des}</div>\n<div class="normal">{doc_des_info}</div>\n来自[2c]👉技术支持❤️'

data = {
"toparty": 1,
"touser": self.wecom_to_user,
"toparty": self.wecom_party,
"msgtype": "textcard",
"agentid": self.wecom_agent_id,
"textcard": {
Expand Down Expand Up @@ -146,6 +180,8 @@ def send(send_config: dict, send_data: dict) -> bool:
"wecom_id": "",
"wecom_agent_id": 0,
"wecom_secret": "",
"wecom_party_list": [],
"wecom_to_user": "",
},
send_data={
"doc_id": "f42460107f69c9e929f8d591243efeb2",
Expand Down

0 comments on commit 0ce50b2

Please sign in to comment.