From 5e591fc1b7661bd4ef345df16ba1fa2c3e25bbe8 Mon Sep 17 00:00:00 2001 From: arkii Date: Mon, 1 Apr 2024 18:03:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20Feishu(=E9=A3=9E=E4=B9=A6)=20tool?= =?UTF-8?q?=20for=20sending=20message=20to=20chat=20group=20bot=20via=20we?= =?UTF-8?q?bhook=20(#3059)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: crazywoola <427733928@qq.com> --- api/core/tools/provider/_position.yaml | 1 + .../provider/builtin/feishu/_assets/icon.svg | 1 + .../tools/provider/builtin/feishu/feishu.py | 8 +++ .../tools/provider/builtin/feishu/feishu.yaml | 13 +++++ .../builtin/feishu/tools/feishu_group_bot.py | 50 +++++++++++++++++++ .../feishu/tools/feishu_group_bot.yaml | 40 +++++++++++++++ 6 files changed, 113 insertions(+) create mode 100644 api/core/tools/provider/builtin/feishu/_assets/icon.svg create mode 100644 api/core/tools/provider/builtin/feishu/feishu.py create mode 100644 api/core/tools/provider/builtin/feishu/feishu.yaml create mode 100644 api/core/tools/provider/builtin/feishu/tools/feishu_group_bot.py create mode 100644 api/core/tools/provider/builtin/feishu/tools/feishu_group_bot.yaml diff --git a/api/core/tools/provider/_position.yaml b/api/core/tools/provider/_position.yaml index a69f37618e6116..7eb40b2ab89416 100644 --- a/api/core/tools/provider/_position.yaml +++ b/api/core/tools/provider/_position.yaml @@ -25,3 +25,4 @@ - wecom - qrcode - dingtalk +- feishu diff --git a/api/core/tools/provider/builtin/feishu/_assets/icon.svg b/api/core/tools/provider/builtin/feishu/_assets/icon.svg new file mode 100644 index 00000000000000..bf3c202abf3ff6 --- /dev/null +++ b/api/core/tools/provider/builtin/feishu/_assets/icon.svg @@ -0,0 +1 @@ + diff --git a/api/core/tools/provider/builtin/feishu/feishu.py b/api/core/tools/provider/builtin/feishu/feishu.py new file mode 100644 index 00000000000000..13303dbe64e09c --- /dev/null +++ b/api/core/tools/provider/builtin/feishu/feishu.py @@ -0,0 +1,8 @@ +from core.tools.provider.builtin.feishu.tools.feishu_group_bot import FeishuGroupBotTool +from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController + + +class FeishuProvider(BuiltinToolProviderController): + def _validate_credentials(self, credentials: dict) -> None: + FeishuGroupBotTool() + pass diff --git a/api/core/tools/provider/builtin/feishu/feishu.yaml b/api/core/tools/provider/builtin/feishu/feishu.yaml new file mode 100644 index 00000000000000..a1fcd3804742ea --- /dev/null +++ b/api/core/tools/provider/builtin/feishu/feishu.yaml @@ -0,0 +1,13 @@ +identity: + author: Arkii Sun + name: feishu + label: + en_US: Feishu + zh_Hans: 飞书 + pt_BR: Feishu + description: + en_US: Feishu group bot + zh_Hans: 飞书群机器人 + pt_BR: Feishu group bot + icon: icon.svg +credentials_for_provider: diff --git a/api/core/tools/provider/builtin/feishu/tools/feishu_group_bot.py b/api/core/tools/provider/builtin/feishu/tools/feishu_group_bot.py new file mode 100644 index 00000000000000..e8ab02f55ea4ed --- /dev/null +++ b/api/core/tools/provider/builtin/feishu/tools/feishu_group_bot.py @@ -0,0 +1,50 @@ +from typing import Any, Union + +import httpx + +from core.tools.entities.tool_entities import ToolInvokeMessage +from core.tools.tool.builtin_tool import BuiltinTool +from core.tools.utils.uuid_utils import is_valid_uuid + + +class FeishuGroupBotTool(BuiltinTool): + def _invoke(self, user_id: str, tool_parameters: dict[str, Any] + ) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]: + """ + invoke tools + API document: https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot + """ + + url = "https://open.feishu.cn/open-apis/bot/v2/hook" + + content = tool_parameters.get('content', '') + if not content: + return self.create_text_message('Invalid parameter content') + + hook_key = tool_parameters.get('hook_key', '') + if not is_valid_uuid(hook_key): + return self.create_text_message( + f'Invalid parameter hook_key ${hook_key}, not a valid UUID') + + msg_type = 'text' + api_url = f'{url}/{hook_key}' + headers = { + 'Content-Type': 'application/json', + } + params = {} + payload = { + "msg_type": msg_type, + "content": { + "text": content, + } + } + + try: + res = httpx.post(api_url, headers=headers, params=params, json=payload) + if res.is_success: + 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}") + except Exception as e: + return self.create_text_message("Failed to send message to group chat bot. {}".format(e)) \ No newline at end of file diff --git a/api/core/tools/provider/builtin/feishu/tools/feishu_group_bot.yaml b/api/core/tools/provider/builtin/feishu/tools/feishu_group_bot.yaml new file mode 100644 index 00000000000000..6c3f084e4dafe3 --- /dev/null +++ b/api/core/tools/provider/builtin/feishu/tools/feishu_group_bot.yaml @@ -0,0 +1,40 @@ +identity: + name: feishu_group_bot + author: Arkii Sun + label: + en_US: Send Group Message + zh_Hans: 发送群消息 + pt_BR: Send Group Message + icon: icon.png +description: + human: + en_US: Sending a group message on Feishu via the webhook of group bot + zh_Hans: 通过飞书的群机器人webhook发送群消息 + pt_BR: Sending a group message on Feishu via the webhook of group bot + llm: A tool for sending messages to a chat group on Feishu(飞书) . +parameters: + - name: hook_key + type: secret-input + required: true + label: + en_US: Feishu Group bot webhook key + zh_Hans: 群机器人webhook的key + pt_BR: Feishu Group bot webhook key + human_description: + en_US: Feishu Group bot webhook key + zh_Hans: 群机器人webhook的key + pt_BR: Feishu Group bot webhook key + form: form + - name: content + type: string + required: true + label: + en_US: content + zh_Hans: 消息内容 + pt_BR: content + human_description: + en_US: Content to sent to the group. + zh_Hans: 群消息文本 + pt_BR: Content to sent to the group. + llm_description: Content of the message + form: llm