LawsonE commited on
Commit
a37dce5
·
verified ·
1 Parent(s): ecbd41d

Updated calendar tool to support service accounts

Browse files
Files changed (1) hide show
  1. app.py +23 -41
app.py CHANGED
@@ -8,68 +8,50 @@ from tools.final_answer import FinalAnswerTool
8
  from Gradio_UI import GradioUI
9
 
10
  @tool
11
- def add_event_to_calendar(event_name:str, start:str, end:str)-> str: # it's important to specify the return type
12
- """A tool that allows you to create events on google calendar.
13
- Time should be represented in this format: 2015-05-28T09:00:00+utc. for now let utc be 01:00
14
- Args:
15
- event_name: The name of the event.
16
- start: The start time of the event represented as a datetime str.
17
- end: The end time of the event respresented as a datetime str.
18
  """
19
- import datetime
20
- import os.path
21
-
22
- from google.auth.transport.requests import Request
23
- from google.oauth2.credentials import Credentials
24
- from google_auth_oauthlib.flow import InstalledAppFlow
25
  from googleapiclient.discovery import build
26
  from googleapiclient.errors import HttpError
27
 
28
-
 
29
  SCOPES = ["https://www.googleapis.com/auth/calendar"]
30
-
31
- creds = None
32
- # The file token.json stores the user's access and refresh tokens, and is
33
- # created automatically when the authorization flow completes for the first
34
- # time.
35
- if os.path.exists("token.json"):
36
- creds = Credentials.from_authorized_user_file("token.json", SCOPES)
37
- # If there are no (valid) credentials available, let the user log in.
38
- if not creds or not creds.valid:
39
- if creds and creds.expired and creds.refresh_token:
40
- creds.refresh(Request())
41
- else:
42
- flow = InstalledAppFlow.from_client_secrets_file(
43
- "credentials.json", SCOPES
44
- )
45
- creds = flow.run_local_server(port=0)
46
- # Save the credentials for the next run
47
- with open("token.json", "w") as token:
48
- token.write(creds.to_json())
49
 
50
  try:
 
 
 
 
 
51
  service = build("calendar", "v3", credentials=creds)
52
 
53
- # Call the Calendar API
54
  event = {
55
  'summary': event_name,
56
  'start': {
57
  'dateTime': start,
58
- 'timeZone': '', # Africa/Lagos
59
  },
60
-
61
  'end': {
62
  'dateTime': end,
63
- 'timeZone': '',
64
  },
65
  }
66
 
67
- event = service.events().insert(calendarId='primary', body=event).execute()
 
 
 
68
 
69
- except HttpError as error:
70
- print(f"An error occurred: {error}")
71
- return f"Event '{event_name}' has been added successfully to calendar✅"
72
 
 
 
 
 
73
 
74
  @tool
75
  def get_current_time_in_timezone(timezone: str) -> str:
 
8
  from Gradio_UI import GradioUI
9
 
10
  @tool
11
+ def add_event_to_calendar(event_name: str, start: str, end: str) -> str:
12
+ """A tool that creates events on Google Calendar using a service account.
13
+ Time format: 2015-05-28T09:00:00+01:00 (WAT = UTC+1)
 
 
 
 
14
  """
15
+ from google.oauth2 import service_account
 
 
 
 
 
16
  from googleapiclient.discovery import build
17
  from googleapiclient.errors import HttpError
18
 
19
+ # Service account configuration
20
+ SERVICE_ACCOUNT_FILE = "credentials.json" # Your service account JSON file
21
  SCOPES = ["https://www.googleapis.com/auth/calendar"]
22
+ CALENDAR_ID = "primary" # Or your specific calendar ID
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  try:
25
+ # Authenticate with service account
26
+ creds = service_account.Credentials.from_service_account_file(
27
+ SERVICE_ACCOUNT_FILE, scopes=SCOPES
28
+ )
29
+
30
  service = build("calendar", "v3", credentials=creds)
31
 
 
32
  event = {
33
  'summary': event_name,
34
  'start': {
35
  'dateTime': start,
36
+ 'timeZone': 'Africa/Lagos', # Fixed timezone
37
  },
 
38
  'end': {
39
  'dateTime': end,
40
+ 'timeZone': 'Africa/Lagos',
41
  },
42
  }
43
 
44
+ event = service.events().insert(
45
+ calendarId=CALENDAR_ID,
46
+ body=event
47
+ ).execute()
48
 
49
+ return f"Event '{event_name}' added successfully! 🎉"
 
 
50
 
51
+ except HttpError as error:
52
+ return f"❌ Error: {error}"
53
+ except Exception as e:
54
+ return f"❌ Unexpected error: {str(e)}"
55
 
56
  @tool
57
  def get_current_time_in_timezone(timezone: str) -> str: