4.6 KiB
Gmail Webhook Setup Guide
This guide explains how to set up automatic email detection and AI-powered replies using Gmail push notifications.
Overview
When a customer sends an email to your Gmail account, Gmail will send a push notification to our webhook endpoint. The webhook then:
- Fetches the incoming email
- Analyzes it using your store's knowledge base
- Generates an AI-powered reply
- Sends the reply automatically
Prerequisites
- Gmail Integration: Your Gmail account must be connected via
/dashboard/juno-engine/gmail-connect - Knowledge Base: Your store must have FAQs or documents in the knowledge base
- Google Cloud Pub/Sub: Required for Gmail push notifications
Step 1: Set Up Google Cloud Pub/Sub
Gmail push notifications require Google Cloud Pub/Sub. Follow these steps:
1.1 Create a Pub/Sub Topic
- Go to Google Cloud Console
- Navigate to Pub/Sub > Topics
- Click Create Topic
- Name it
gmail-notifications(or any name you prefer) - Click Create
1.2 Create a Subscription
- In the topic page, click Create Subscription
- Name it
gmail-webhook-subscription - Choose Push delivery type
- Set the Endpoint URL to:
https://yourdomain.com/api/webhooks/gmail- Replace
yourdomain.comwith your actual domain - For local development with ngrok:
https://your-ngrok-url.ngrok-free.dev/api/webhooks/gmail
- Replace
- Click Create
1.3 Grant Gmail API Access
- Go to IAM & Admin > Service Accounts
- Find or create a service account for Gmail API
- Grant it the
pubsub.topics.publishpermission on your topic
1.4 Get Topic Name
Copy the full topic name in the format:
projects/YOUR_PROJECT_ID/topics/gmail-notifications
Step 2: Configure Environment Variables
Add the Pub/Sub topic name to your .env file:
GMAIL_PUBSUB_TOPIC=projects/YOUR_PROJECT_ID/topics/gmail-notifications
Step 3: Enable Gmail Watch
Once Pub/Sub is set up, enable Gmail push notifications:
Option A: Via API
Make a POST request to:
POST /api/integrations/gmail/watch
This will set up Gmail to send push notifications to your webhook.
Option B: Via UI (Recommended)
Add a button in the Gmail connect page to enable webhooks (coming soon).
Step 4: Verify Webhook
Gmail will send a verification request to your webhook endpoint. The endpoint automatically handles verification by returning the challenge token.
How It Works
Webhook Flow
- Customer sends email → Gmail receives it
- Gmail sends push notification → Webhook endpoint receives notification
- Webhook processes email:
- Fetches the email content
- Extracts sender, subject, and body
- Searches knowledge base for relevant information
- Generates AI-powered reply using Bedrock
- Reply is sent → Customer receives automated response
- Email is marked as read → Prevents duplicate processing
Email Processing Logic
- Only processes emails from external senders (not same domain)
- Uses knowledge base to find relevant answers
- Generates contextual replies using AI
- Maintains email threading (replies are properly threaded)
- Logs all sent emails for tracking
Monitoring
Check logs for:
- Webhook notifications received
- Emails processed
- Replies sent
- Any errors during processing
Troubleshooting
Webhook Not Receiving Notifications
- Verify Pub/Sub topic exists and is configured correctly
- Check that Gmail watch is active (watch expires after 7 days)
- Verify webhook URL is accessible (not behind firewall)
- Check Google Cloud Console logs for Pub/Sub errors
Replies Not Being Sent
- Verify Gmail integration is connected
- Check that store has active knowledge base content
- Review error logs for specific issues
- Ensure Bedrock API credentials are configured
Watch Expired
Gmail watch requests expire after 7 days. You need to renew them:
- Call
/api/integrations/gmail/watchagain - Or set up a cron job to renew automatically
Security
- Webhook endpoint verifies Gmail notifications
- Only processes emails for authenticated users
- Validates store ownership before processing
- Logs all actions for audit trail
Limitations
- Gmail watch expires after 7 days (needs renewal)
- Only processes unread emails in inbox
- Requires active knowledge base for quality replies
- May have rate limits from Gmail API
Next Steps
- Set up automatic watch renewal (cron job)
- Add email filtering rules (only reply to certain senders)
- Implement reply templates
- Add analytics dashboard for email metrics