Automating Insulin Ordering: Adding an Event to Google Calendar (Part 5)
Tags: automation, productivity, programming, processoptimisation
Published: 2024-06-06 08:20:47

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: 

```bash
brew install gcalcli
```

If you're on a different Operating System, check out [the gcalcli docs](https://github.com/insanum/gcalcli) 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](https://console.developers.google.com) and create a new project, e.g., gcalcli or Insulin Orderer. 


![Create a new project in the modal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jd2uqijyibdbgt5kpcv.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0x78bivtlxrblrlndvx9.png)

**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`):

```bash
gcalcli --client-id "your client ID ending in ...apps.googleusercontent.com" --client-secret "your client secret" agenda
```

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:

```bash
--client-id="your client ID ending in ...apps.googleusercontent.com" --client-secret="your client secret" 
```

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:

```bash
gcalcli add
```

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:

```bash
gcalcli add --calendar 'Calendar Name' --title 'Pick up insulin' --when 'Tomorrow 13:00' --duration 15 --noprompt
```

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:

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

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