Spaces:
Sleeping
Sleeping
Update comunicacion_gmail.py
Browse files- comunicacion_gmail.py +107 -78
comunicacion_gmail.py
CHANGED
@@ -1,98 +1,127 @@
|
|
|
|
1 |
from googleapiclient.discovery import build
|
2 |
-
from google_auth_oauthlib.flow import InstalledAppFlow
|
3 |
from google.auth.transport.requests import Request
|
|
|
4 |
from email.mime.text import MIMEText
|
5 |
import pickle
|
6 |
import os.path
|
7 |
import base64
|
|
|
8 |
|
9 |
-
# Define los 谩mbitos que necesitas (ajusta seg煤n tus necesidades)
|
10 |
-
SCOPES = ['https://mail.google.com/', 'https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/gmail.send', 'https://www.googleapis.com/auth/gmail.modify']
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
def gmail_tool(accion, parametros={}):
|
14 |
"""Interact煤a con la API de Gmail."""
|
15 |
-
|
|
|
|
|
|
|
16 |
|
17 |
-
if os.path.exists('token.pickle'):
|
18 |
-
with open('token.pickle', 'rb') as token:
|
19 |
-
creds = pickle.load(token)
|
20 |
|
21 |
-
if
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
creds.refresh(Request())
|
24 |
-
else:
|
25 |
-
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
|
26 |
-
# Obtener URL de autorizaci贸n
|
27 |
-
auth_url, _ = flow.authorization_url(prompt='consent')
|
28 |
-
return {"auth_url": auth_url, "requires_auth": True} # Indicar que se requiere autorizaci贸n
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
try:
|
34 |
-
if accion == "leer_correos":
|
35 |
-
# ... (implementaci贸n para leer correos) ...
|
36 |
-
results = service.users().messages().list(userId='me', maxResults=parametros.get("maxResults", 10)).execute() # Usa parametros para maxResults
|
37 |
-
messages = results.get('messages', [])
|
38 |
-
result["messages"] = [] # Inicializa una lista para almacenar los mensajes
|
39 |
-
for message in messages:
|
40 |
-
msg = service.users().messages().get(userId='me', id=message['id']).execute()
|
41 |
-
# Decodificar el mensaje (puede ser multipart)
|
42 |
-
payload = msg['payload']
|
43 |
-
parts = payload.get('parts', []) # Verificar si hay partes
|
44 |
-
body = ""
|
45 |
-
if parts:
|
46 |
-
for part in parts:
|
47 |
-
if part.get('mimeType') == 'text/plain':
|
48 |
-
data = part['body'].get('data', '')
|
49 |
-
body += base64.urlsafe_b64decode(data).decode()
|
50 |
-
|
51 |
-
else: # Si no hay partes, el cuerpo est谩 en payload['body']
|
52 |
-
data = payload['body'].get('data','')
|
53 |
-
body+= base64.urlsafe_b64decode(data).decode()
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
create_message = {'raw': base64.urlsafe_b64encode(message.as_bytes()).decode()}
|
71 |
-
|
72 |
-
send_message = service.users().messages().send(userId='me', body=create_message).execute()
|
73 |
-
|
74 |
-
print(F'Message Id: {send_message["id"]}')
|
75 |
-
result["message_id"] = send_message["id"] # Almacenar el ID
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
# ... (Integraci贸n con el prompt del sistema)
|
|
|
1 |
+
from google.oauth2.credentials import Credentials
|
2 |
from googleapiclient.discovery import build
|
|
|
3 |
from google.auth.transport.requests import Request
|
4 |
+
from google_auth_oauthlib.flow import InstalledAppFlow
|
5 |
from email.mime.text import MIMEText
|
6 |
import pickle
|
7 |
import os.path
|
8 |
import base64
|
9 |
+
import json
|
10 |
|
|
|
|
|
11 |
|
12 |
+
# Define los 谩mbitos que necesitas (ajusta seg煤n tus necesidades)
|
13 |
+
SCOPES = [
|
14 |
+
'https://mail.google.com/', # Acceso completo a Gmail (preferido)
|
15 |
+
'https://www.googleapis.com/auth/gmail.metadata', # Necesario para leer metadatos (etiquetas, etc.)
|
16 |
+
'https://www.googleapis.com/auth/gmail.labels', # Para gestionar etiquetas
|
17 |
+
'https://www.googleapis.com/auth/gmail.addons.current.message.metadata', # Metadatos al ejecutar complemento
|
18 |
+
'https://www.googleapis.com/auth/gmail.addons.current.message.readonly', # Lectura al ejecutar complemento
|
19 |
+
'https://www.googleapis.com/auth/gmail.addons.current.action.compose', # Redactar con complemento
|
20 |
+
'https://www.googleapis.com/auth/gmail.settings.basic', # Configuraci贸n b谩sica de Gmail
|
21 |
+
'https://www.googleapis.com/auth/gmail.settings.sharing', # Delegaci贸n de acceso (si es necesario)
|
22 |
+
'https://www.googleapis.com/auth/drive', # Acceso completo a Drive
|
23 |
+
'https://www.googleapis.com/auth/drive.metadata.readonly', # Metadatos de Drive (lectura). Usa 'drive.metadata' para lectura/escritura.
|
24 |
+
'https://www.googleapis.com/auth/drive.appdata', # Datos de la app en Drive
|
25 |
+
'https://www.googleapis.com/auth/drive.install', # Conexi贸n a Drive
|
26 |
+
'https://www.googleapis.com/auth/drive.file', # Para archivos creados o abiertos por la app
|
27 |
+
'https://www.googleapis.com/auth/activity' # leer el activity de gmail
|
28 |
+
]
|
29 |
|
30 |
def gmail_tool(accion, parametros={}):
|
31 |
"""Interact煤a con la API de Gmail."""
|
32 |
+
# Obtener credenciales desde secrets del Space
|
33 |
+
client_id = os.environ.get("GMAIL_CLIENT_ID")
|
34 |
+
client_secret = os.environ.get("GMAIL_CLIENT_SECRET")
|
35 |
+
refresh_token = os.environ.get("GMAIL_REFRESH_TOKEN")
|
36 |
|
|
|
|
|
|
|
37 |
|
38 |
+
if client_id and client_secret and refresh_token:
|
39 |
+
try:
|
40 |
+
creds = Credentials(
|
41 |
+
token=None,
|
42 |
+
refresh_token=refresh_token,
|
43 |
+
token_uri="https://oauth2.googleapis.com/token",
|
44 |
+
client_id=client_id,
|
45 |
+
client_secret=client_secret,
|
46 |
+
scopes=SCOPES
|
47 |
+
)
|
48 |
creds.refresh(Request())
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
|
51 |
+
service = build('gmail', 'v1', credentials=creds)
|
52 |
+
result = {} # Inicializar result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
try:
|
55 |
+
if accion == "leer_correos":
|
56 |
+
# ... (implementaci贸n para leer correos) ...
|
57 |
+
results = service.users().messages().list(userId='me', maxResults=parametros.get("maxResults", 10)).execute() # Usa parametros para maxResults
|
58 |
+
messages = results.get('messages', [])
|
59 |
+
result["messages"] = [] # Inicializa una lista para almacenar los mensajes
|
60 |
+
for message in messages:
|
61 |
+
try:
|
62 |
+
msg = service.users().messages().get(userId='me', id=message['id']).execute()
|
63 |
+
# Decodificar el mensaje (puede ser multipart)
|
64 |
+
payload = msg['payload']
|
65 |
+
parts = payload.get('parts', []) # Verificar si hay partes
|
66 |
+
body = ""
|
67 |
+
if parts:
|
68 |
+
for part in parts:
|
69 |
+
if part.get('mimeType') == 'text/plain':
|
70 |
+
data = part['body'].get('data', '')
|
71 |
+
body += base64.urlsafe_b64decode(data).decode()
|
72 |
+
|
73 |
+
else: # Si no hay partes, el cuerpo est谩 en payload['body']
|
74 |
+
data = payload['body'].get('data','')
|
75 |
+
body+= base64.urlsafe_b64decode(data).decode()
|
76 |
+
|
77 |
+
print(body)
|
78 |
+
result["messages"].append({"body": body, "id": message['id']}) # Asocia el ID al mensaje
|
79 |
+
except Exception as e:
|
80 |
+
print(f"Error al leer el mensaje {message['id']}: {e}")
|
81 |
+
result["messages"].append({"error": str(e), "id": message['id']}) # Almacena el error para ese mensaje
|
82 |
+
|
83 |
+
|
84 |
+
elif accion == "enviar_correo":
|
85 |
+
# ... (implementaci贸n para enviar correo) ...
|
86 |
+
destinatario = parametros.get("destinatario")
|
87 |
+
asunto = parametros.get("asunto")
|
88 |
+
cuerpo_correo = parametros.get("cuerpo")
|
89 |
|
90 |
+
message = MIMEText(cuerpo_correo)
|
91 |
+
message['to'] = destinatario
|
92 |
+
message['from'] = '[email protected]' #!CAMBIAR TU CORREO
|
93 |
+
message['subject'] = asunto
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
+
create_message = {'raw': base64.urlsafe_b64encode(message.as_bytes()).decode()}
|
96 |
+
|
97 |
+
send_message = service.users().messages().send(userId='me', body=create_message).execute()
|
98 |
+
|
99 |
+
print(F'Message Id: {send_message["id"]}')
|
100 |
+
result["message_id"] = send_message["id"] # Almacenar el ID
|
101 |
+
|
102 |
+
elif accion == "verificar_almacenamiento":
|
103 |
+
try:
|
104 |
+
drive_service = build('drive', 'v3', credentials=creds)
|
105 |
+
about = drive_service.about().get(fields="storageQuota").execute()
|
106 |
+
result = about.get('storageQuota', {}) # Asigna directamente a result.get y maneja si no existe storageQuota
|
107 |
+
return result
|
108 |
+
|
109 |
+
except Exception as e:
|
110 |
+
print(f"Error al verificar el almacenamiento: {e}")
|
111 |
+
return {"error": "Error al verificar el almacenamiento: " + str(e)}
|
112 |
+
|
113 |
+
|
114 |
+
return result # Retorna el resultado al final del try...except
|
115 |
+
# ... (otras acciones)
|
116 |
+
|
117 |
+
except Exception as e: # Captura excepciones de la API de Gmail/Drive
|
118 |
+
print(f"Error en gmail_tool (acciones): {e}")
|
119 |
+
return {"error": str(e)}
|
120 |
+
|
121 |
+
except Exception as e: # Excepci贸n para la creaci贸n de credenciales
|
122 |
+
print(f"Error al crear credenciales: {e}")
|
123 |
+
return {"error": str(e)}
|
124 |
+
else:
|
125 |
+
return {"error": "Credenciales incompletas."}
|
126 |
|
127 |
# ... (Integraci贸n con el prompt del sistema)
|