Spaces:
Runtime error
Runtime error
Commit
·
7a158ef
1
Parent(s):
af798e5
Add SendWhatsAppText tool for sending messages via WhatsApp
Browse files
agency_ai_demo/agents/TechnicalProjectManager/TechnicalProjectManager.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
from agency_swarm.agents import Agent
|
|
|
2 |
|
3 |
|
4 |
class TechnicalProjectManager(Agent):
|
@@ -9,7 +10,7 @@ class TechnicalProjectManager(Agent):
|
|
9 |
instructions="./instructions.md",
|
10 |
files_folder="./files",
|
11 |
schemas_folder="./schemas",
|
12 |
-
tools=[],
|
13 |
tools_folder="./tools",
|
14 |
model="gpt-4o",
|
15 |
temperature=0.3,
|
|
|
1 |
from agency_swarm.agents import Agent
|
2 |
+
from .tools.SendWhatsAppText import SendWhatsAppText
|
3 |
|
4 |
|
5 |
class TechnicalProjectManager(Agent):
|
|
|
10 |
instructions="./instructions.md",
|
11 |
files_folder="./files",
|
12 |
schemas_folder="./schemas",
|
13 |
+
tools=[SendWhatsAppText],
|
14 |
tools_folder="./tools",
|
15 |
model="gpt-4o",
|
16 |
temperature=0.3,
|
agency_ai_demo/agents/TechnicalProjectManager/tools/SendWhatsAppText.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
from agency_swarm.tools import BaseTool
|
4 |
+
from pydantic import Field
|
5 |
+
from typing import Dict, Any
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
# Load API credentials from environment variables
|
10 |
+
api_key = os.getenv("EVOLUTION_API_KEY")
|
11 |
+
api_url = os.getenv("EVOLUTION_API_URL")
|
12 |
+
api_instance = os.getenv("EVOLUTION_API_INSTANCE")
|
13 |
+
|
14 |
+
|
15 |
+
class SendWhatsAppText(BaseTool):
|
16 |
+
"""
|
17 |
+
Tool for sending WhatsApp text messages to users.
|
18 |
+
This tool sends a text message to a specified phone number using the Evolution API.
|
19 |
+
Use this when you need to request authorization, feedback, or additional information
|
20 |
+
from a human user to make progress on a task or project.
|
21 |
+
"""
|
22 |
+
|
23 |
+
# Add example_field with a default value to satisfy BaseTool validation
|
24 |
+
example_field: str = Field(
|
25 |
+
default="whatsapp_text",
|
26 |
+
description="Identifier for this tool. Can be left at its default value.",
|
27 |
+
)
|
28 |
+
|
29 |
+
phone_number: str = Field(
|
30 |
+
...,
|
31 |
+
description="Recipient's phone number in E.164 format (e.g., '5521988456100').",
|
32 |
+
)
|
33 |
+
|
34 |
+
message: str = Field(..., description="The text message content to send.")
|
35 |
+
|
36 |
+
def run(self) -> Dict[str, Any]:
|
37 |
+
"""
|
38 |
+
Send a WhatsApp text message to the specified phone number.
|
39 |
+
|
40 |
+
Returns:
|
41 |
+
dict: The JSON response from the API containing the message ID on success,
|
42 |
+
or error details on failure.
|
43 |
+
"""
|
44 |
+
import requests
|
45 |
+
|
46 |
+
# Build the complete URL
|
47 |
+
instance_name = os.getenv("EVOLUTION_API_INSTANCE", "Cogmo_Secretary_Joao")
|
48 |
+
url = f"{os.getenv('EVOLUTION_API_URL')}/message/sendText/{instance_name}"
|
49 |
+
|
50 |
+
# Set up the headers with API key authentication
|
51 |
+
headers = {
|
52 |
+
"Content-Type": "application/json",
|
53 |
+
"apikey": os.getenv("EVOLUTION_API_KEY"),
|
54 |
+
}
|
55 |
+
|
56 |
+
# Prepare the request body
|
57 |
+
data = {"number": self.phone_number, "text": self.message}
|
58 |
+
|
59 |
+
try:
|
60 |
+
# Make the POST request
|
61 |
+
response = requests.post(url, headers=headers, json=data)
|
62 |
+
|
63 |
+
# Return the JSON response
|
64 |
+
return response.json()
|
65 |
+
except Exception as e:
|
66 |
+
# Handle any exceptions
|
67 |
+
return {
|
68 |
+
"error": str(e),
|
69 |
+
"status": "failed",
|
70 |
+
"message": "Failed to send WhatsApp message",
|
71 |
+
}
|