Skip to content

Synchronise events from Google Calendar to user profile in Slack

License

Notifications You must be signed in to change notification settings

Tomasz-Kluczkowski/slack-gcal-sync

Repository files navigation

slack-gcal-sync

Synchronise events from Google Calendar to user profile in Slack

Lint-Test-Build

codecov

This project is my test ground for learning Rust language. The goal is as follows:

  • get google calendar events on a predetermined interval
  • using configuration mapping google calendar event title to slack user profile settings set the new value for slack user profile

Project Setup

Assuming linux Ubuntu as OS or WSL2 with Ubuntu.

  • clone the repo to your local machine
  • install rust language
  • open terminal in root of the project
  • install pre-commit tool:
sudo apt update && sudo apt install -y pre-commit
  • install pre-commit hook for the project:
pre-commit install
  • run tests for entire project:
 cargo test --workspace
  • build entire project (in debug, unoptimised mode), check target/debug:
cargo build --workspace
  • build entire project (optimised for production), check target/release:
cargo build --release --workspace

Command line interface

  • Run slack-gcal-sync --help to see command line interface options.

Application Configuration

The application configuration is read in the following order:

  • if --application-config-path cli option is specified, we try to load it from a json file at that path. The application configuration must specify all values for application configuration, or it will fail to load.
  • if --application-config-path cli option is not specified, a default application path will be used: config/application_config.json.
  • if there is no application config file (at default or specified path) we will set default app config.
  • any CLI options such as --calendar-id override what is specified in the application config file or default application config.

Application config file

This file holds application configuration in json format. All keys are required for configuration to be valid.

{
  "calendar_id":  "[email protected]",
  "service_account_key_path": ".secrets/.service_account.json"
}

Setting up integrations required by slack-gcal-sync app

In the following 2 sections you will see how to set up your Slack and Google Cloud Platform to get necessary credentials and set up access permissions for slack-gcal-sync app to work.

Setting Up Integration With Slack API

image

  • Click From a Manifest

image

  • select target workspace for the new app and click Next

image

  • Paste this manifest in the Json tab to allow user profile reading and writing (only) and click Next
{
    "display_information": {
        "name": "user-profile-app",
        "description": "User Profile Integration",
        "background_color": "#004492"
    },
    "oauth_config": {
        "scopes": {
            "user": [
                "users.profile:read",
                "users.profile:write"
            ]
        }
    },
    "settings": {
        "org_deploy_enabled": false,
        "socket_mode_enabled": false,
        "token_rotation_enabled": false
    }
}

image

  • click Create

image

  • After your app is created, navigate to Settings -> Install App to install it in your workspace and generate necessary OAuth tokens for programmatic communication.

image

  • Click Install to <your workspace name> (here my workspace is called lab so it shows as Install to lab).
  • Confirm that you want to install the app and click Allow.

image

  • After you click Allow you will be presented with the User OAuth Token which will be used for programmatic communication with Slack API on behalf of your user. The secret always starts with xoxp.

image

  • This can also be found in Features -> OAuth & Permissions.

Setting Up Integration With Google Cloud API

Create Google Cloud Platform Project

  • if you already have google account you can just join google cloud platform, otherwise create a new google account.

  • set up Google Cloud Account - you can select the free trial.

    • accept Terms of Service and click AGREE AND CONTINUE.

      image

  • Click Select a project to use existing or create a new one.

image

  • Click NEW PROJECT (we assume starting from scratch here).

image

  • Give your project a name and click CREATE.

image

Enable Google Calendar API

  • You will see a notification popup, click SELECT PROJECT.

image

  • Click on hamburger navigation menu.

image

  • Click on APIs & Services -> Library tab.

image

  • Scroll down a bit to Google Workspace group of apis or just click on it in the sidebar menu and click on Google Calendar API box.

image

  • Click ENABLE to switch Google Calendar API in your project.

image

Create Credentials

  • Now we need to create a service account to allow machine to machine communication without need for interactive login.

  • In the view that appears after you enabled Google Calendar API, click Credentials in the sidebar.

image

  • Click + CREATE CREDENTIALS.

image

  • Click Service account.

image

  • Fill in Service account name and optionally Service account description and click DONE since we don't need to grant any optional permissions here.

image

  • Click on your newly listed service account to add a service account key.
  • Make note of the auto-generated email - we will need it to share Google Calendar with this service account.

image

  • Click on KEYS tab.

image

  • Click on Add KEY -> Create new key.

image

  • In the popup use the default JSON key type and click CREATE.

image

  • The private key in json format will be saved in your downloads folder on your machine - keep it safe.
  • This json file is needed by slack-gcal-sync app to authenticate itself to google calendar api. You will need to specify path to it in the app configuration.

image

Share Google Calendar With Service Account

  • open Google Calendar for your Google account
  • Click on the cog drop down and Settings

image

  • in Settings for my calendars click on the calendar you want to share with the service account

image

  • Scroll down to Share with specific people or groups and click Add people and groups

image

  • Fill in the auto-generated email that your service account got from a step above and select appropriate access level. For read only use See all event details.

image

  • Click Send

image

Get calendar id

  • The calendar id is needed when making calls to Google calendar API and needs to be set in the slack-gcal-sync app configuration.
  • Simply scroll down on the selected calendar settings page to Integrate calendar section and copy Calendar ID.

image

Debugging tests containing google calendar api calls

To see what mockito server is being called with:

  • add env_logger to dev dependencies
  • set up logger at the beginning of test: let _ = env_logger::try_init();
  • run test configuration with environment variable set RUST_LOG=mockito=debug
  • mockito server will nicely show what it was called with like this:
[2024-12-31T17:42:11Z DEBUG mockito::server] Request received: 
    GET /calendars/test_calendar_id/events?timeMin=2024-12-31T00%3A00%3A00.000Z&timeMax=2025-01-01T00%3A00%3A00.000Z&alt=json
    user-agent: google-api-rust-client/6.0.0
    authorization: Bearer <REDACTED>
    content-length: 0
    host: 127.0.0.1:33285

About

Synchronise events from Google Calendar to user profile in Slack

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published