forked from langgenius/dify
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
api/core/tools/provider/builtin/feishu/tools/feishu_get_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from typing import Any, Union | ||
|
||
from core.tools.entities.tool_entities import ToolInvokeMessage | ||
from core.tools.tool.builtin_tool import BuiltinTool | ||
from feishuconnector import FeishuConnector | ||
import re | ||
import pandas as pd | ||
|
||
|
||
def get_feishu_table(app_id, app_secret, url): | ||
fc = FeishuConnector({"default": None}) | ||
fc.init(app_id=app_id, app_secret=app_secret) | ||
""" | ||
https://puyuan.feishu.cn/wiki/JqfTbpjOqaaqX6sH7YycBHnQndc?table=tblpLi6m92tgzzM5&view=vewp20ORLW | ||
https://puyuan.feishu.cn/wiki/KUnOwXioIii4MwkmyEHckbyan2b?sheet=ed98fa | ||
""" | ||
assert not "base" in url, "Only Wiki is supported" | ||
is_sheet = "sheet" in url | ||
if is_sheet: | ||
match = re.findall(r"wiki/(.*)\?sheet=(.*)", url) | ||
else: | ||
match = re.findall(r"wiki/(.*)\?table=(.*)&", url) | ||
assert match, "Invalid parameter table_url" | ||
assert len(match[0]) == 2, "Invalid parameter table_url" | ||
if is_sheet: | ||
records = fc.get_sheet_data(match[0][0], match[0][1]) | ||
return pd.DataFrame(records[1:], columns=records[0]) | ||
else: | ||
records = fc.get_bitable_records(match[0][0], match[0][1]) | ||
return pd.DataFrame([r["fields"] for r in records]) | ||
|
||
|
||
class FeishuGetTableTool(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 | ||
https://open.feishu.cn/document/home/agile-project-cycle-management-based-on-bitable/prep-work | ||
""" | ||
|
||
# app_id = tool_parameters.get("app_id", '') | ||
# if not app_id: | ||
# return self.create_text_message('Invalid parameter app_id') | ||
|
||
# app_secret = tool_parameters.get('app_secret', '') | ||
# if not app_secret: | ||
# return self.create_text_message('Invalid parameter app_secret') | ||
|
||
table_url = tool_parameters.get("table_url", "") | ||
if not table_url: | ||
return self.create_text_message("Invalid parameter table_url") | ||
|
||
try: | ||
df = get_feishu_table( | ||
self.runtime.credentials["app_id"], | ||
self.runtime.credentials["app_secret"], | ||
table_url, | ||
) | ||
return self.create_text_message(df.to_markdown(index=False)) | ||
except Exception as e: | ||
return self.create_text_message("Failed to get table. {}".format(e)) |
52 changes: 52 additions & 0 deletions
52
api/core/tools/provider/builtin/feishu/tools/feishu_get_table.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
identity: | ||
name: feishu_get_table | ||
author: Hangxing Wei | ||
label: | ||
en_US: Get Table | ||
zh_Hans: 获取表格 | ||
pt_BR: Get Table | ||
icon: icon.png | ||
description: | ||
human: | ||
en_US: Get table on Feishu via group bot | ||
zh_Hans: 通过飞书的群机器人获取表格 | ||
pt_BR: Get table on Feishu via group bot | ||
llm: A tool for get table from Feishu(飞书) . | ||
parameters: | ||
# - name: app_id | ||
# type: secret-input | ||
# required: true | ||
# label: | ||
# en_US: Feishu bot app_id | ||
# zh_Hans: 飞书机器人app_id | ||
# pt_BR: Feishu bot app_id | ||
# human_description: | ||
# en_US: Feishu bot app_id | ||
# zh_Hans: 飞书机器人app_id | ||
# pt_BR: Feishu bot app_id | ||
# form: form | ||
# - name: app_secret | ||
# type: secret-input | ||
# required: true | ||
# label: | ||
# en_US: Feishu bot app_secret | ||
# zh_Hans: 飞书机器人app_secret | ||
# pt_BR: Feishu bot app_secret | ||
# human_description: | ||
# en_US: Feishu bot app_secret | ||
# zh_Hans: 飞书机器人app_secret | ||
# pt_BR: Feishu bot app_secret | ||
# form: form | ||
- name: table_url | ||
type: string | ||
required: true | ||
label: | ||
en_US: table url | ||
zh_Hans: 表格 url | ||
pt_BR: table url | ||
human_description: | ||
en_US: table url | ||
zh_Hans: 表格 url | ||
pt_BR: table url | ||
llm_description: table url | ||
form: llm |
50 changes: 50 additions & 0 deletions
50
api/core/tools/provider/builtin/feishu/tools/feishu_write_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 FeishuGetTableTool(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)) |
40 changes: 40 additions & 0 deletions
40
api/core/tools/provider/builtin/feishu/tools/feishu_write_table.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
identity: | ||
name: feishu_write_table | ||
author: Hangxing Wei | ||
label: | ||
en_US: Write Table | ||
zh_Hans: 写入表格 | ||
pt_BR: Write Table | ||
icon: icon.png | ||
description: | ||
human: | ||
en_US: Write table on Feishu via group bot | ||
zh_Hans: 通过飞书的群机器人写入表格 | ||
pt_BR: Write table on Feishu via group bot | ||
llm: A tool for write table to 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 |