mirror of
https://gitee.com/dify_ai/dify.git
synced 2024-11-29 17:58:19 +08:00
FEAT: Add twilio tool for sending text and whatsapp messages (#2700)
Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>
This commit is contained in:
parent
fa7ba30ba3
commit
2f28afebb6
1
api/core/tools/provider/builtin/twilio/_assets/icon.svg
Normal file
1
api/core/tools/provider/builtin/twilio/_assets/icon.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg width="2500" height="2500" viewBox="0 0 256 256" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid"><g fill="#CF272D"><path d="M127.86 222.304c-52.005 0-94.164-42.159-94.164-94.163 0-52.005 42.159-94.163 94.164-94.163 52.004 0 94.162 42.158 94.162 94.163 0 52.004-42.158 94.163-94.162 94.163zm0-222.023C57.245.281 0 57.527 0 128.141 0 198.756 57.245 256 127.86 256c70.614 0 127.859-57.244 127.859-127.859 0-70.614-57.245-127.86-127.86-127.86z"/><path d="M133.116 96.297c0-14.682 11.903-26.585 26.586-26.585 14.683 0 26.585 11.903 26.585 26.585 0 14.684-11.902 26.586-26.585 26.586-14.683 0-26.586-11.902-26.586-26.586M133.116 159.983c0-14.682 11.903-26.586 26.586-26.586 14.683 0 26.585 11.904 26.585 26.586 0 14.683-11.902 26.586-26.585 26.586-14.683 0-26.586-11.903-26.586-26.586M69.431 159.983c0-14.682 11.904-26.586 26.586-26.586 14.683 0 26.586 11.904 26.586 26.586 0 14.683-11.903 26.586-26.586 26.586-14.682 0-26.586-11.903-26.586-26.586M69.431 96.298c0-14.683 11.904-26.585 26.586-26.585 14.683 0 26.586 11.902 26.586 26.585 0 14.684-11.903 26.586-26.586 26.586-14.682 0-26.586-11.902-26.586-26.586"/></g></svg>
|
After Width: | Height: | Size: 1.1 KiB |
41
api/core/tools/provider/builtin/twilio/tools/send_message.py
Normal file
41
api/core/tools/provider/builtin/twilio/tools/send_message.py
Normal file
@ -0,0 +1,41 @@
|
||||
from typing import Any, Union
|
||||
|
||||
from langchain.utilities import TwilioAPIWrapper
|
||||
|
||||
from core.tools.entities.tool_entities import ToolInvokeMessage
|
||||
from core.tools.tool.builtin_tool import BuiltinTool
|
||||
|
||||
|
||||
class SendMessageTool(BuiltinTool):
|
||||
"""
|
||||
A tool for sending messages using Twilio API.
|
||||
|
||||
Args:
|
||||
user_id (str): The ID of the user invoking the tool.
|
||||
tool_parameters (Dict[str, Any]): The parameters required for sending the message.
|
||||
|
||||
Returns:
|
||||
Union[ToolInvokeMessage, List[ToolInvokeMessage]]: The result of invoking the tool, which includes the status of the message sending operation.
|
||||
"""
|
||||
|
||||
def _invoke(
|
||||
self, user_id: str, tool_parameters: dict[str, Any]
|
||||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
account_sid = self.runtime.credentials["account_sid"]
|
||||
auth_token = self.runtime.credentials["auth_token"]
|
||||
from_number = self.runtime.credentials["from_number"]
|
||||
|
||||
message = tool_parameters["message"]
|
||||
to_number = tool_parameters["to_number"]
|
||||
|
||||
if to_number.startswith("whatsapp:"):
|
||||
from_number = f"whatsapp: {from_number}"
|
||||
|
||||
twilio = TwilioAPIWrapper(
|
||||
account_sid=account_sid, auth_token=auth_token, from_number=from_number
|
||||
)
|
||||
|
||||
# Sending the message through Twilio
|
||||
result = twilio.run(message, to_number)
|
||||
|
||||
return self.create_text_message(text="Message sent successfully.")
|
@ -0,0 +1,40 @@
|
||||
identity:
|
||||
name: send_message
|
||||
author: Yash Parmar
|
||||
label:
|
||||
en_US: SendMessage
|
||||
zh_Hans: 发送消息
|
||||
pt_BR: SendMessage
|
||||
description:
|
||||
human:
|
||||
en_US: Send SMS or Twilio Messaging Channels messages.
|
||||
zh_Hans: 发送SMS或Twilio消息通道消息。
|
||||
pt_BR: Send SMS or Twilio Messaging Channels messages.
|
||||
llm: Send SMS or Twilio Messaging Channels messages. Supports different channels including WhatsApp.
|
||||
parameters:
|
||||
- name: message
|
||||
type: string
|
||||
required: true
|
||||
label:
|
||||
en_US: Message
|
||||
zh_Hans: 消息内容
|
||||
pt_BR: Message
|
||||
human_description:
|
||||
en_US: The content of the message to be sent.
|
||||
zh_Hans: 要发送的消息内容。
|
||||
pt_BR: The content of the message to be sent.
|
||||
llm_description: The content of the message to be sent.
|
||||
form: llm
|
||||
- name: to_number
|
||||
type: string
|
||||
required: true
|
||||
label:
|
||||
en_US: To Number
|
||||
zh_Hans: 收信号码
|
||||
pt_BR: Para Número
|
||||
human_description:
|
||||
en_US: The recipient's phone number. Prefix with 'whatsapp:' for WhatsApp messages, e.g., "whatsapp:+1234567890".
|
||||
zh_Hans: 收件人的电话号码。WhatsApp消息前缀为'whatsapp:',例如,"whatsapp:+1234567890"。
|
||||
pt_BR: The recipient's phone number. Prefix with 'whatsapp:' for WhatsApp messages, e.g., "whatsapp:+1234567890".
|
||||
llm_description: The recipient's phone number. Prefix with 'whatsapp:' for WhatsApp messages, e.g., "whatsapp:+1234567890".
|
||||
form: llm
|
25
api/core/tools/provider/builtin/twilio/twilio.py
Normal file
25
api/core/tools/provider/builtin/twilio/twilio.py
Normal file
@ -0,0 +1,25 @@
|
||||
from typing import Any
|
||||
|
||||
from core.tools.errors import ToolProviderCredentialValidationError
|
||||
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController
|
||||
|
||||
|
||||
class TwilioProvider(BuiltinToolProviderController):
|
||||
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
|
||||
try:
|
||||
"""
|
||||
SendMessageTool().fork_tool_runtime(
|
||||
meta={
|
||||
"credentials": credentials,
|
||||
}
|
||||
).invoke(
|
||||
user_id="",
|
||||
tool_parameters={
|
||||
"message": "Credential validation message",
|
||||
"to_number": "+14846624384",
|
||||
},
|
||||
)
|
||||
"""
|
||||
pass
|
||||
except Exception as e:
|
||||
raise ToolProviderCredentialValidationError(str(e))
|
46
api/core/tools/provider/builtin/twilio/twilio.yaml
Normal file
46
api/core/tools/provider/builtin/twilio/twilio.yaml
Normal file
@ -0,0 +1,46 @@
|
||||
identity:
|
||||
author: Yash Parmar
|
||||
name: twilio
|
||||
label:
|
||||
en_US: Twilio
|
||||
zh_Hans: Twilio
|
||||
pt_BR: Twilio
|
||||
description:
|
||||
en_US: Send messages through SMS or Twilio Messaging Channels.
|
||||
zh_Hans: 通过SMS或Twilio消息通道发送消息。
|
||||
pt_BR: Send messages through SMS or Twilio Messaging Channels.
|
||||
icon: icon.svg
|
||||
credentials_for_provider:
|
||||
account_sid:
|
||||
type: secret-input
|
||||
required: true
|
||||
label:
|
||||
en_US: Account SID
|
||||
zh_Hans: 账户SID
|
||||
pt_BR: Account SID
|
||||
placeholder:
|
||||
en_US: Please input your Twilio Account SID
|
||||
zh_Hans: 请输入您的Twilio账户SID
|
||||
pt_BR: Please input your Twilio Account SID
|
||||
auth_token:
|
||||
type: secret-input
|
||||
required: true
|
||||
label:
|
||||
en_US: Auth Token
|
||||
zh_Hans: 认证令牌
|
||||
pt_BR: Auth Token
|
||||
placeholder:
|
||||
en_US: Please input your Twilio Auth Token
|
||||
zh_Hans: 请输入您的Twilio认证令牌
|
||||
pt_BR: Please input your Twilio Auth Token
|
||||
from_number:
|
||||
type: secret-input
|
||||
required: true
|
||||
label:
|
||||
en_US: From Number
|
||||
zh_Hans: 发信号码
|
||||
pt_BR: De Número
|
||||
placeholder:
|
||||
en_US: Please input your Twilio phone number
|
||||
zh_Hans: 请输入您的Twilio电话号码
|
||||
pt_BR: Please input your Twilio phone number
|
@ -70,4 +70,5 @@ numexpr~=2.9.0
|
||||
duckduckgo-search==4.4.3
|
||||
arxiv==2.1.0
|
||||
yarl~=1.9.4
|
||||
twilio==9.0.0
|
||||
qrcode~=7.4.2
|
||||
|
Loading…
Reference in New Issue
Block a user