Browse Source

feat: add a new built-in tool of Slack Incoming Webhook (#4067)

Pan YANG 11 months ago
parent
commit
5940564d84

+ 1 - 0
api/core/tools/provider/_position.yaml

@@ -30,3 +30,4 @@
 - qrcode
 - dingtalk
 - feishu
+- slack

+ 22 - 0
api/core/tools/provider/builtin/slack/_assets/icon.svg

@@ -0,0 +1,22 @@
+<?xml version="1.0" ?>
+<svg width="54" height="54" viewBox="0 0 54 54" xmlns="http://www.w3.org/2000/svg" role="presentation">
+  <title>Slack</title>
+  <g fill="none" fill-rule="evenodd">
+    <path
+      d="M19.712.133a5.381 5.381 0 0 0-5.376 5.387 5.381 5.381 0 0 0 5.376 5.386h5.376V5.52A5.381 5.381 0 0 0 19.712.133m0 14.365H5.376A5.381 5.381 0 0 0 0 19.884a5.381 5.381 0 0 0 5.376 5.387h14.336a5.381 5.381 0 0 0 5.376-5.387 5.381 5.381 0 0 0-5.376-5.386"
+      fill="#44BEDF"
+    ></path>
+    <path
+      d="M53.76 19.884a5.381 5.381 0 0 0-5.376-5.386 5.381 5.381 0 0 0-5.376 5.386v5.387h5.376a5.381 5.381 0 0 0 5.376-5.387m-14.336 0V5.52A5.381 5.381 0 0 0 34.048.133a5.381 5.381 0 0 0-5.376 5.387v14.364a5.381 5.381 0 0 0 5.376 5.387 5.381 5.381 0 0 0 5.376-5.387"
+      fill="#2EB67D"
+    ></path>
+    <path
+      d="M34.048 54a5.381 5.381 0 0 0 5.376-5.387 5.381 5.381 0 0 0-5.376-5.386h-5.376v5.386A5.381 5.381 0 0 0 34.048 54m0-14.365h14.336a5.381 5.381 0 0 0 5.376-5.386 5.381 5.381 0 0 0-5.376-5.387H34.048a5.381 5.381 0 0 0-5.376 5.387 5.381 5.381 0 0 0 5.376 5.386"
+      fill="#ECB22E"
+    ></path>
+    <path
+      d="M0 34.249a5.381 5.381 0 0 0 5.376 5.386 5.381 5.381 0 0 0 5.376-5.386v-5.387H5.376A5.381 5.381 0 0 0 0 34.25m14.336-.001v14.364A5.381 5.381 0 0 0 19.712 54a5.381 5.381 0 0 0 5.376-5.387V34.25a5.381 5.381 0 0 0-5.376-5.387 5.381 5.381 0 0 0-5.376 5.387"
+      fill="#E01E5A"
+    ></path>
+  </g>
+</svg>

+ 8 - 0
api/core/tools/provider/builtin/slack/slack.py

@@ -0,0 +1,8 @@
+from core.tools.provider.builtin.slack.tools.slack_webhook import SlackWebhookTool
+from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController
+
+
+class SlackProvider(BuiltinToolProviderController):
+    def _validate_credentials(self, credentials: dict) -> None:
+        SlackWebhookTool()
+        pass

+ 13 - 0
api/core/tools/provider/builtin/slack/slack.yaml

@@ -0,0 +1,13 @@
+identity:
+  author: Pan YANG
+  name: slack
+  label:
+    en_US: Slack
+    zh_Hans: Slack
+    pt_BR: Slack
+  description:
+    en_US: Slack Webhook
+    zh_Hans: Slack Webhook
+    pt_BR: Slack Webhook
+  icon: icon.svg
+credentials_for_provider:

+ 43 - 0
api/core/tools/provider/builtin/slack/tools/slack_webhook.py

@@ -0,0 +1,43 @@
+from typing import Any, Union
+
+import httpx
+
+from core.tools.entities.tool_entities import ToolInvokeMessage
+from core.tools.tool.builtin_tool import BuiltinTool
+
+
+class SlackWebhookTool(BuiltinTool):
+    def _invoke(self, user_id: str, tool_parameters: dict[str, Any]
+                ) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
+        """
+            Incoming Webhooks
+            API Document: https://api.slack.com/messaging/webhooks
+        """
+
+        content = tool_parameters.get('content', '')
+        if not content:
+            return self.create_text_message('Invalid parameter content')
+
+        webhook_url = tool_parameters.get('webhook_url', '')
+
+        if not webhook_url.startswith('https://hooks.slack.com/services/'):
+            return self.create_text_message(
+                f'Invalid parameter webhook_url ${webhook_url}, not a valid Slack webhook URL')
+
+        headers = {
+            'Content-Type': 'application/json',
+        }
+        params = {}
+        payload = {
+            "text": content,
+        }
+
+        try:
+            res = httpx.post(webhook_url, headers=headers, params=params, json=payload)
+            if res.is_success:
+                return self.create_text_message("Text message was 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 through webhook. {}".format(e))

+ 40 - 0
api/core/tools/provider/builtin/slack/tools/slack_webhook.yaml

@@ -0,0 +1,40 @@
+identity:
+  name: slack_webhook
+  author: Pan YANG
+  label:
+    en_US: Incoming Webhook to send message
+    zh_Hans: 通过入站 Webhook 发送消息
+    pt_BR: Incoming Webhook to send message
+  icon: icon.svg
+description:
+  human:
+    en_US: Sending a message on Slack via the Incoming Webhook
+    zh_Hans: 通过入站 Webhook 在 Slack 上发送消息
+    pt_BR: Sending a message on Slack via the Incoming Webhook
+  llm: A tool for sending messages to a chat on Slack.
+parameters:
+  - name: webhook_url
+    type: string
+    required: true
+    label:
+      en_US: Slack Incoming Webhook url
+      zh_Hans: Slack 入站 Webhook 的 url
+      pt_BR: Slack Incoming Webhook url
+    human_description:
+      en_US: Slack Incoming Webhook url
+      zh_Hans: Slack 入站 Webhook 的 url
+      pt_BR: Slack Incoming Webhook url
+    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 channel or person.
+      zh_Hans: 消息内容文本
+      pt_BR: Content to sent to the channel or person.
+    llm_description: Content of the message
+    form: llm