Spaces:
Sleeping
Sleeping
Update tool.py
Browse files
tool.py
CHANGED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from google.oauth2.credentials import Credentials
|
| 3 |
+
from googleapiclient.discovery import build
|
| 4 |
+
from smolagents import tool # HF agents framework
|
| 5 |
+
|
| 6 |
+
def get_gmail_service():
|
| 7 |
+
creds = Credentials(
|
| 8 |
+
token=None,
|
| 9 |
+
refresh_token=os.environ["GMAIL_REFRESH_TOKEN"],
|
| 10 |
+
client_id=os.environ["GMAIL_CLIENT_ID"],
|
| 11 |
+
client_secret=os.environ["GMAIL_CLIENT_SECRET"],
|
| 12 |
+
token_uri="https://oauth2.googleapis.com/token",
|
| 13 |
+
)
|
| 14 |
+
return build("gmail", "v1", credentials=creds)
|
| 15 |
+
|
| 16 |
+
@tool
|
| 17 |
+
def read_latest_emails(max_results: int = 5) -> str:
|
| 18 |
+
"""Reads the latest emails from Gmail inbox.
|
| 19 |
+
|
| 20 |
+
Args:
|
| 21 |
+
max_results: Number of emails to fetch (default 5)
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
A summary of recent emails
|
| 25 |
+
"""
|
| 26 |
+
service = get_gmail_service()
|
| 27 |
+
results = service.users().messages().list(
|
| 28 |
+
userId="me", maxResults=max_results, labelIds=["INBOX"]
|
| 29 |
+
).execute()
|
| 30 |
+
|
| 31 |
+
messages = results.get("messages", [])
|
| 32 |
+
summaries = []
|
| 33 |
+
|
| 34 |
+
for msg in messages:
|
| 35 |
+
detail = service.users().messages().get(
|
| 36 |
+
userId="me", id=msg["id"], format="metadata",
|
| 37 |
+
metadataHeaders=["From", "Subject", "Date"]
|
| 38 |
+
).execute()
|
| 39 |
+
headers = {h["name"]: h["value"] for h in detail["payload"]["headers"]}
|
| 40 |
+
summaries.append(
|
| 41 |
+
f"From: {headers.get('From')}\n"
|
| 42 |
+
f"Subject: {headers.get('Subject')}\n"
|
| 43 |
+
f"Date: {headers.get('Date')}"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
return "\n\n".join(summaries)
|
| 47 |
+
|
| 48 |
+
@tool
|
| 49 |
+
def send_email(to: str, subject: str, body: str) -> str:
|
| 50 |
+
"""Sends an email via Gmail.
|
| 51 |
+
|
| 52 |
+
Args:
|
| 53 |
+
to: Recipient email address
|
| 54 |
+
subject: Email subject
|
| 55 |
+
body: Email body text
|
| 56 |
+
|
| 57 |
+
Returns:
|
| 58 |
+
Success or error message
|
| 59 |
+
"""
|
| 60 |
+
import base64
|
| 61 |
+
from email.mime.text import MIMEText
|
| 62 |
+
|
| 63 |
+
service = get_gmail_service()
|
| 64 |
+
message = MIMEText(body)
|
| 65 |
+
message["to"] = to
|
| 66 |
+
message["subject"] = subject
|
| 67 |
+
|
| 68 |
+
raw = base64.urlsafe_b64encode(message.as_bytes()).decode()
|
| 69 |
+
service.users().messages().send(
|
| 70 |
+
userId="me", body={"raw": raw}
|
| 71 |
+
).execute()
|
| 72 |
+
|
| 73 |
+
return f"Email sent to {to} successfully!"
|