LawsonE commited on
Commit
eaca291
·
verified ·
1 Parent(s): ae7a494

Created a tool to allow the agent to add events to the user's calendar.

Browse files
Files changed (1) hide show
  1. app.py +60 -7
app.py CHANGED
@@ -7,16 +7,69 @@ from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
 
10
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
15
  Args:
16
- arg1: the first argument
17
- arg2: the second argument
 
18
  """
19
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
 
7
 
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-00:00
14
  Args:
15
+ event: 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: