Building a Gmail to AI Assistant Pipeline with Pub/Sub and Webhooks
Set up real-time Gmail notifications that route through an AI assistant to filter noise, extract insights, and only alert you when it matters.
Tired of email noise? In this tutorial, we'll build a pipeline that watches your Gmail inbox in real-time, routes emails through an AI assistant, and only notifies you about what actually matters. Newsletters get summarized, receipts get ignored, and urgent emails get flagged immediately.
Key Takeaways
- Gmail's Push Notifications API with Google Pub/Sub enables real-time email monitoring without polling
- The
gogCLI simplifies Gmail watch setup and webhook forwarding - Cloudflare Tunnels provide free, instant HTTPS endpoints for local webhook receivers
- AI filtering can categorize emails and extract insights before they reach you
- The entire pipeline runs locally with minimal cloud infrastructure
Architecture Overview
Here's what we're building:
Prerequisites
Before we start, you'll need:
- A Google Cloud project with billing enabled
gcloudCLI installed and authenticated- Node.js 18+ (for the webhook handler)
- A Gmail account to monitor
1. Install the Required Tools
First, install gog (a Gmail CLI) and cloudflared:
2. Set Up Google Cloud Pub/Sub
Gmail's push notifications work through Google Pub/Sub. Let's set up the infrastructure:
3. Configure OAuth Credentials
You'll need OAuth credentials for gog to access Gmail:
- Go to Google Cloud Console → APIs & Credentials
- Click Create Credentials → OAuth client ID
- Select Desktop app as the application type
- Download the JSON credentials file and save it somewhere accessible (e.g.,
credentials.jsonin your project directory) - Configure
gogwith your credentials:
4. Start the Cloudflare Tunnel
Pub/Sub needs a public HTTPS endpoint to push notifications. Cloudflare Tunnels provide this for free:
You'll see output like:
Save this URL — you'll need it for the Pub/Sub subscription.
Note: Free Cloudflare tunnel URLs change on restart. For production, consider a named tunnel with a stable subdomain.
5. Create the Pub/Sub Subscription
Now create a push subscription that sends notifications to your tunnel:
6. Start the Gmail Watch
With gog, starting a Gmail watch is simple:
This tells Gmail to send notifications to your Pub/Sub topic whenever new emails arrive.
7. Run the Webhook Server
Now let's run gog's built-in webhook server to receive and forward notifications:
Key flags:
--token: Validates incoming Pub/Sub requests--hook-url: Where to forward processed emails--include-body: Include email body content (not just metadata)--max-bytes: Limit body size to avoid huge payloads
8. Build the AI Webhook Handler
Now for the fun part — the AI-powered webhook handler. Here's a Next.js API route example:
9. Environment Variables
Set up your environment:
10. Testing the Pipeline
Send yourself a test email and watch the logs:
When an email arrives, you should see:
- Pub/Sub pushes to your tunnel
gogfetches the email content- Your webhook receives the payload
- AI analyzes and categorizes
- Notification (or silence) based on importance
Customizing the AI Behavior
The real power is in customizing the AI prompt. Here are some ideas:
Strict Newsletter Filter
Smart Summarization
Priority Detection
Production Considerations
Before running this in production:
-
Stable Tunnel URL: Use a named Cloudflare tunnel or alternative (ngrok with reserved domain, Tailscale Funnel)
-
Error Handling: Add retry logic and dead-letter queues for failed webhook processing
-
Rate Limiting: Gmail has API quotas — batch process during high-volume periods
-
Token Refresh:
goghandles OAuth token refresh, but monitor for auth failures -
Cost Management: Each AI call costs money — consider caching or batching for high-volume inboxes
Conclusion
You've now built an AI-powered email pipeline that:
- Monitors Gmail in real-time via Pub/Sub
- Filters out noise automatically
- Extracts insights from valuable content
- Only notifies you when it matters
The same pattern works for other use cases:
- Slack message filtering
- GitHub notification triage
- RSS feed summarization
- Any webhook-driven data source
Key Takeaways:
- Gmail Pub/Sub + webhooks = real-time without polling
gogCLI simplifies the Gmail integration- Cloudflare Tunnels provide instant public endpoints
- AI classification turns noise into signal
- The whole stack runs locally with minimal infrastructure
Experiment with different AI prompts to tune the filtering to your workflow. The goal is inbox zero without the manual triage.
Damian Hodgkiss
Senior Staff Engineer at Sumo Group, leading development of AppSumo marketplace. Technical solopreneur with 25+ years of experience building SaaS products.