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

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

5 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. Creating event notifications is a great way to improve attendance. The Nylas Ruby SDK offers three ways of reminding people about important events by using notifications, and in this blog post, we’re going to review them one by one.

Is your system ready?

If you already have the Nylas Ruby 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 Ruby 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 event notifications using Ruby and Nylas is a great way to ensure that the participant will not forget about the event.

Event notifications using email

The most simple method is to send an event reminder email. So let’s grab our favorite code editor and create a file called EmailReminderEvent.rb

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

#!/usr/bin/env ruby
# Import your dependencies
require 'dotenv/load'
require 'nylas'
require 'date'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV["CLIENT_ID"],
    app_secret: ENV["CLIENT_SECRET"],
    access_token: ENV["ACCESS_TOKEN"]
)

today = Date.today

start_time = Time.local(today.year, today.month, today.day, 13, 0, 0).strftime("%s")
end_time = Time.local(today.year, today.month, today.day, 13, 30, 0).strftime("%s")

event = nylas.events.create(title: "Let's learn some Nylas Ruby API!", location: "Blag's Den", calendar_id: ENV["CALENDAR_ID"],
	when: { start_time: start_time, end_time: end_time },
	notifications: [{type: "email", minutes_before_event: "10",subject: "Ruby Workshop Alert",
	body: "Ruby Workshop starts in 10 minutes!"}],
	participants: [{"name": "Blag", "email": "[email protected]"}])

if event.id
	puts 'Event created successfully'
else
	puts 'There was an error creating the event'
end

We can execute this script from the terminal by typing:

$ ruby EmailReminderEvent.rb

Let’s check the event invitation

Checking an invitation

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

Email notification reminder

Nice and simple.

Event notifications using webhooks

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

First, we need to have an 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.

Adding 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.

Enter code

This will open Slack with the new workspace option.

Open Slack app

Here we can choose an appropriate name.

Name your team

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

Find people

Again, we can write something appropriate here.

Focus of your team

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

Describe channel

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

Invite someone

And that’s it.

Channel is done

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

Create app

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

Name app

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

Add webhooks

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

Activate webhook

Next, we need to add it to our workspace.

Add webhook to workspace

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.

Select where to post webhook

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

Copy webhook name

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

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

#!/usr/bin/env ruby
# Import your dependencies
require 'dotenv/load'
require 'nylas'
require 'date'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV["CLIENT_ID"],
    app_secret: ENV["CLIENT_SECRET"],
    access_token: ENV["ACCESS_TOKEN"]
)

# Get today’s date
today = Date.today

# Today’s date at 11:30:00 am
start_time = Time.local(today.year, today.month, today.day, 13, 0, 0).strftime("%s")
# Today’s date at 12:00:00 am
end_time = Time.local(today.year, today.month, today.day, 12, 0, 0).strftime("%s")

# Create the event
event = nylas.events.create(title: "Let's learn some Nylas Ruby API!", location: "Blag's Den", calendar_id: ENV["CALENDAR_ID"] ,
	when: { start_time: start_time, end_time: end_time },
	notifications: [{type: "webhook", minutes_before_event: "10",
	url: "<your_slack_webhook>",
	payload: {text: "Ruby Workshop starts in 10 minutes!"}.to_json}],
	participants: [{"name": "Blag", "email": ENV["RECIPIENT_ADDRESS"] }],
	notify_participants: true)

# Everything went Ok
if event.id
	puts 'Event created successfully'
# Something went wrong
else
	puts 'There was an error creating the event'
end

We can run this script from the terminal by typing:

$ ruby WebhookReminderEvent.rb

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

Slack notification reminder

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

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

Related resources

How to build a CRM in 3 sprints with Nylas

What is a CRM? CRM stands for Customer Relationship Management, and it’s basically a way…

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.