DEV Community

Robin Goudeketting
Robin Goudeketting

Posted on

Automating Insulin Ordering: Rethinking Google Calendar Integration (Part 6)

After we discussed automating the prescription request and sending emails to the pharmacy, in the previous part of this series, we explored integrating a task scheduler by adding an event to Google Calendar using gcalcli. However, as promising as this solution seemed, a significant issue has arisen during using the script for a while: the need for reauthentication with Google Calendar every week.

The Issue with Google Calendar CLI (gcalcli)

gcalcli is a command-line tool that allows you to interact with your Google Calendar directly from the terminal. It’s convenient for creating, viewing, and managing events without leaving the command line. However, it relies on OAuth 2.0 authentication, and requires you to reauthorize the tool with Google every seven days if you use it via a test application.

This frequent reauthorization poses a problem for automation. A script designed to run unattended, such as the one we’ve been building to order insulin and schedule reminders, would fail after a week due to this reauthentication requirement. This makes gcalcli unsuitable for long-term, fully automated tasks, unless we want to open up your google calendar to the world (the regular reauthorization is not required on published applications).

Why Reauthentication Happens

Google’s security policies enforce a regular reauthentication process to protect user data. This is particularly stringent for tools like gcalcli that use OAuth 2.0. Even though this enhances security, it complicates the use of such tools in automated scripts where human intervention is not desired.

Alternative Approach - IFTTT

Instead of gcalcli, I've decided to use IFTTT (if this then that). This is a service that allows you to trigger actions, such as creating a calendar event, by sending an email with a specific tag.

Here’s how I set it up:

Step-by-Step Guide for IFTTT Setup

  1. Create an IFTTT Account:
    • Sign up for an account using your Google account for easier integration.
  2. Create a New Applet:

    • Click the “Create” button on the top left of the IFTTT dashboard.
  3. Set Up the Trigger:

    • Click “Add” on the “If This” section.
    • Search for the Email service and select the “Send IFTTT an email tagged” trigger.
    • Input a tag to trigger the event. For example, use insulin_ordered.
    • Click “Create Trigger.”
  4. Set Up the Action:

    • Click “Add” on the “Then That” section.
    • Search for Google Calendar and select the “Create a detailed event” action.
    • Connect your Google Calendar if you haven’t done so already.
    • Fill out the form using the following details:
      • Title: Pick up insulin
      • Start Time: Tomorrow 13:00
      • End Time: Tomorrow 13:15
      • All Day: Select “No”
    • I left the description and location fields blank.
    • Click “Create Action,” then “Continue,” and finally “Create” to finalize the applet.
  5. Test the Setup:

    • Send an email to trigger@applet.ifttt.com with the subject #insulin_ordered. If everything is set up correctly, you should see a new event in your Google Calendar.

Script to Automate Event Creation

Now, we will create a script that sends the email to IFTTT whenever we have successfully sent an email to the pharmacy and need to create a calendar event. This script uses the same SMTP credentials as the email we send to the pharmacy, so it should work without a hitch.

function create_event_in_google() {
  SMTP_SERVER="smtp.gmail.com"
  SMTP_PORT="587"
  SMTP_USERNAME="<your gmail account>"
  SMTP_PASSWORD="<app password>"
  TO="trigger@applet.ifttt.com"
  SUBJECT="#insulin_ordered"
  MESSAGE="Subject: $SUBJECT\nTo: $TO\n\n"

  curl --url "smtp://$SMTP_SERVER:$SMTP_PORT" \
      --ssl-reqd \
      --mail-from "$SMTP_USERNAME" \
      --mail-rcpt "$TO" \
      --user "$SMTP_USERNAME:$SMTP_PASSWORD" \
      --tlsv1.2 \
      -T <(echo -e "$MESSAGE")

  if [ $? -eq 0 ]; then
      echo "Done, created event in Google Calendar"
      return 0
  else
      echo "Failed to create event in Google Calendar"
      return 1
  fi
}
Enter fullscreen mode Exit fullscreen mode

How It Works:

  • SMTP Details: The script uses the same SMTP configuration as the email sent to your pharmacy.
  • IFTTT Trigger: The TO field is set to IFTTT’s trigger email, and the SUBJECT contains the tag you configured in IFTTT (#insulin_ordered).
  • Success/Failure Handling: The script checks if the email was successfully sent and provides feedback.

This method bypasses the need for gcalcli and its reauthentication requirement, offering a simple and effective way to automate event creation in Google Calendar.

What’s Next?

With the update 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)