Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
a2bcf8df9a
@ -169,18 +169,19 @@ class MessageForward(_PluginBase):
|
|||||||
|
|
||||||
# 正则匹配
|
# 正则匹配
|
||||||
patterns = self._pattern.split("\n")
|
patterns = self._pattern.split("\n")
|
||||||
for i, pattern in enumerate(patterns):
|
for index, pattern in enumerate(patterns):
|
||||||
msg_match = re.search(pattern, title)
|
msg_match = re.search(pattern, title)
|
||||||
if msg_match:
|
if msg_match:
|
||||||
access_token, appid = self.__flush_access_token(i)
|
access_token, appid = self.__flush_access_token(index)
|
||||||
if not access_token:
|
if not access_token:
|
||||||
|
logger.error("未获取到有效token,请检查配置")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# 发送消息
|
# 发送消息
|
||||||
if image:
|
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:
|
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):
|
def __save_wechat_token(self):
|
||||||
"""
|
"""
|
||||||
@ -191,7 +192,7 @@ class MessageForward(_PluginBase):
|
|||||||
|
|
||||||
# 解析配置
|
# 解析配置
|
||||||
wechats = self._wechat.split("\n")
|
wechats = self._wechat.split("\n")
|
||||||
for i, wechat in enumerate(wechats):
|
for index, wechat in enumerate(wechats):
|
||||||
wechat_config = wechat.split(":")
|
wechat_config = wechat.split(":")
|
||||||
if len(wechat_config) != 3:
|
if len(wechat_config) != 3:
|
||||||
logger.error(f"{wechat} 应用配置不正确")
|
logger.error(f"{wechat} 应用配置不正确")
|
||||||
@ -201,20 +202,21 @@ class MessageForward(_PluginBase):
|
|||||||
appsecret = wechat_config[2]
|
appsecret = wechat_config[2]
|
||||||
|
|
||||||
# 查询历史是否存储token
|
# 查询历史是否存储token
|
||||||
wechat_config = wechat_token_history.get("appid")
|
wechat_config = wechat_token_history.get(appid)
|
||||||
access_token = None
|
access_token = None
|
||||||
expires_in = None
|
expires_in = None
|
||||||
access_token_time = None
|
access_token_time = None
|
||||||
if wechat_config:
|
if wechat_config:
|
||||||
access_token_time = wechat_config['access_token_time']
|
access_token_time = wechat_config['access_token_time']
|
||||||
expires_in = wechat_config['expires_in']
|
expires_in = wechat_config['expires_in']
|
||||||
|
access_token = wechat_config['access_token']
|
||||||
# 判断token是否过期
|
# 判断token是否过期
|
||||||
if (datetime.now() - access_token_time).seconds < expires_in:
|
if (datetime.now() - access_token_time).seconds < expires_in:
|
||||||
# 重新获取token
|
# 已过期,重新获取token
|
||||||
access_token, expires_in, access_token_time = self.__get_access_token(corpid=corpid,
|
access_token, expires_in, access_token_time = self.__get_access_token(corpid=corpid,
|
||||||
appsecret=appsecret)
|
appsecret=appsecret)
|
||||||
if not access_token:
|
if not access_token:
|
||||||
# 获取token
|
# 没有token,获取token
|
||||||
access_token, expires_in, access_token_time = self.__get_access_token(corpid=corpid,
|
access_token, expires_in, access_token_time = self.__get_access_token(corpid=corpid,
|
||||||
appsecret=appsecret)
|
appsecret=appsecret)
|
||||||
if access_token:
|
if access_token:
|
||||||
@ -225,7 +227,7 @@ class MessageForward(_PluginBase):
|
|||||||
"corpid": corpid,
|
"corpid": corpid,
|
||||||
"appsecret": appsecret
|
"appsecret": appsecret
|
||||||
}
|
}
|
||||||
self._pattern_token[i] = {
|
self._pattern_token[index] = {
|
||||||
"appid": appid,
|
"appid": appid,
|
||||||
"corpid": corpid,
|
"corpid": corpid,
|
||||||
"appsecret": appsecret,
|
"appsecret": appsecret,
|
||||||
@ -240,13 +242,13 @@ class MessageForward(_PluginBase):
|
|||||||
if wechat_token_history:
|
if wechat_token_history:
|
||||||
self.save_data("wechat_token", 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
|
获取第i个配置wechat token
|
||||||
"""
|
"""
|
||||||
wechat_token = self._pattern_token[i]
|
wechat_token = self._pattern_token[index]
|
||||||
if not wechat_token:
|
if not wechat_token:
|
||||||
logger.error(f"未获取到第 {i} 条正则对应的wechat应用token,请检查配置")
|
logger.error(f"未获取到第 {index} 条正则对应的wechat应用token,请检查配置")
|
||||||
return None
|
return None
|
||||||
access_token = wechat_token['access_token']
|
access_token = wechat_token['access_token']
|
||||||
expires_in = wechat_token['expires_in']
|
expires_in = wechat_token['expires_in']
|
||||||
@ -264,7 +266,7 @@ class MessageForward(_PluginBase):
|
|||||||
logger.error(f"wechat配置 appid = {appid} 获取token失败,请检查配置")
|
logger.error(f"wechat配置 appid = {appid} 获取token失败,请检查配置")
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
self._pattern_token[i] = {
|
self._pattern_token[index] = {
|
||||||
"appid": appid,
|
"appid": appid,
|
||||||
"corpid": corpid,
|
"corpid": corpid,
|
||||||
"appsecret": appsecret,
|
"appsecret": appsecret,
|
||||||
@ -275,7 +277,7 @@ class MessageForward(_PluginBase):
|
|||||||
return access_token, appid
|
return access_token, appid
|
||||||
|
|
||||||
def __send_message(self, title: str, text: str = None, userid: str = None, access_token: str = None,
|
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: 消息标题
|
:param title: 消息标题
|
||||||
@ -283,7 +285,6 @@ class MessageForward(_PluginBase):
|
|||||||
:param userid: 消息发送对象的ID,为空则发给所有人
|
:param userid: 消息发送对象的ID,为空则发给所有人
|
||||||
:return: 发送状态,错误信息
|
:return: 发送状态,错误信息
|
||||||
"""
|
"""
|
||||||
message_url = self._send_msg_url % access_token
|
|
||||||
if text:
|
if text:
|
||||||
conent = "%s\n%s" % (title, text.replace("\n\n", "\n"))
|
conent = "%s\n%s" % (title, text.replace("\n\n", "\n"))
|
||||||
else:
|
else:
|
||||||
@ -302,10 +303,10 @@ class MessageForward(_PluginBase):
|
|||||||
"enable_id_trans": 0,
|
"enable_id_trans": 0,
|
||||||
"enable_duplicate_check": 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,
|
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: 消息标题
|
:param title: 消息标题
|
||||||
@ -314,7 +315,6 @@ class MessageForward(_PluginBase):
|
|||||||
:param userid: 消息发送对象的ID,为空则发给所有人
|
:param userid: 消息发送对象的ID,为空则发给所有人
|
||||||
:return: 发送状态,错误信息
|
:return: 发送状态,错误信息
|
||||||
"""
|
"""
|
||||||
message_url = self._send_msg_url % access_token
|
|
||||||
if text:
|
if text:
|
||||||
text = text.replace("\n\n", "\n")
|
text = text.replace("\n\n", "\n")
|
||||||
if not userid:
|
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} 成功")
|
logger.info(f"转发消息 {title} 成功")
|
||||||
return True
|
return True
|
||||||
else:
|
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}")
|
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
|
return False
|
||||||
elif res is not None:
|
elif res is not None:
|
||||||
logger.error(f"转发消息 {title} 失败,错误码:{res.status_code},错误原因:{res.reason}")
|
logger.error(f"转发消息 {title} 失败,错误码:{res.status_code},错误原因:{res.reason}")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user