- Products
- Solutions Use casesBy industry
- Developers
- Resources Connect
- Pricing
The events on your user’s weekly calendar don’t exist in a vacuum; they’re the result of relationships he has cultivated across numerous communications channels. Most people regularly engage in email communications that span across numerous aspects of their life, but one area that email is particularly important is when your application’s users are building relationships with clients at other companies and organizations.
When your user’s day is packed with meetings, he might want to quickly review the details of his next meeting to make sure he’s fully prepare to impress his client. In addition, he might need to make adjustments to the meeting based on emails he’s shared with the client over the past couple of days, such as inviting new participants, changing the time of the meeting, or adding items to the itinerary.
There is a strong relationship between the events on your user’s calendar, the contacts in his contacts book, and the recent email communications he’s been involved in. This post will explore ways that you can improve your application by adding more context and insight to upcoming events on a user’s calendar with the Nylas Platform.
import os import datetime from nylas import APIClient CLIENT_ID = os.environ['CLIENT_ID'] CLIENT_SECRET = os.environ['CLIENT_SECRET'] ACCESS_TOKEN = os.environ['ACCESS_TOKEN'] nylas = APIClient( CLIENT_ID, CLIENT_SECRET, ACCESS_TOKEN, )
calendars = nylas.calendars.all() # Get the calendar_id of the "Client Meetings" calendar calendar_id = [ calendar['id'] for calendar in calendars if 'Client Meetings' in calendar['name'] ][0] today = datetime.date.today() # Get Monday's datetime and convert it to a unix timestamp monday = today + datetime.timedelta(days=-today.weekday()) monday_unix = monday.strftime("%s") # Get Friday's datetime friday = monday + datetime.timedelta(days=5) friday_unix = friday.strftime("%s") # Return all events between Monday and Friday of this week events = nylas.events.where(calendar_id=calendar_id, starts_after=monday_unix, ends_before=friday_unix)
for event in events: print("Title: {} | Participants: {} | Description: {} | Start Time: {} | End Time: {}".format( event.title, ",".join([participant['email'] for participant in event.participants]), event.description, event.when['start_time'], event.when['end_time'] ))
Title: Discuss the Latest Line of Nuclear Ornithopters | Participants: [email protected] , [email protected] | Description: The Latest model of our Nuclear Ornithopters is out! Let's talk about why you might want to upgrade. | Start Time: 1585792800 | End Time: 1585796400
# Select the first event and get the list of participants first_event = events[0] participants = first_event['participants'] # Use the Nylas Contacts API to return detailed information about each participant for participant in participants: contacts = nylas.contacts.where(email=participant['email']) if contacts.all(): contact = contacts[0] phone_number = next(iter(list(contact['phone_numbers'].values())), None) email = next(iter(list(contact['emails'].values())), None) print("Full Name: {} | Email: {} | Phone Number: {} | Location: {} | Profile Picture: {} | Company Name: {} | Job: {}".format( contact['given_name'] + " " + contact['surname'], email, phone_number, contact['office_location'], contact['picture_url'], contact['company_name'], contact['job_title'], ))
Full Name: Marie Curie | Email: ['[email protected]'] | Phone Number: ['(040) 719-3466'] | Location: Warsaw, Poland | Profile Picture: https://api.nylas.com/contacts/marie/picture | Company Name: Radioactivity Inc. | Job: Physicist Full Name: Leonardo Da Vinci | Email: ['[email protected]'] | Phone Number: ['(020) 515-1967'] | Location: Vinci, Italy | Profile Picture: https://api.nylas.com/contacts/leo/picture | Company Name: Ornitech Unlimited | Job: Flight Architect
threads = [] for participant in participants: two_weeks_ago = datetime.date.today() - datetime.timedelta(14) unix_two_weeks_ago = two_weeks_ago.strftime("%s") # Search across the user's email inbox for threads in last 14 days that are from any of the event participants. threads = nylas.threads.where(from_=participant['email'], last_message_after=unix_two_weeks_ago) # For all matching threads, return the subject line, snippet, and date for all messages from that thread for thread in threads: for message_id in thread['message_ids']: message = nylas.messages.get(message_id) print("Subject Line:\n {} \n Snippet: \n {} \n Date: \n {}".format( message.subject, message.snippet, message.date ))
Subject Line: Re: Meeting on Thursday Snippet: I've got a conflict and may need to reschedule. Date: 1585346511 Subject Line: Nuclear Ornithopters Snippet: Hey Al, I'm wondering if you might know anything about some of the Nuclear Ornithopters that are hitting the market. Date: 1585346511
for thread in threads: for message_id in thread['message_ids']: message = nylas.messages.get(message_id) if message.files: for file_ in message.files: file_object = nylas.files.get(file_['id']) print("File: {} | Content Type: {}".format( file_object.filename, file_object.content_type )) open(file_object.filename, 'wb').write(file_object.download())
current_participants = [ participant['email'] for participant in participants ] new_participants = [] # For all matching threads, return the participants who aren't also a participant in the event for thread in threads: for thread_participant in thread.participants: if thread_participant['email'] not in current_participants + new_participants : new_participants.append(thread_participant['email']) print("Adding the following participants to the event:") for participant in new_participants: print(participant) # Modify the upcoming event to add the missing participants for participant in new_participants: first_event.participants.append({'email' : participant}) first_event.save(notify_participants='true')
Adding the following participants to the event: [email protected]
The Nylas Platform is the easiest way to integrate with 100% of calendar providers in the world. Here are a few resources to teach you about what else is possible with Nylas.
Ben is the Developer Advocate for Nylas. He is a triathlete, musician, avid gamer, and loves to seek out the best breakfast tacos in Austin, Texas.