DEV Community

Robin Goudeketting
Robin Goudeketting

Posted on

Automating Insulin Ordering: Adding an Event to Google Calendar (Part 5)

In the previous parts of this series, we discussed capturing the browser request to the GP's website and sending an email to the pharmacy. Now, we will focus on automating the addition of an event to Google Calendar. We're going to be using gcalcli to reduce the amount of manual labor.

Installing gcalcli

First, we need to install gcalcli. I'm using MacOS, so I can install in using homebrew:

brew install gcalcli
Enter fullscreen mode Exit fullscreen mode

If you're on a different Operating System, check out the gcalcli docs to find your installation method.

Setting Up Google Calendar API

To enable making events, we need to create a project in the Google Developer Console and set up OAuth credentials. Otherwise Google does not allow us using the Calendar API to create events. Here are the steps I used:

Step 1. Go to Google Developers Console and create a new project, e.g., gcalcli or Insulin Orderer.

Create a new project in the modal

Step 2. In the left sidebar, click Enabled APIs and Services and click on "+ Enable APIs and Services". Look for the Google Calendar API and enable it.

Look for the Google Calendar API

Step 3. Create Credentials:

  • Go back to the project gcalcli in the Developers Console.
  • Navigate to the "Credentials" section in the left menu.
  • Click "+ Create Credentials" > "OAuth client ID".
  • Choose "Application type Desktop App".
  • Name it gcalcli.
  • The credentials produced here are needed in step 5.

Step 4. OAuth Consent Screen:

  • Navigate to the "OAuth consent screen" section in the left menu.
  • Select "External" users.
  • Fill in the App name (e.g., gcalcli), User support email, and Developer contact information with your Gmail address.
  • Leave the App domain blank.
  • For Scopes, choose all calendar API-related scopes.
  • Add your Gmail address as a Test user.
  • Save everything and then go back to the dashboard. It is important to leave the Publishing status as Testing, otherwise your google calendar will be openly available to the world.

Step 5. Authenticate locally:
There are two ways in which you can authenticate your machine to enable interacting with Google Calendar. Either you login using the OAuth screen from google or you create a configuration file.

To login using the Google OAuth screen, paste the following command into your terminal (here you of course change the values of the --client-id and --client-secret):

gcalcli --client-id "your client ID ending in ...apps.googleusercontent.com" --client-secret "your client secret" agenda
Enter fullscreen mode Exit fullscreen mode

This will open your browser and allow you to login.

Alternatively, you can create a configuration file like this: touch ~/.gcalclirc. Then edit this file and add the following to it:

--client-id="your client ID ending in ...apps.googleusercontent.com" --client-secret="your client secret" 
Enter fullscreen mode Exit fullscreen mode

In either case, after you've done this, you should be able to run gcalcli agenda and it should show you your agenda items.

Adding a Task to Google Calendar

To check if the gcalcli command works, run:

gcalcli add
Enter fullscreen mode Exit fullscreen mode

If this command runs correctly, it will ask you a few questions regarding the event you want to add. The most important parts are 'Title', 'When', 'Duration', and to which calendar to add the event to. Since I have access to multiple calendars, I needed to select the calendar to add the event to. I copied the name of the calendar to use in the command itself.

Here is the command I use to add an event to my Google Calendar:

gcalcli add --calendar 'Calendar Name' --title 'Pick up insulin' --when 'Tomorrow 13:00' --duration 15 --noprompt
Enter fullscreen mode Exit fullscreen mode

This will add the event to the calendar with name Calendar Name. The title of the event will be Pick up insulin. It will be an event of 15 minutes and it will be scheduled for tomorrow at 13:00. Tomorrow is important, because it can take up to 24h before the prescription is available on my insurance card. Lastly, --noprompt will ensure that if data is missing (like the Location), it will not go into interactive mode to fill in the missing information, which is important for a script that needs to run autonomously.

The command can be added into the bash function relatively simply:

function add_calendar_task() {
  gcalcli add --calendar 'Robin Goudeketting (privé)' --title 'Pick up insulin' --when 'Tomorrow 13:00' --duration 15 --noprompt
}
Enter fullscreen mode Exit fullscreen mode

What’s Next?

With the complete automation script in place, the next step will be to have this automation run regularly in the background. I can now already order insulin with one command in my terminal, but ideal would be if it runs periodically in the background.

Stay tuned for any further enhancements and refinements to this process!

Thank you for following along! If you have any questions or suggestions, feel free to leave a comment below. Don't forget to share this post with anyone who might find it useful.

Top comments (0)