How to send notifications for calendar events using the Nylas Python SDK

How to send notifications for calendar events using the Nylas Python SDK

6 min read

Creating calendar events is important, but having people attend those events is even more important. And most of the time, people just forget about the events because they are not reminded of them. Events notifications can become your best ally.

The Nylas Python SDK offers three ways of reminding people about important events by sending events notifications, and in this blog post, we’re going to review them one by one.

If you feel more comfortable using Ruby, we have you covered How to send notifications for calendar events using the Nylas Ruby SDK.

Is your system ready?

If you already have the Nylas Python SDK installed and your environment is configured, then process with the blog, otherwise, I would recommend you to read the post How to Send Emails with the Nylas Python SDK where everything is clearly explained.

Why are reminders important?

When we first create an event, the participant or participants will receive an email that they can accept or reject in order to let the organizer know that they are planning to attend the event. But it doesn’t guarantee that they will remember to attend the event. Sending a reminder is a great way to at least make sure that the participant will not forget about the event.

Event notification using email

This is the most simple method that we can use to send an event reminder. So let’s grab our favorite code editor and create a file called EmailReminderEvent.py

Keep in mind that if we run this script after 8:30am it’s not going to work. Add a couple of hours to your current time before running it.

# Load your env variables
from dotenv import load_dotenv
load_dotenv()

# Import your dependencies
import os
import datetime
from datetime import date
from nylas import APIClient

# Initialize your Nylas API client
nylas = APIClient(
    os.environ.get("CLIENT_ID"),
    os.environ.get("CLIENT_SECRET"),
    os.environ.get("ACCESS_TOKEN")
)

# Get today’s date
today = date.today()
# Today’s date at 7:15:00 am
_START = int(datetime.datetime(today.year, today.month, today.day, 7, 15, 0).strftime("%s"))
# Today’s date at 8:30:00 am
_END = int(datetime.datetime(today.year, today.month, today.day, 8, 30, 0).strftime("%s"))

# Draft the event
event = nylas.events.create()

# Define the event characteristics
event.title = "Let's learn some Nylas Python API!"
event.location = "Blag's Den!"
event.when = {"start_time": START_TIME, "end_time": END_TIME}
event.participants = [{"name": "<YOUR_NAME>", 'email': os.environ.get("RECIPIENT_ADDRESS")}]
# It’s imperative to define the Calendar Id
event.calendar_id = os.environ.get("CALENDAR_ID")
# Email Reminder
#We can add a reminder notification to the event invitation itself that will be sent to all participants
event.notifications = [{"body": "Heads up! The Nylas Python Workshop is starting in 10 minutes. See you there!", \
                        "minutes_before_event": 10, "subject": "Nylas Python Workshop starts soon", "type": "email"}]

# Create the event and send an email notification
event.save(notify_participants=True)
if event.id:
	print("Event created successfully")
else:
	print("There was an error creating the event")

We can execute this script from the terminal by typing:

$ python3 EmailReminderEvent.py

Let’s check the event invitation

Check event invitation

And 10 minutes before the event starts, the attendee will get a reminder.

Email event notification

Nice and simple.

Event Notification using webhooks

Webhooks can be used to send event notifications for Slack and Microsoft Teams. We’re going to review how to do that for Slack.

First, we need to have a Slack account. As we’re in a testing phase, it would be better to create a new and separate workspace where we can work without disrupting anyone else. So, we can go to Add Workspaces → Create a new workspace.

Create Slack Workspace

We will need to enter our email address for validation.

Enter your email

We will receive a security code, so we need to check our inbox.

Verify your email with code

This will open Slack with the new workspace option.

Open Slack

Here we can choose an appropriate name.

Company name

We’re just testing, so no need to invite anyone else. Simply skip this step.

Just skip this

Again, we can write something appropriate here.

Working on something?

Slack really wants us to collaborate, but let’s just skip this.

Skip this one too

Can’t blame them for trying ????. We can just skip it again.

Yes, skip it

And that’s it.

Finally, we're done

Now comes the interesting part. Let’s create a new Slack application. We’re going to use the “From scratch” option.

Create an Slack application

We need to choose a name for our application and also a workspace to work with. We’re using our newly created workspace.

Choose a workplace for the application

Once we created the application, we will be presented with some options to add features and functionality. Let’s choose Incoming Webhooks.

Choose incoming webhooks

It will be off by default, so we just need to activate it.

Activate Incoming Webhooks

Next, we need to add it to our workspace.

Add new Webhook

Slack needs to know where the messages from the Webhook are going to be posted, so let’s choose the channel that we created in a previous step.

Grant application permission

Now, we can use the webhook address in our Python script.

Setup the Slack channel and we're almost ready to send event notifications

So, let’s create the script and call it WebhookReminderEvent.py

Keep in mind that if we run this script after 1:00pm it’s not going to work. Add a couple of hours to your current time before running it.

# Load your env variables
from dotenv import load_dotenv
load_dotenv()

# Import your dependencies
import os
import json
import datetime
from datetime import date
from nylas import APIClient

# Initialize your Nylas API client
nylas = APIClient(
    os.environ.get("CLIENT_ID"),
    os.environ.get("CLIENT_SECRET"),
    os.environ.get("ACCESS_TOKEN"),
)

# Get today’s date
today = date.today()
# Today’s date at 12:30:00 pm
START_TIME = int(datetime.datetime(today.year, today.month, today.day, 12, 30, 0).strftime('%s'))
# Today’s date at 13:00:00 pm
END_TIME = int(datetime.datetime(today.year, today.month, today.day, 13, 0, 0).strftime('%s'))

# Create event draft
event = nylas.events.create()

# Define event elements
event.title = "Let's learn some Nylas Python API!"
event.location = "Blag's Den!"
event.when = {"start_time": START_TIME, "end_time": END_TIME}
event.participants = [{"name": "Blag", 'email': os.environ.get("RECIPIENT_ADDRESS")}]
event.calendar_id = "<your_calendar_id>"

# Create notification
event.notifications = [{"type": "webhook", "minutes_before_event": 10, "url": "<your_slack_webhook>","payload": json.dumps({"text": "Workshop starts in 10 minutes!"})}]

# We would like to notify participants
event.save(notify_participants=True)
if event.id:
	print("Event created successfully")
else:
	print("There was an error creating the event")

We can run this script from the terminal by typing:

$ python3 WebhookReminderEvent.py

Ten minutes prior to the event, we will get a notification on Slack.

Slack event notification

Using event notifications using the Nylas Python SDK is fast and easy.

You can sign up Nylas for free and start building!

For more information, don’t forget to visit our Documentation page.

Related resources

How to create an appointment scheduler in your React app

Learn how to create an appointment scheduler in your React app using Nylas Scheduler. Streamline bookings and UX with step-by-step guidance.

Beyond APIs: Designing elegant, smart Web Components

Dive into the world of smart Web Components for advanced API integration solutions. Design elegant, customizable elements to elevate user experiences.

How to integrate a React calendar component to streamline scheduling

Elevate your app’s scheduling with a React calendar component and seamlessly integrate with Nylas for efficient calendar management.