Skip to content

Google - Calendar

Interact and perform various operations through a Google account. Get events, create events, update events, delete events, and more.

Installation

pip install botcity-googlecalendar-plugin

Importing the Plugin

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.

from botcity.plugins.googlecalendar import BotGoogleCalendarPlugin

Setting up the Google Calendar

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 file
credentials = "resources/credentials.json"

# Instantiate the plugin
googlecalendar = BotGoogleCalendarPlugin(credentials)

Getting some events

With the plugin configured, let's look for some events and access some information.

# Get upcoming calendar events
events = googlecalendar.get_events()

# For each event: prints the participants, description and start date.
for ev in events:
    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.

from datetime import datetime

# Dates that will be used as a reference to return events
first_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 considered
events = googlecalendar.get_events(date_min=first_date, date_max=last_date)

# For each event: prints the creator, summary and end date.
for ev in events:
    print(ev.creator)
    print(ev.summary)
    print(ev.end)

Creating single events

Now, let's create a new simple event with some basic information.

from datetime import datetime

# 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"]
)

Creating recurrent events

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.

from datetime import datetime
from botcity.plugins.googlecalendar import BotGoogleCalendarPlugin, EventRecurrence

# Creating an event that repeats daily starting 8/5/2022 until 14/5/2022
googlecalendar.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.

from datetime import datetime
from botcity.plugins.googlecalendar import BotGoogleCalendarPlugin, EventRecurrence, EventDays

# Creating an event every two Fridays
googlecalendar.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]
)