Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions gmail-notion-forwarder-2026-06-25/.github/workflows/deploy-gas.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Deploy Google Apps Script

on:
push:
branches:
- main
paths:
- 'gmail-notion-forwarder-2026-06-25/gas/**'
- '.github/workflows/deploy-gas.yml'

permissions:
contents: read

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '18'

- name: Install dependencies
working-directory: gmail-notion-forwarder-2026-06-25/gas
run: npm install

- name: Install clasp
run: npm install -g @google/clasp

- name: Create .clasp.json
working-directory: gmail-notion-forwarder-2026-06-25/gas
env:
CLASP_CONFIG: ${{ secrets.CLASP_CONFIG }}
run: echo "$CLASP_CONFIG" > .clasp.json

- name: Authenticate with Google
env:
CLASP_CREDENTIALS: ${{ secrets.CLASP_CREDENTIALS }}
run: |
mkdir -p ~/.config/clasp
echo "$CLASP_CREDENTIALS" > ~/.config/clasp/credentials.json

- name: Deploy to Apps Script
working-directory: gmail-notion-forwarder-2026-06-25/gas
run: clasp push

- name: Create deployment
working-directory: gmail-notion-forwarder-2026-06-25/gas
run: clasp deployments

- name: Notify Slack
if: always()
uses: slackapi/slack-github-action@v1.24.0
with:
payload: |
{
"text": "GAS deployment: ${{ job.status }}",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Gmail to Notion Forwarder\nStatus: ${{ job.status }}\nCommit: ${{ github.sha }}"
}
}
]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
28 changes: 28 additions & 0 deletions gmail-notion-forwarder-2026-06-25/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Node.js
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Terraform
*.tfstate
*.tfstate.*
.terraform/
.terraform.lock.hcl
terraform.tfvars
!terraform.tfvars.example

# clasp
.clasp.json
!.clasp.json.example

# IDE
.vscode/
.idea/
*.swp
*.swo
*~
.DS_Store

# GAS
clasp_error_log.txt
89 changes: 89 additions & 0 deletions gmail-notion-forwarder-2026-06-25/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Gmail to Notion Forwarder

Gmail で受信したメールを Notion に自動転送するシステムです。

## 概要

- **実装**:Google Apps Script (GAS)
- **スケジュール**:時間ベースのトリガー(デフォルト:1時間ごと)
- **連携**:Gmail API + Notion API

## セットアップ

### 前提条件

- Google Workspace / Google Account
- Notion Workspace & API Token
- Node.js 18+
- clasp CLI

### 1. GAS プロジェクトのセットアップ

```bash
cd gas
npm install
cp .clasp.json.example .clasp.json
# .clasp.json を編集してスクリプトID等を設定
clasp push
```

### 2. Notion API キーの設定

Terraform で Google Secret Manager に保存します。

```bash
cd terraform
cp terraform.tfvars.example terraform.tfvars
# terraform.tfvars を編集
terraform init
terraform apply
```

### 3. GAS でのトリガー設定

Google Apps Script エディタで以下を実行:
- メニュー:**実行 → 関数を実行 → forwardEmailsToNotion**
- トリガー:**時間ドリブン → 1時間ごと**

または `appsscript.json` で事前定義

## ファイル構成

```
.
├── gas/ # Google Apps Script
│ ├── main.gs # メイン処理
│ ├── appsscript.json # トリガー定義
│ ├── .clasp.json.example
│ └── README.md
├── terraform/ # IaC (秘密鍵管理)
│ ├── main.tf
│ ├── variables.tf
│ ├── terraform.tfvars.example
│ └── .gitignore
├── .github/workflows/ # CI/CD
│ └── deploy-gas.yml
└── README.md
```

## デプロイ

GitHub Actions で自動デプロイ:
1. 変更を `gas/` にコミット
2. `main` ブランチにプッシュ
3. ワークフローが自動実行

## トラブルシューティング

### メールが転送されない場合
- Gmail ラベルの設定を確認
- GAS の実行ログを確認:**実行 → ログ**
- Notion API キーの有効性を確認

