From c46b1dd116ed55833cf8577faf4b6774d9905c00 Mon Sep 17 00:00:00 2001 From: thsrite Date: Fri, 8 Sep 2023 09:22:59 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E6=B6=88=E6=81=AF=E8=BD=AC=E5=8F=91?= =?UTF-8?q?=E6=8F=92=E4=BB=B6bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/plugins/messageforward/__init__.py | 62 ++++++++++++++------------ 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/app/plugins/messageforward/__init__.py b/app/plugins/messageforward/__init__.py index 13f90523..d377e6f8 100644 --- a/app/plugins/messageforward/__init__.py +++ b/app/plugins/messageforward/__init__.py @@ -169,18 +169,19 @@ class MessageForward(_PluginBase): # 正则匹配 patterns = self._pattern.split("\n") - for i, pattern in enumerate(patterns): + for index, pattern in enumerate(patterns): msg_match = re.search(pattern, title) if msg_match: - access_token, appid = self.__flush_access_token(i) + access_token, appid = self.__flush_access_token(index) if not access_token: + logger.error("未获取到有效token,请检查配置") continue # 发送消息 if image: - self.__send_image_message(title, text, image, userid, access_token, appid, i) + self.__send_image_message(title, text, image, userid, access_token, appid, index) else: - self.__send_message(title, text, userid, access_token, appid, i) + self.__send_message(title, text, userid, access_token, appid, index) def __save_wechat_token(self): """ @@ -191,7 +192,7 @@ class MessageForward(_PluginBase): # 解析配置 wechats = self._wechat.split("\n") - for i, wechat in enumerate(wechats): + for index, wechat in enumerate(wechats): wechat_config = wechat.split(":") if len(wechat_config) != 3: logger.error(f"{wechat} 应用配置不正确") @@ -201,20 +202,21 @@ class MessageForward(_PluginBase): appsecret = wechat_config[2] # 查询历史是否存储token - wechat_config = wechat_token_history.get("appid") + wechat_config = wechat_token_history.get(appid) access_token = None expires_in = None access_token_time = None if wechat_config: access_token_time = wechat_config['access_token_time'] expires_in = wechat_config['expires_in'] + access_token = wechat_config['access_token'] # 判断token是否过期 if (datetime.now() - access_token_time).seconds < expires_in: - # 重新获取token + # 已过期,重新获取token access_token, expires_in, access_token_time = self.__get_access_token(corpid=corpid, appsecret=appsecret) if not access_token: - # 获取token + # 没有token,获取token access_token, expires_in, access_token_time = self.__get_access_token(corpid=corpid, appsecret=appsecret) if access_token: @@ -225,7 +227,7 @@ class MessageForward(_PluginBase): "corpid": corpid, "appsecret": appsecret } - self._pattern_token[i] = { + self._pattern_token[index] = { "appid": appid, "corpid": corpid, "appsecret": appsecret, @@ -240,13 +242,13 @@ class MessageForward(_PluginBase): if wechat_token_history: self.save_data("wechat_token", wechat_token_history) - def __flush_access_token(self, i: int): + def __flush_access_token(self, index: int): """ 获取第i个配置wechat token """ - wechat_token = self._pattern_token[i] + wechat_token = self._pattern_token[index] if not wechat_token: - logger.error(f"未获取到第 {i} 条正则对应的wechat应用token,请检查配置") + logger.error(f"未获取到第 {index} 条正则对应的wechat应用token,请检查配置") return None access_token = wechat_token['access_token'] expires_in = wechat_token['expires_in'] @@ -264,7 +266,7 @@ class MessageForward(_PluginBase): logger.error(f"wechat配置 appid = {appid} 获取token失败,请检查配置") return None, None - self._pattern_token[i] = { + self._pattern_token[index] = { "appid": appid, "corpid": corpid, "appsecret": appsecret, @@ -275,7 +277,7 @@ class MessageForward(_PluginBase): return access_token, appid def __send_message(self, title: str, text: str = None, userid: str = None, access_token: str = None, - appid: str = None, i: int = None) -> Optional[bool]: + appid: str = None, index: int = None) -> Optional[bool]: """ 发送文本消息 :param title: 消息标题 @@ -283,7 +285,6 @@ class MessageForward(_PluginBase): :param userid: 消息发送对象的ID,为空则发给所有人 :return: 发送状态,错误信息 """ - message_url = self._send_msg_url % access_token if text: conent = "%s\n%s" % (title, text.replace("\n\n", "\n")) else: @@ -302,10 +303,10 @@ class MessageForward(_PluginBase): "enable_id_trans": 0, "enable_duplicate_check": 0 } - return self.__post_request(message_url=message_url, req_json=req_json, i=i, title=title) + return self.__post_request(access_token=access_token, req_json=req_json, index=index, title=title) def __send_image_message(self, title: str, text: str, image_url: str, userid: str = None, - access_token: str = None, appid: str = None, i: int = None) -> Optional[bool]: + access_token: str = None, appid: str = None, index: int = None) -> Optional[bool]: """ 发送图文消息 :param title: 消息标题 @@ -314,7 +315,6 @@ class MessageForward(_PluginBase): :param userid: 消息发送对象的ID,为空则发给所有人 :return: 发送状态,错误信息 """ - message_url = self._send_msg_url % access_token if text: text = text.replace("\n\n", "\n") if not userid: @@ -334,9 +334,10 @@ class MessageForward(_PluginBase): ] } } - return self.__post_request(message_url=message_url, req_json=req_json, i=i, title=title) + return self.__post_request(access_token=access_token, req_json=req_json, index=index, title=title) - def __post_request(self, message_url: str, req_json: dict, i: int, title: str, retry: int = 0) -> bool: + def __post_request(self, access_token: str, req_json: dict, index: int, title: str, retry: int = 0) -> bool: + message_url = self._send_msg_url % access_token """ 向微信发送请求 """ @@ -351,15 +352,20 @@ class MessageForward(_PluginBase): logger.info(f"转发消息 {title} 成功") return True else: - if ret_json.get('errcode') == 42001: - # 重新获取token - self.__flush_access_token(i) - retry += 1 - # 重发请求 - if retry <= 3: - self.__post_request(message_url=message_url, req_json=req_json, i=i, title=title, - retry=retry) logger.error(f"转发消息 {title} 失败,错误信息:{ret_json}") + if ret_json.get('errcode') == 42001 or ret_json.get('errcode') == 40014: + logger.info("token已过期,正在重新刷新token重试") + # 重新获取token + access_token, appid = self.__flush_access_token(index) + if access_token: + retry += 1 + # 重发请求 + if retry <= 3: + return self.__post_request(access_token=access_token, + req_json=req_json, + index=index, + title=title, + retry=retry) return False elif res is not None: logger.error(f"转发消息 {title} 失败,错误码:{res.status_code},错误原因:{res.reason}")