Building a (Personal) Relationship Manager with Notion API - Automating Your Process with GitHub Actions.

Notion API + Node.js + GitHub Actions as a Personal Relationship Manager

Building a (Personal) Relationship Manager with Notion API - Automating Your Process with GitHub Actions.

Introduction

In the last part of the series, we created our application in Node.js and we have found it to be functional. Getting the list of people we haven't spoken to and being able to send reminders and check-in messages by running the code working on our local is cool but it is missing the key feature that we want to get out of this project. We need it to run automatically. For this phase of the project, we can use GitHub Actions. If you do not know a lot about GitHub actions, this course on LinkedIn Learning can get you up to speed.

Automating the Flow with GitHub Actions.

We need to create a folder for our workflows.

  • At the root of your project, create a folder called workflows: .github/workflows
  • Create a file called run-notion-prm.yml
  • Add the following code to it

    name: Check for Contacts that Have Not Been Contacted Recently and Notify Through SMS Flow
    on:
      schedule:
        - cron: '0 14 * * 6'
      workflow_dispatch:
    jobs:
      check_and_notify:
        name: Check for Contacts that Have Not Been Contacted Recently and Send SMS Notification
        runs-on: ubuntu-latest
        steps:
          - name: Checkout
            uses: actions/checkout@v2
          - name: Set up Node
            uses: actions/setup-node@v3
            with:
              node-version: 16.15.1
          - name: Install dependencies
            run: yarn install
          - name: Run index.js
            run: node index.js
    

    The first property is the name of our workflow. This is what you will see in the GitHub web interface when you click on the Actions tab for your repository.

    The next line describes when we want to run this workflow. Instead of listening for the typical push or pull request events, we have specified a schedule for when this workflow will be run. I want this flow to be run on Saturdays at 10 am. However, GitHub servers use the UTC time zone and I stay in the Eastern time zone. Using a cron expression, I have specified that the flow should run at 14:00 hrs or 2pm UTC which will be 10:00am my time.

    The rest of the file is handling how the code is run. Under the jobs property, I specified a list of tasks I want to perform. First, I want to checkout the repository. Next, I want to setup a Node environment in the runner with version 16.15.1. Then, I install the dependencies and finally, I run the index.js file.

  • Initialize a git repository, stage and commit your file changes and push them to GitHub.
  • Click on the Actions tab of your newly created GitHub repository. You might notice that nothing is happening. If you are following this tutorial on a day that is not Saturday, the first time the workflow will run is the next Saturday.

    However, I added something in the yml file that I did not mention in the previous step. It is the workflow_dispatch property. Adding this property allows us to run this flow manually at any time from the GitHub web interface.

  • Click on the Check for Contacts that Have Not Been Contacted Recently and Notify Through SMS Flow workflow.

    image.png

    GitHub informs us that the workflow has a workflow_dispatch event trigger and provides us with a button to run the workflow.

  • Click on 'Run workflow'

    image.png If you have different branches you can choose the branch from which you want to run your workflow. However, I just have the main branch so I will click on the green Run workflow button.

When you do that you will see that workflow is queued and eventually should fail.

image.png

Click on the workflow and continue drilling down (workflow -> job -> steps) till you see the screen shown in the image below.

image.png

We see that the job failed at the Run index.js step. The output log shows a Error: username is required text as part of the message. This is an error from Twilio. The username is undefined because we are trying to access process.env.TWILIO_ACCOUNT_SID in our code but we have not set that environment variable yet.

NOTE: If you used another service for your SMS messaging, the error might be different. Because we catch and log any notion client errors, it is possible that the workflow will pass and we would simply see the error message in the console.

Setting Environment Variables for GitHub Actions with GitHub Secrets

Typically, our auth tokens and secrets should be kept safe. We do not want to commit them to GitHub. That is why we create a .env file which we add to .gitignore and store those variables in there locally instead of hard coding them into our code. GitHub secrets allows us to be able to do that for our workflows. When you create a secret, the GitHub Actions environment variables encrypts the values such that it is not understandable by anyone else.

To use GitHub Secrets, we need to do the following:

  1. Click on the Settings tab for your GitHub repo.
  2. Scroll to the Security section and click on Secrets.

image.png

  1. Click on Actions secrets.
  2. Click on New repository secret. image.png
  3. Add the NOTION_AUTH_TOKEN environment variable and it's corresponding value from the .env file.
  4. Repeat the process for all other environment variables.

When you are done, you will have a list that looks like this:

image.png

Click on the Actions tab and re-run the workflow. The jobs still fail. What else could be wrong?

We need to pass those secrets down to our code as environment variables. How do we do that? We need to make some modifications to our run-notion-prm.yml file.

##....
jobs:
  check_and_notify:
    name: Check for Contacts that Have Not Been Contacted Recently and Send SMS Notification
    runs-on: ubuntu-latest
    steps:
      ## ... previous steps
      - name: Run index.js
        run: node index.js
        env:
          NOTION_AUTH_TOKEN: ${{ secrets.NOTION_AUTH_TOKEN }}
          NOTION_DATABASE_ID: ${{ secrets.NOTION_DATABASE_ID }}
          TWILIO_ACCOUNT_SID: ${{ secrets.TWILIO_ACCOUNT_SID }}
          TWILIO_AUTH_TOKEN: ${{ secrets.TWILIO_AUTH_TOKEN }}
          TWILIO_PHONE_NUMBER: ${{ secrets.TWILIO_PHONE_NUMBER }}
          PHONE_NUMBER_FOR_REMINDERS: ${{ secrets.PHONE_NUMBER_FOR_REMINDERS }}

We added the env property for our last step and set the different environment variables we use to the GitHub secrets. Henceforth, anywhere in our code where we access those environment variables through process.env, the value of the GitHub secret is passed down to the job so that it uses it.

Run the workflow one last time. All steps should execute successfully and the job passes as indicated by the checkmarks.

image.png

In the console output of the Run index.js step, you see the values of response.sid and response.status logged to the console. You should also get an SMS at the numbers you specified.

image.png

Conclusion

In this series, we talked about Notion and the Notion API. We discussed a use case for Nation and its API and we went ahead to build it. Lastly, we learned briefly about GitHub actions and how they can prove to be useful in automation use cases like ours.

The code for this project can be found in the notion-personal-relationship-manager repo. Feel free to fork it and play around with. The final version of the index.js file is also found here

Resources

twilio.com/docs/sms/send-messages#send-an-s.. developers.notion.com crontab.guru/#014__6