-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Tools): add discord incoming webhook for sending messages
- Loading branch information
Showing
5 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,9 @@ | ||
from typing import Any | ||
|
||
from core.tools.provider.builtin.discord.tools.discord_webhook import DiscordWebhookTool | ||
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController | ||
|
||
|
||
class DiscordProvider(BuiltinToolProviderController): | ||
def _validate_credentials(self, credentials: dict[str, Any]) -> None: | ||
DiscordWebhookTool() |
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,16 @@ | ||
identity: | ||
author: Ice Yao | ||
name: discord | ||
label: | ||
en_US: Discord | ||
zh_Hans: Discord | ||
pt_BR: Discord | ||
description: | ||
en_US: Discord Webhook | ||
zh_Hans: Discord Webhook | ||
pt_BR: Discord Webhook | ||
icon: icon.svg | ||
tags: | ||
- social | ||
- productivity | ||
credentials_for_provider: |
49 changes: 49 additions & 0 deletions
49
api/core/tools/provider/builtin/discord/tools/discord_webhook.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,49 @@ | ||
from typing import Any, Union | ||
|
||
import httpx | ||
|
||
from core.tools.entities.tool_entities import ToolInvokeMessage | ||
from core.tools.tool.builtin_tool import BuiltinTool | ||
|
||
|
||
class DiscordWebhookTool(BuiltinTool): | ||
def _invoke(self, user_id: str, tool_parameters: dict[str, Any] | ||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]: | ||
""" | ||
Incoming Webhooks | ||
API Document: | ||
https://discord.com/developers/docs/resources/webhook#execute-webhook | ||
""" | ||
|
||
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://discord.com/api/webhooks/'): | ||
return self.create_text_message( | ||
f'Invalid parameter webhook_url ${webhook_url}, \ | ||
not a valid Discord webhook URL') | ||
|
||
headers = { | ||
'Content-Type': 'application/json', | ||
} | ||
params = {} | ||
payload = { | ||
"content": 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 changes: 40 additions & 0 deletions
40
api/core/tools/provider/builtin/discord/tools/discord_webhook.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: discord_webhook | ||
author: Ice Yao | ||
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 Discord via the Incoming Webhook | ||
zh_Hans: 通过入站Webhook在Discord上发送消息 | ||
pt_BR: Sending a message on Discord via the Incoming Webhook | ||
llm: A tool for sending messages to a chat on Discord. | ||
parameters: | ||
- name: webhook_url | ||
type: string | ||
required: true | ||
label: | ||
en_US: Discord Incoming Webhook url | ||
zh_Hans: Discord入站Webhook的url | ||
pt_BR: Discord Incoming Webhook url | ||
human_description: | ||
en_US: Discord Incoming Webhook url | ||
zh_Hans: Discord入站Webhook的url | ||
pt_BR: Discord 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 |