Creating a Credit Card Bill Notification using Gmail API, Google Calendar API, and Python
Recently, I read about the importance of paying your credit card bills on time and the repercussions of failing to do so. I have been fortunate to be able to pay my credit card bills on time so far, however I realized there aren’t enough measures in place to prevent me from missing a bill payment. Thankfully my bank sends out a notification email to my Gmail Account, informing me of when my next payment is due and how much I need to pay.
I often read the email and forget to set a Calendar notification for myself and I figured I could probably write a script to do just this. So the purpose of this blog is to teach you how to retrieve relevant information from your Gmail Account and how to create a Google Calendar notification with this information.
Full disclaimer: The authentication portion of the code should work for everyone however the specific details of the email content and the email address needs to be modified for your use case.
You should have python installed on your machine first. If you do not then simply go to this link and install the latest version of python. Follow the interactive wizard in order to setup python locally on your machine.
You also would need a Google Account for this since you will be interacting with both the Gmail and the Google Calendar API.
Enable APIs and download client configuration files
Next, you need to set up authentication to the Gmail API as well as the Google Calendars API. Kindly follow the tutorials below to enable the API as well as to install the necessary libraries.
Google Calendar: https://developers.google.com/calendar/quickstart/python
To enable both APIs, you can put any name for the project name, then select Desktop App and finally click Download Client Configuration. Copy this file to the working directory of your application and rename the Calendar API file to calendarCredentials.json and the Gmail API file to gmailCredentials.json.
Step 1: Define the main.py file
As you can see in the code below, this main file will import 3 files that we will define for each major functionality. The scope variables are API parameters that will be provided to authenticate to the Gmail and Calendar API respectively.
To summarize what the code is doing above, we first will authenticate to the Gmail API, if the authentication works then we will extract the necessary information from the Notification Email, next we will authenticate to the Calendar API, and finally create a Google Calendar Notification with the email information.
Now that we have the main file, we need to define the 3 imported files to make this application work. Next we will define the file to handle the authentication.
Step 2: Generate session tokens and authenticate to both APIs
Please see below for the file called generateCredentials.py and the purpose of it is to set up the authentication to both Gmail and Calendar API. As you can see in the code, this function will take in a type and a scope variable from the main function and this will define whether we want to authenticate to the Gmail API or the Calendar API in the current function call.
The scope is an API parameter that is defined in the main.py file. So what this does is if we need to authenticate to the Gmail API, we will supply our login configuration via the json files we downloaded from the API setup guide. Then we will create a pickle file that will be used to store the session token.
First Time Authentication
If the session token file does not exist then that means we need to authenticate to Gmail for the first time and you will get a browser redirect to do this. If the session token file is expired or not valid anymore, then we will refresh the session token.
Next, we need to set up the logic to access our email, find the notification email, and extract the necessary data from it.
Step 3: Extract the important information from our Notification Email using the Gmail API
In this code, we use our authenticated credentials to access the Gmail API. Line number 14 is the crucial command. This is the API command that will extract all the emails from your bank’s email address. So you would insert your bank’s email after the from: portion, and this will fetch all of their emails.
We then have to parse the messages, and loop through each email to request more details about each email in another API command. The reason for this is that Gmail will only return a short portion of an email’s heading to deal with large requests. Therefore, we need to get the full heading for each of these emails to find the notification email of interest to us.
In my case, my bank usually includes the keyword “e-Statement” in their notification emails so I simply searched for that and extracted the matching email from the list. The Gmail API will return emails in chronological order so the newest email will be at the beginning of the list. Next, I extracted the entire body of the email and parsed the fields of importance to me. If this format is different for your bank’s emails then lines 42–47 is where you would need to change to cater to their email structure.
Now that we have the crucial information, we need to set up a Google Calendar event for this.
Step 4: Create a calendar notification using the Google Calendar API
Below is the final file called createCalendarNotification.py which will simply create a Google Calendar Event using the email information we extracted. In this file, we first login to the Google Calendar API and then convert the due date from a string into a datetime format that Google will accept.
Line 9 is very specific to Google datetime formats so that is why it may seem strange. Then we want to set up the calendar notification to the day before the actual due date and that is the purpose of Line 11. We then check if the due date has already passed the current date and if it has not then we will search for the last 10 events from the due date until now.
The purpose of this is to check if we have already created this credit card notification event previously. Then we parse the event results and check if the credit card notification event exists in the list of events we extracted. If it does not then we create the notification event between Lines 31 and 41.
We first create our json body and pass in the necessary parameters for the event, and then we actually insert this new event using the Calendar API.
Now that you have placed the main.py file, generateCredentials.py file, createCalendarNotification.py file, extractNotificationDetails.py file, the gmailCredentials.json file, and the calendarCredentials.json file into a folder you are ready to run the final command! Your final folder structure should look like the screenshot below.
See below for the command to run your application!
After running the command above, you should receive a new window popup asking you to sign into your Gmail account. After selecting your email address to use, click Advanced and Go to “your project’s name”. Allow permissions to the API and you should be done.
You should then get a second pop up to grant permissions to the Calendar API and should follow the same approach above to do this. Hopefully you will get a print message stating that the Event was created along with a link to the calendar event.
As you can see below, my next upcoming bill payment is on March 3.
Your event will be on the day right before the due date of your upcoming bill payment. I hope this guide worked for you and prevented you from missing your upcoming bill payment! See you on my next blog post!