chore(api/core): apply ruff reformatting (#7624)

This commit is contained in:
Bowen Liang
2024-09-10 17:00:20 +08:00
committed by GitHub
parent 178730266d
commit 2cf1187b32
724 changed files with 21180 additions and 21123 deletions

View File

@@ -13,38 +13,43 @@ from core.tools.tool.builtin_tool import BuiltinTool
class DingTalkGroupBotTool(BuiltinTool):
def _invoke(self, user_id: str, tool_parameters: dict[str, Any]
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
def _invoke(
self, user_id: str, tool_parameters: dict[str, Any]
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
"""
invoke tools
Dingtalk custom group robot API docs:
https://open.dingtalk.com/document/orgapp/custom-robot-access
invoke tools
Dingtalk custom group robot API docs:
https://open.dingtalk.com/document/orgapp/custom-robot-access
"""
content = tool_parameters.get('content')
content = tool_parameters.get("content")
if not content:
return self.create_text_message('Invalid parameter content')
return self.create_text_message("Invalid parameter content")
access_token = tool_parameters.get('access_token')
access_token = tool_parameters.get("access_token")
if not access_token:
return self.create_text_message('Invalid parameter access_token. '
'Regarding information about security details,'
'please refer to the DingTalk docs:'
'https://open.dingtalk.com/document/robots/customize-robot-security-settings')
return self.create_text_message(
"Invalid parameter access_token. "
"Regarding information about security details,"
"please refer to the DingTalk docs:"
"https://open.dingtalk.com/document/robots/customize-robot-security-settings"
)
sign_secret = tool_parameters.get('sign_secret')
sign_secret = tool_parameters.get("sign_secret")
if not sign_secret:
return self.create_text_message('Invalid parameter sign_secret. '
'Regarding information about security details,'
'please refer to the DingTalk docs:'
'https://open.dingtalk.com/document/robots/customize-robot-security-settings')
return self.create_text_message(
"Invalid parameter sign_secret. "
"Regarding information about security details,"
"please refer to the DingTalk docs:"
"https://open.dingtalk.com/document/robots/customize-robot-security-settings"
)
msgtype = 'text'
api_url = 'https://oapi.dingtalk.com/robot/send'
msgtype = "text"
api_url = "https://oapi.dingtalk.com/robot/send"
headers = {
'Content-Type': 'application/json',
"Content-Type": "application/json",
}
params = {
'access_token': access_token,
"access_token": access_token,
}
self._apply_security_mechanism(params, sign_secret)
@@ -53,7 +58,7 @@ class DingTalkGroupBotTool(BuiltinTool):
"msgtype": msgtype,
"text": {
"content": content,
}
},
}
try:
@@ -62,7 +67,8 @@ class DingTalkGroupBotTool(BuiltinTool):
return self.create_text_message("Text message sent successfully")
else:
return self.create_text_message(
f"Failed to send the text message, status code: {res.status_code}, response: {res.text}")
f"Failed to send the text message, status code: {res.status_code}, response: {res.text}"
)
except Exception as e:
return self.create_text_message("Failed to send message to group chat bot. {}".format(e))
@@ -70,14 +76,14 @@ class DingTalkGroupBotTool(BuiltinTool):
def _apply_security_mechanism(params: dict[str, Any], sign_secret: str):
try:
timestamp = str(round(time.time() * 1000))
secret_enc = sign_secret.encode('utf-8')
string_to_sign = f'{timestamp}\n{sign_secret}'
string_to_sign_enc = string_to_sign.encode('utf-8')
secret_enc = sign_secret.encode("utf-8")
string_to_sign = f"{timestamp}\n{sign_secret}"
string_to_sign_enc = string_to_sign.encode("utf-8")
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
params['timestamp'] = timestamp
params['sign'] = sign
params["timestamp"] = timestamp
params["sign"] = sign
except Exception:
msg = "Failed to apply security mechanism to the request."
logging.exception(msg)