### Notion API エラー
- API キーが正しく Secret Manager に保存されているか確認
- Notion Database ID が正しいか確認

## ライセンス

MIT
8 changes: 8 additions & 0 deletions gmail-notion-forwarder-2026-06-25/gas/.clasp.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"scriptId": "YOUR_SCRIPT_ID_HERE",
"projectIdSettings": {
"projectId": "YOUR_GCP_PROJECT_ID_HERE"
},
"rootDir": "./",
"fileExtension": "gs"
}
109 changes: 109 additions & 0 deletions gmail-notion-forwarder-2026-06-25/gas/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Google Apps Script Setup

## セットアップ手順

### 1. Google Apps Script プロジェクトの作成

1. [Google Apps Script](https://script.google.com) にアクセス
2. 新しいプロジェクトを作成
3. プロジェクト設定を開いて **Script ID** をコピー

### 2. clasp ログイン

```bash
npm install -g @google/clasp
clasp login
```

### 3. .clasp.json の設定

```bash
cp .clasp.json.example .clasp.json
```

`.clasp.json` を編集:
```json
{
"scriptId": "YOUR_SCRIPT_ID_HERE",
"projectIdSettings": {
"projectId": "YOUR_GCP_PROJECT_ID_HERE"
}
}
```

### 4. スクリプトをデプロイ

```bash
clasp push
```

### 5. 環境変数の設定(GAS UI)

Google Apps Script エディタで:

1. **プロジェクト設定 → スクリプト プロパティ** を開く
2. 以下を追加:

| キー | 値 |
|------|-----|
| `NOTION_API_KEY` | Notion API キー |
| `NOTION_DATABASE_ID` | Notion Database ID |

### 6. トリガーの設定

Google Apps Script エディタで:

1. **実行 → トリガー(時計アイコン)** をクリック
2. **トリガーを追加** をクリック
3. 設定:
- 実行する関数:`forwardEmailsToNotion`
- 実行するデプロイ:`Head`
- イベントソース:`時間ベース`
- 時間トリガーのタイプ:`1 時間ごと`

### 7. テスト

Google Apps Script エディタで:

1. **関数を選択 → testForwardEmail**
2. **実行(▶️ ボタン)** をクリック
3. **実行ログ** で結果を確認

## Notion API キーの取得

1. [Notion Integration Console](https://www.notion.so/my-integrations) にアクセス
2. **新しいインテグレーション** を作成
3. 内部統合トークンをコピー
4. GAS スクリプト プロパティに設定

## Notion Database ID の取得

1. Notion でデータベースを開く
2. URL を確認:`https://www.notion.so/{DATABASE_ID}?v=...`
3. `{DATABASE_ID}` をコピー(32文字)

## Properties(データベース構造)

Notion Database に以下のプロパティを作成:

| 名前 | タイプ | 説明 |
|------|--------|------|
| `Subject` | Title | メールの件名(必須) |
| `From` | Email | 送信元アドレス |
| `Received Date` | Date | 受信日時 |
| `Gmail Thread ID` | Text | Gmail スレッドID(検証用) |

## トラブルシューティング

### エラー: "Cannot read property 'getProperty' of null"
- スクリプト プロパティが設定されていないか確認
- GAS UI で再度プロパティを追加

### エラー: "401 Unauthorized"
- Notion API キーが正しいか確認
- トークンの有効期限を確認

### メールが転送されない
- Gmail ラベル「ToNotion」が正しく作成されているか確認
- GAS **実行ログ** でエラーメッセージを確認
- `testForwardEmail` で手動テストを実行
21 changes: 21 additions & 0 deletions gmail-notion-forwarder-2026-06-25/gas/appsscript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"timeZone": "Asia/Tokyo",
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"dependencies": {
"enabledAdvancedServices": [
{
"userSymbol": "Gmail",
"serviceId": "gmail",
"version": "v1"
}
]
},
"oauthScopes": [
"https://www.googleapis.com/auth/gmail.modify",
"https://www.googleapis.com/auth/script.external_request"
],
"executionApi": {
"access": "ANYONE"
}
}
Loading