diff --git a/.env b/.env index f4504cb..83b3c5a 100644 --- a/.env +++ b/.env @@ -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="是不是很酷;老胡的储物柜" \ No newline at end of file diff --git "a/docs/02.2C\347\216\257\345\242\203\345\217\230\351\207\217.md" "b/docs/02.2C\347\216\257\345\242\203\345\217\230\351\207\217.md" index 676ba7e..6ca23e7 100644 --- "a/docs/02.2C\347\216\257\345\242\203\345\217\230\351\207\217.md" +++ "b/docs/02.2C\347\216\257\345\242\203\345\217\230\351\207\217.md" @@ -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="" ``` diff --git a/src/config/config.py b/src/config/config.py index dbc74e3..f8995e6 100644 --- a/src/config/config.py +++ b/src/config/config.py @@ -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", diff --git a/src/sender/wecom_sender.py b/src/sender/wecom_sender.py index e10e4ab..f82d8c1 100644 --- a/src/sender/wecom_sender.py +++ b/src/sender/wecom_sender.py @@ -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): """ @@ -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): """ 发送卡片消息 @@ -67,7 +100,8 @@ def send_text_card(self, send_data): doc_des = f'