Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ingest emails from Gmail and send to Airtable #4

Closed
wants to merge 12 commits into from
Closed
6 changes: 6 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "none",
"tabWidth": 2,
"semi": true,
"singleQuote": false
}
12 changes: 12 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
"AIRTABLE_BASE": {
"description": "Go to your airtable base. Click on 'Help -> API Documentation' in top right. Scroll to `Authenitcation`. The thing we need is in 'base('')` to the right.",
"required": true
},
"GOOGLE_CLIENT_KEY": {
"description": "See docs/gmail-refresh-token.md",
"required": true
},
"GOOGLE_CLIENT_SECRET":{
"description": "See docs/gmail-refresh-token.md",
"required": true
},
"GOOGLE_REFRESH_TOKEN":{
"description": "See docs/gmail-refresh-token.md",
"required": true
}
}
}
20 changes: 20 additions & 0 deletions docs/gmail-refresh-token.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Creating credentials for the Gmail integration

Unfortunately you can't just add your username/password and call it a day. So there are a few steps to get the right credentuals for Gmail. You can use the Google OAuth Playground to get a long-lived refresh token, which lets you authenticate correctly. Ideally the server would run a real OAuth flow, but for a single user that may be overkill. The process involves generating a new OAuth client and manually authenticating it against the current user.

This is all stolen from: https://stackoverflow.com/questions/19766912/how-do-i-authorise-an-app-web-or-installed-without-user-intervention/19766913#19766913i

### Steps

* Go to https://console.developers.google.com/apis/credentials/consent and set up a consent screen. If you need to create a Google Cloud project, the name isn't important. Here you can put the hostname/url of the heroku app in the places where it's asked for.
* For scopes, go to `Add scopes -> manually paste` and set the scopes to `https://mail.google.com/ https://www.googleapis.com/auth/gmail.labels https://www.googleapis.com/auth/gmail.readonly`
* Go to https://console.developers.google.com/apis/credentials/oauthclient to set up the client. Once again put in the herkou url/hostname where applicable. The redirect URL will also need to include:

`https://developers.google.com/oauthplayground`

* Save this Client Key / Secret.

* Go to `https://developers.google.com/oauthplayground`. You will need to add the key and secret from above by clicking on `Settings -> Use your own OAuth credentials`
* Paste the following scopes: `https://mail.google.com/ https://www.googleapis.com/auth/gmail.labels https://www.googleapis.com/auth/gmail.readonly` into the box next to Authorize APIs, click that and then Exchange authorization code for tokens.
* You made it! Grab the refresh token and hope to never have to go through /this/ process again.

7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const bodyParser = require("body-parser");
const { App, ExpressReceiver } = require("@slack/bolt");
const startEvents = require("./src/endpoints/events.js");
const startInteractivity = require("./src/endpoints/interactivity.js");
const { startGmailSync } = require("./src/gmail-sync-worker.js");
const { initIntl, addUserInfo } = require("./src/middleware.js");
const { addressHandler } = require("./src/endpoints/geo.js");

Expand Down Expand Up @@ -40,6 +41,12 @@ expressApp.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "build", "index.html"));
});

// ---------- CUSTOM SERVICES -------------

if (process.env.SYNC_GMAIL) {
startGmailSync();
}

(async () => {
// Start the app
await boltApp.start(process.env.PORT || 3000);
Expand Down
Loading