How to Send Notifications for Calendar Events Using the Nylas Ruby SDK

Have you ever created an event just to forget about it? Creating notifications using the Nylas Ruby SDK is a great way to fix this.

How to Send Notifications for Calendar Events Using the Nylas Ruby SDK

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 notifications is a great way to improve attendance. The Nylas Ruby SDK offers three ways of reminding people about important events, 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 a reminder is a great way to at least make sure that the participant will not forget about the event.

Event notifications 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.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 reminder

Nice and simple.

Event notifications using SMS

This might be one of the most efficient ways of notifying someone about an event, because we always have our cellphones handy and an SMS message is more noticeable than an email message.

Just keep in mind that using SMS reminders is a paid feature and might not be included in your paid plan. So you better reach out to your account manager before using it, as it will fail otherwise.

When creating a form for our event, we should ask for the name, email and phone number (providing a way to opt out and making clear that we need that information for reminding purposes only).

It would be a good practice to associate the participant or participants phone number with their contact information so we can notify them of future events (until they decide to opt-out).

Check for contact information

We’re going to use a script that will check if the contact exists and if there’s an available cellphone to send the SMS notifications. If the contact doesn’t exist, it will create it. We will call this file CreateUpdateContact.rb

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

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

# Define the contact information
contact_given_name = "<NAME>"
contact_surname = "<LAST_NAME>"
contact_email = ENV["RECIPIENT_ADDRESS"]
contact_cellphone = "<CELLPHONE>"

contacts = nylas.contacts.where(email: ENV["RECIPIENT_ADDRESS"])

begin
	contact = nylas.contacts.find(contacts[0].id)
	puts "Update contact"
	if contact.phone_numbers[0].number == 0
		contact.phone_numbers[0].number = contact_cellphone
	end
rescue
	puts "Create contact"
	contact = nylas.contacts.create
	contact.given_name = contact_given_name
	contact.surname = contact_surname
	contact.emails = [{type: 'personal', email: contact_email}]
	contact.phone_numbers = [{type: 'mobile', number: contact_cellphone}]
	contact.save()	
end

We can execute this script from the terminal by typing:

$ ruby CreateUpdateContact.rb

This will return either “Update Contact” or “Create Contact” depending on the performed action.

Once the new contact has been created or updated, we can continue with the SMS notification.

So let’s grab our favorite code editor and create a file called SMSReminderEvent.rb

#!/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 21:30:00 pm
start_time = Time.local(today.year, today.month, today.day, 21, 30, 0).strftime("%s")
# Today’s date at 10:00:00 pm
end_time = Time.local(today.year, today.month, today.day, 22, 0, 0).strftime("%s")

# Get participants email
email = ENV["RECIPIENT_ADDRESS"] 

# Get the contact information
contacts = nylas.contacts.where(email: email)
contact = nylas.contacts.find(contacts[0].id)
contact_name = contact.given_name + " " + contact.surname

# 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: "sms", minutes_before_event: "10", message: "Ruby Workshop starts in 10 minutes!"}],
	participants: [{name: contact_name, email: email, phone_number: contact.phone_numbers[0].number}])

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

If we don’t have SMS enable we will get an error message saying:

SMS notifications feature is not turned on for this application. Please contact your Nylas rep.

If we have it enabled, we will be able to check the event invitation.

Another invitation

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

Cellphone alert

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 we’re done.

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

Once that’s done, we’re going to have a Webhook address that we can use in our Python 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 reminder

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

Tags:

You May Also Like

A small introduction to Functional Programming
Working with the Nylas Contacts API and Ruby
hero banner for hacktoberfest
Build with Nylas 🤝 Hacktoberfest 2022