---
created: 2026-03-12
source: Rivet
tags: [agent-archive, rivet]
---

# Gmail Integration for Rivet Morning Brief

## Scripts

### `gmail-check.py`
Main script that:
1. Reads access token from `/root/.clawdbot/google-oauth.json`
2. Automatically refreshes token if expired (401 error)
3. Fetches up to 20 unread emails from Gmail API
4. Categorizes emails as URGENT, IMPORTANT, or LOW
5. Generates formatted summary
6. Saves output to `/home/ccuser/rateright-growth/rivet/memory/gmail-summary.md`

### `google-refresh.py`
Helper script that refreshes Google OAuth tokens using the refresh token.

## Usage

```bash
# Make scripts executable (already done)
chmod +x gmail-check.py google-refresh.py

# Run Gmail check
python3 gmail-check.py

# Run manually to refresh token
python3 google-refresh.py
```

## Integration with Morning Brief

The output file `gmail-summary.md` is formatted for inclusion in Rivet's morning brief. It includes:

- Total unread count with breakdown by category
- URGENT emails (from known contacts or containing urgent keywords)
- IMPORTANT emails (business-related)
- LOW priority emails (newsletters, promotions)

## Categorization Logic

### URGENT (🔴)
- From known contacts (admin@rateright.com.au, chris@rateright.com.au, etc.)
- Contains urgent keywords: "urgent", "asap", "immediate", "important", "invoice", "payment", "due", "deadline"

### IMPORTANT (🔵)
- Contains business keywords: "meeting", "call", "project", "proposal", "contract", "agreement", "business", "work"
- From business domains (.com, .au, .io, .co)

### LOW (⚪)
- Contains newsletter/promotion indicators: "newsletter", "promotion", "offer", "sale", "discount", "unsubscribe", "marketing"

## Token Management

Tokens are stored in `/root/.clawdbot/google-oauth.json`. The script automatically handles token refresh when needed.

## API Scope Required
- `https://www.googleapis.com/auth/gmail.modify` (read, send, delete, and manage labels)

## Testing

```bash
# Test Gmail API connectivity
python3 -c "import requests; import json; t=json.load(open('/root/.clawdbot/google-oauth.json'))['access_token']; r=requests.get('https://gmail.googleapis.com/gmail/v1/users/me/profile', headers={'Authorization': f'Bearer {t}'}); print(r.status_code, r.json())"
```

## Scheduling

Add to cron for automatic morning brief generation:

```bash
# Run daily at 8 AM
0 8 * * * cd /home/ccuser/rateright-growth/rivet/scripts && python3 gmail-check.py
```