-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_integrations.py
More file actions
223 lines (194 loc) · 10.2 KB
/
api_integrations.py
File metadata and controls
223 lines (194 loc) · 10.2 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# api_integrations.py
import os
import pickle
from datetime import datetime, timedelta, UTC
import json
from flask import current_app
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from logging_config import get_logger
logger = get_logger(__name__)
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
import requests
from services.contact_service_refactored import ContactService
from urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
SCOPES = [
'https://www.googleapis.com/auth/gmail.readonly',
'https://www.googleapis.com/auth/calendar'
]
TOKEN_FILE = 'token.pickle'
def get_google_creds():
creds = None
if os.path.exists(TOKEN_FILE):
with open(TOKEN_FILE, 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
try:
creds.refresh(Request())
except Exception as e:
logger.error("Error refreshing Google token", error=str(e))
logger.error("Please delete token.pickle and re-run the application.")
return None
else:
try:
client_id = current_app.config.get('GOOGLE_CLIENT_ID')
client_secret = current_app.config.get('GOOGLE_CLIENT_SECRET')
if not client_id or not client_secret:
logger.critical("="*80)
logger.critical("FATAL OAUTH ERROR: 'GOOGLE_CLIENT_ID' or 'GOOGLE_CLIENT_SECRET' is missing.")
logger.critical("ACTION: Please ensure these variables are correctly set in your .env file.")
logger.critical("="*80)
return None
client_config = {
"installed": {
"client_id": client_id,
"project_id": current_app.config.get('GOOGLE_PROJECT_ID'),
"client_secret": client_secret,
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"redirect_uris": ["http://localhost:8989/"]
}
}
flow = InstalledAppFlow.from_client_config(client_config, SCOPES)
creds = flow.run_local_server(port=8989)
except Exception as e:
logger.critical("FATAL: Could not get new Google credentials", error=str(e))
logger.critical("ACTION: Please verify your .env file and Google Cloud project settings.")
return None
with open(TOKEN_FILE, 'wb') as token:
pickle.dump(creds, token)
return creds
def get_upcoming_calendar_events(count=5):
creds = get_google_creds()
if not creds: return []
try:
service = build('calendar', 'v3', credentials=creds)
now = datetime.now(UTC).isoformat()
events_result = service.events().list(calendarId='primary', timeMin=now, maxResults=count, singleEvents=True, orderBy='startTime').execute()
return events_result.get('items', [])
except Exception as e:
logger.error("Error fetching Google Calendar events", error=str(e))
return []
def get_recent_gmail_messages(count=5):
creds = get_google_creds()
if not creds: return []
try:
service = build('gmail', 'v1', credentials=creds)
results = service.users().messages().list(userId='me', labelIds=['INBOX', 'UNREAD'], maxResults=count).execute()
messages_info = results.get('messages', [])
emails = []
for msg_info in messages_info:
msg = service.users().messages().get(userId='me', id=msg_info['id'], format='metadata', metadataHeaders=['From', 'Subject']).execute()
headers = msg['payload']['headers']
subject = next((h['value'] for h in headers if h['name'] == 'Subject'), 'No Subject')
sender = next((h['value'] for h in headers if h['name'] == 'From'), 'Unknown Sender')
emails.append({'id': msg['id'], 'threadId': msg['threadId'], 'subject': subject, 'sender': sender})
return emails
except Exception as e:
logger.error("Error fetching Gmail messages", error=str(e))
return []
def get_emails_for_contact(email_address, count=5):
if not email_address:
return []
creds = get_google_creds()
if not creds: return []
try:
service = build('gmail', 'v1', credentials=creds)
query = f"from:{email_address} OR to:{email_address}"
results = service.users().messages().list(userId='me', q=query, maxResults=count).execute()
messages_info = results.get('messages', [])
emails = []
for msg_info in messages_info:
msg = service.users().messages().get(userId='me', id=msg_info['id'], format='metadata', metadataHeaders=['From', 'Subject', 'Date']).execute()
headers = msg['payload']['headers']
subject = next((h['value'] for h in headers if h['name'] == 'Subject'), 'No Subject')
sender = next((h['value'] for h in headers if h['name'] == 'From'), 'Unknown Sender')
date = next((h['value'] for h in headers if h['name'] == 'Date'), '')
emails.append({'subject': subject, 'sender': sender, 'date': date})
return emails
except Exception as e:
logger.error("Error fetching emails for contact", email_address=email_address, error=str(e))
return []
def get_recent_openphone_texts(contact_service: ContactService, count=5):
try:
api_key = current_app.config.get('OPENPHONE_API_KEY')
phone_number_id = current_app.config.get('OPENPHONE_PHONE_NUMBER_ID')
if not api_key or not phone_number_id:
return ([], "OpenPhone API Key or Phone Number ID is not configured.")
# The header is now in the correct format, without "Bearer ".
headers = {"Authorization": api_key}
conversations_url = f"https://api.openphone.com/v1/conversations?phoneNumberId={phone_number_id}&limit={count}"
conversation_response = requests.get(conversations_url, headers=headers, verify=True, timeout=(5, 30))
conversation_response.raise_for_status()
conversations = conversation_response.json().get('data', [])
processed_conversations = []
for convo in conversations:
last_activity_type = convo.get('lastActivityType')
latest_message_body = ""
if last_activity_type == 'message':
last_activity_id = convo.get('lastActivityId')
message_url = f"https://api.openphone.com/v1/messages/{last_activity_id}"
message_response = requests.get(message_url, headers=headers, verify=True, timeout=(5, 30))
if message_response.status_code == 200:
message_data = message_response.json()
latest_message_body = message_data.get('data', {}).get('text', "[Message with no body]")
else:
latest_message_body = f"[Error fetching message: {message_response.status_code}]"
elif last_activity_type == 'call':
latest_message_body = "[Last activity was a phone call]"
else:
latest_message_body = f"[Last activity: {last_activity_type or 'Unknown'}]"
other_participant_number = next((p for p in convo.get('participants', []) if p != current_app.config.get('OPENPHONE_PHONE_NUMBER')), None)
contact = contact_service.get_contact_by_phone(other_participant_number) if other_participant_number else None
processed_conversations.append({
'contact_id': contact.id if contact else None,
'contact_name': convo.get('name') or (contact.first_name if contact else None),
'contact_number': other_participant_number,
'latest_message_body': latest_message_body
})
return (processed_conversations, None)
except Exception as e:
# We can leave the detailed exception logging for now, it's useful.
logger.debug("An exception occurred in get_recent_openphone_texts",
exception_type=type(e).__name__,
exception_details=str(e))
return ([], str(e))
def create_google_calendar_event(title, description, start_time, end_time, attendees: list, location: str = None):
creds = get_google_creds()
if not creds:
logger.error("Could not create Google Calendar event: invalid credentials.")
return None
try:
service = build('calendar', 'v3', credentials=creds)
event = {
'summary': title,
'description': description,
'start': {'dateTime': start_time.isoformat(), 'timeZone': 'America/New_York'},
'end': {'dateTime': end_time.isoformat(), 'timeZone': 'America/New_York'},
'attendees': [{'email': email} for email in attendees],
}
if location:
event['location'] = location
created_event = service.events().insert(calendarId='primary', body=event).execute()
logger.info("Google Calendar event created", event_link=created_event.get('htmlLink'))
return created_event
except Exception as e:
logger.error("Error creating Google Calendar event", error=str(e))
return None
def delete_google_calendar_event(event_id: str):
creds = get_google_creds()
if not creds:
logger.error("Could not delete Google Calendar event: invalid credentials.")
return False
try:
service = build('calendar', 'v3', credentials=creds)
service.events().delete(calendarId='primary', eventId=event_id).execute()
logger.info("Successfully deleted Google Calendar event", event_id=event_id)
return True
except Exception as e:
logger.error("Error deleting Google Calendar event", event_id=event_id, error=str(e))
return False