DigitalTwin / tools.py
Shekarss's picture
Update tools.py
aeb63f1 verified
import requests
make_a_mail_json = {
"type": "function",
"function": {
"name": "make_a_mail",
"description": "Send an email to the site owner and an auto-reply to the user with their details.",
"parameters": {
"type": "object",
"properties": {
"email": {
"type": "string",
"description": "The user's email address"
},
"name": {
"type": "string",
"description": "The user's name"
},
"notes": {
"type": "string",
"description": "Optional extra context about the conversation or the user's message"
}
},
"required": ["email", "name"],
"additionalProperties": False
}
}
}
def make_a_mail(email, name, notes="not provided"):
"""
Records user details by sending emails through EmailJS API.
This tool takes email, name and notes(optional) and sends email to the host and user.
Args:
email (str): The email address of the user
name (str): The user's name
notes (str, optional): Additional information about the conversation
Returns:
dict: Response status and message(format it and show succeful message to the user.)
"""
# EmailJS API endpoint
api_url = "https://api.emailjs.com/api/v1.0/email/send"
# Your EmailJS configuration
service_id = "service_w38t77o"
owner_template_id = "template_dsfuqkm"
user_template_id = "template_rdki9pa"
public_key = "0__in7VreG-e8M7PL"
# Prepare the data for the first email (to site owner)
owner_email_data = {
"service_id": service_id,
"template_id": owner_template_id,
"user_id": public_key,
"template_params": {
"from_name": name,
"reply_to": email,
"message": notes
}
}
# Prepare the data for the auto-reply email (to user)
user_email_data = {
"service_id": service_id,
"template_id": user_template_id,
"user_id": public_key,
"template_params": {
"to_email": email,
"to_name": name,
"from_name": "Shekar",
"message": f"Thank you for your interest! We'll be in touch soon.\n\nYour message: {notes}"
}
}
try:
# Send email to site owner
owner_response = requests.post(
api_url,
json=owner_email_data,
headers={"Content-Type": "application/json", "origin": "https://yourwebsite.com"}
)
# Check if the first email was sent successfully
if owner_response.status_code != 200:
return {"status": "error", "message": f"Failed to send email to site owner: {owner_response.text}"}
# Send auto-reply to user
user_response = requests.post(
api_url,
json=user_email_data,
headers={"Content-Type": "application/json", "origin": "https://yourwebsite.com"}
)
# Check if the auto-reply was sent successfully
if user_response.status_code != 200:
return {"status": "partial_success", "message": f"Sent to owner but auto-reply failed: {user_response.text}"}
return {"status": "success", "message": "Both emails sent successfully"}
except requests.exceptions.RequestException as e:
return {"status": "error", "message": f"Network error: {str(e)}"}
except Exception as e:
return {"status": "error", "message": f"Unexpected error: {str(e)}"}