diff --git a/api/core/tools/provider/builtin/twilio/_assets/icon.svg b/api/core/tools/provider/builtin/twilio/_assets/icon.svg new file mode 100644 index 0000000000..a1e2bd12c2 --- /dev/null +++ b/api/core/tools/provider/builtin/twilio/_assets/icon.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/api/core/tools/provider/builtin/twilio/tools/send_message.py b/api/core/tools/provider/builtin/twilio/tools/send_message.py new file mode 100644 index 0000000000..984ac3e906 --- /dev/null +++ b/api/core/tools/provider/builtin/twilio/tools/send_message.py @@ -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.") diff --git a/api/core/tools/provider/builtin/twilio/tools/send_message.yaml b/api/core/tools/provider/builtin/twilio/tools/send_message.yaml new file mode 100644 index 0000000000..e129698c86 --- /dev/null +++ b/api/core/tools/provider/builtin/twilio/tools/send_message.yaml @@ -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 diff --git a/api/core/tools/provider/builtin/twilio/twilio.py b/api/core/tools/provider/builtin/twilio/twilio.py new file mode 100644 index 0000000000..dbf30962f9 --- /dev/null +++ b/api/core/tools/provider/builtin/twilio/twilio.py @@ -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)) diff --git a/api/core/tools/provider/builtin/twilio/twilio.yaml b/api/core/tools/provider/builtin/twilio/twilio.yaml new file mode 100644 index 0000000000..b5143c8736 --- /dev/null +++ b/api/core/tools/provider/builtin/twilio/twilio.yaml @@ -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 diff --git a/api/requirements.txt b/api/requirements.txt index 8dace7c4fa..9721c3a13d 100644 --- a/api/requirements.txt +++ b/api/requirements.txt @@ -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