After you installed this package and get the Google credentials file, the next step is to
import the package into your code and start using the functions.
First, let's instantiate the plugin and define the calendar that will be used. By default we will use the main calendar, identified as "primary". To use a different calendar you need to specify its id. Go to calendar's settings and sharing -> Integrate calendar -> Calendar ID.
# Set the path of the credentials json filecredentials="resources/credentials.json"# Instantiate the plugingooglecalendar=BotGoogleCalendarPlugin(credentials)
With the plugin configured, let's look for some events and access some information.
# Get upcoming calendar eventsevents=googlecalendar.get_events()# For each event: prints the participants, description and start date.forevinevents:print(ev.attendees)print(ev.description)print(ev.start)
Using just get events method we'll get the events that haven't happened yet. To return old events or return events up to a specific date, we can use the information of date min and date max parameters as a filter.
fromdatetimeimportdatetime# Dates that will be used as a reference to return eventsfirst_date=datetime(2022,4,10)last_date=datetime(2022,5,10)# Only events that are between 10/04/2022 and 10/05/2022 will be consideredevents=googlecalendar.get_events(date_min=first_date,date_max=last_date)# For each event: prints the creator, summary and end date.forevinevents:print(ev.creator)print(ev.summary)print(ev.end)
Now, let's create a new simple event with some basic information.
fromdatetimeimportdatetime# Creating a 4-hour event starting on 12/5/2022 at 10 am.googlecalendar.create_event(title="Test Event",description="My event",start_date=datetime(2022,5,12,10),end_date=datetime(2022,5,12,14))# Creating a event starting on 20/5/2022 at 18 am and containing two participants.# As an end date was not provided, the event will last for 1 hour by default.googlecalendar.create_event(title="Test Event 2",description="My second event",start_date=datetime(2022,5,20,18),attendees=["participant_1@gmail.com","participant_2@gmail.com"])
We can also create events that repeat over a period of time. To define a recurrence, you can select a value defined as an Enum in the EventRecurrence class. Just use EventRecurrence.PERIOD to select a time period.
fromdatetimeimportdatetimefrombotcity.plugins.googlecalendarimportBotGoogleCalendarPlugin,EventRecurrence# Creating an event that repeats daily starting 8/5/2022 until 14/5/2022googlecalendar.create_recurring_event(title="Recurring event",description="This event is repeated daily",start_date=datetime(2022,5,8,10),recurrence=EventRecurrence.DAILY,recurrence_until_date=datetime(2022,5,14))
It is also possible to create recurring events on specific days. You can use the values defined as an Enum in the EventDays class. Just use EventDays.DAY to select a specific day of the week.
fromdatetimeimportdatetimefrombotcity.plugins.googlecalendarimportBotGoogleCalendarPlugin,EventRecurrence,EventDays# Creating an event every two Fridaysgooglecalendar.create_recurring_event(title="Friday event",description="This event is repeated every 2 Fridays",start_date=datetime(2022,5,13,10),recurrence=EventRecurrence.DAILY,recurrence_freq=2,recurrence_days=[EventDays.FRIDAY])