Repository navigation
Expand file tree
/
Copy pathtools.py
More file actions
112 lines (99 loc) · 3.74 KB
/
Copy pathtools.py
File metadata and controls
112 lines (99 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import logging
from livekit.agents import function_tool, RunContext
import requests
from langchain_community.tools import DuckDuckGoSearchRun
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from typing import Optional
@function_tool()
async def get_weather(
context: RunContext, # type: ignore
city: str) -> str:
"""
Get the current weather for a given city.
"""
try:
response = requests.get(
f"https://wttr.in/{city}?format=3")
if response.status_code == 200:
logging.info(f"Weather for {city}: {response.text.strip()}")
return response.text.strip()
else:
logging.error(f"Failed to get weather for {city}: {response.status_code}")
return f"Could not retrieve weather for {city}."
except Exception as e:
logging.error(f"Error retrieving weather for {city}: {e}")
return f"An error occurred while retrieving weather for {city}."
@function_tool()
async def search_web(
context: RunContext, # type: ignore
query: str) -> str:
"""
Search the web using DuckDuckGo.
"""
try:
results = DuckDuckGoSearchRun().run(tool_input=query)
logging.info(f"Search results for '{query}': {results}")
return results
except Exception as e:
logging.error(f"Error searching the web for '{query}': {e}")
return f"An error occurred while searching the web for '{query}'."
@function_tool()
async def send_email(
context: RunContext, # type: ignore
to_email: str,
subject: str,
message: str,
cc_email: Optional[str] = None
) -> str:
"""
Send an email through Gmail.
Args:
to_email: Recipient email address
subject: Email subject line
message: Email body content
cc_email: Optional CC email address
"""
try:
# Gmail SMTP configuration
smtp_server = "smtp.gmail.com"
smtp_port = 587
# Get credentials from environment variables
gmail_user = os.getenv("GMAIL_USER")
gmail_password = os.getenv("GMAIL_APP_PASSWORD") # Use App Password, not regular password
if not gmail_user or not gmail_password:
logging.error("Gmail credentials not found in environment variables")
return "Email sending failed: Gmail credentials not configured."
# Create message
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = to_email
msg['Subject'] = subject
# Add CC if provided
recipients = [to_email]
if cc_email:
msg['Cc'] = cc_email
recipients.append(cc_email)
# Attach message body
msg.attach(MIMEText(message, 'plain'))
# Connect to Gmail SMTP server
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls() # Enable TLS encryption
server.login(gmail_user, gmail_password)
# Send email
text = msg.as_string()
server.sendmail(gmail_user, recipients, text)
server.quit()
logging.info(f"Email sent successfully to {to_email}")
return f"Email sent successfully to {to_email}"
except smtplib.SMTPAuthenticationError:
logging.error("Gmail authentication failed")
return "Email sending failed: Authentication error. Please check your Gmail credentials."
except smtplib.SMTPException as e:
logging.error(f"SMTP error occurred: {e}")
return f"Email sending failed: SMTP error - {str(e)}"
except Exception as e:
logging.error(f"Error sending email: {e}")
return f"An error occurred while sending email: {str(e)}"