mirror of
https://github.com/langgenius/dify-plugin-sdks.git
synced 2026-07-22 10:25:23 -04:00
Gmail Trigger (Push + History)
Overview
- Gmail Trigger is a Dify provider that receives Gmail push notifications via Cloud Pub/Sub and emits concrete events based on Gmail History:
gmail_message_added(new messages)gmail_message_deleted(deleted messages)gmail_label_added(labels added to messages)gmail_label_removed(labels removed from messages)
- Dispatch verifies/auths the push, pulls the history delta once, and splits changes for events to consume.
- Note: API Key is NOT supported for Gmail API access. Use OAuth (gmail.readonly) only.
Prerequisites
- A Google account authorized for Gmail API (used during OAuth)
- A Google Cloud project with Pub/Sub enabled (manual setup)
Step-by-step Setup
- Install the plugin in Dify
- Options:
- If packaged as
.difypkg, import it in Dify’s Plugin Center (Plugins → Import). - For local run during development, ensure the runtime installs dependencies in
requirements.txt.
- If packaged as
- Configure OAuth in the provider (one-time)
- In Google Cloud Console → APIs & Services → Credentials → Create Credentials → OAuth client ID
- Application type: Web application
- Authorized redirect URIs: Copy the redirect URI shown by Dify when setting up OAuth for this provider
- Back in Dify, enter Client ID/Secret; click “Authorize” to complete OAuth (gmail.readonly)
- The constructor validates the token via
users/me/profile
- Create a Gmail subscription in Dify
- Parameters
watch_email(userId): usemeor the full Gmail addresstopic_name: Paste the full Pub/Sub topic path you created (e.g.,projects/<PROJECT_ID>/topics/<TOPIC>). If you configure tenant-level GCP client params (see below), you may leave this empty and the plugin will auto-provision Pub/Sub.label_ids(optional): scope to specific labels (INBOX/UNREAD...)label_filter_action(optional):includeorexcludefor users.watch- Properties (optional)
require_oidc: enforce OIDC bearer verificationoidc_audience: use the exact webhook endpoint URL (shown after subscription is created)oidc_service_account_email: the service account used by the Pub/Sub push subscription
- After you click Create, open the subscription details and copy the Webhook Endpoint URL. You will use this in Pub/Sub push.
- Prepare Pub/Sub (manual)
- Enable APIs:
- Create topic:
gcloud pubsub topics create <TOPIC> --project=<PROJECT_ID>
- Grant Gmail publisher on the topic:
gcloud pubsub topics add-iam-policy-binding projects/<PROJECT_ID>/topics/<TOPIC> \ --member=serviceAccount:gmail-api-push@system.gserviceaccount.com \ --role=roles/pubsub.publisher
- Create a Push subscription to the Callback URL displayed by Dify:
gcloud pubsub subscriptions create <SUB_NAME> \ --topic=projects/<PROJECT_ID>/topics/<TOPIC> \ --push-endpoint="https://<YOUR_DIFY_HOST>/api/plugin/triggers/<SUBSCRIPTION_ID>"- OIDC (optional): add
--push-auth-service-account=<YOUR_SA_EMAIL>and--push-auth-token-audience="<Callback URL>"
Auto mode (tenant-level)
- Configure in OAuth client params (one-time, tenant-level):
gcp_project_id: GCP project IDgcp_service_account_json: Service Account JSON with Pub/Sub admin perms- Optional
gcp_topic_id: default todify-gmail
- With these set, if
topic_nameis empty, the plugin will:- Create/reuse Topic and grant Gmail publisher
- Create a dedicated Push subscription per Dify subscription (supports OIDC)
- Call
users.watchwith the managed Topic
- On unsubscribe, the plugin will best-effort delete the managed Push subscription (Topic is preserved for reuse).
Where To Get OIDC Values
oidc_audience- Use the exact webhook endpoint URL shown in the Dify subscription details (Endpoint). Example:
https://<your-dify-host>/api/plugin/triggers/<subscription-id>
- The YAML field includes a URL to Google docs for the
audienceclaim: see the OIDC token reference.
- Use the exact webhook endpoint URL shown in the Dify subscription details (Endpoint). Example:
oidc_service_account_email- The service account used by the Pub/Sub push subscription (set via
--push-auth-service-account). - Find it under Google Cloud Console → IAM & Admin → Service Accounts, or via:
gcloud iam service-accounts list --format='value(email)'
- The YAML field links to the Service Accounts console page.
- The service account used by the Pub/Sub push subscription (set via
How It Works
- Dispatch (trigger)
- Optionally verifies OIDC bearer from Pub/Sub push (iss/aud/email)
- Decodes
message.datato gethistoryId/emailAddress - Calls
users.history.list(startHistoryId=...)once to gather deltas - Splits changes per family and stores pending batches in Dify storage
- Returns the list of concrete event names for Dify to execute
- Events
- Read the pending batch for their family
gmail_message_addedfetches full message metadata (headers, attachments meta)- Output matches the event YAML
output_schema
Event Outputs (high-level)
gmail_message_addedhistory_id: stringmessages[]: id, threadId, internalDate, snippet, sizeEstimate, labelIds, headers{From,To,Subject,Date,Message-Id}, has_attachments, attachments[]
gmail_message_deletedhistory_id: stringmessages[]: id, threadId
gmail_label_added/gmail_label_removedhistory_id: stringchanges[]: id, threadId, labelIds
Lifecycle & Refresh
- Create:
users.watchwithtopicName, optionallabelIds/labelFilterAction - Refresh:
users.watchagain before expiry (Gmail watch is time-limited). Ifexpirationnot provided, plugin targets ~6 days - Delete:
users.stop
Testing
- Send an email to the watched mailbox (INBOX). You should see
gmail_message_added - Mark read/unread (UNREAD label removed/added) triggers label events
- Delete an email triggers
gmail_message_deleted - Tip: You can view recent deliveries in Pub/Sub subscription details; Dify logs/console will show trigger and event traces.
Troubleshooting
- Nothing triggers: ensure topic exists, Gmail has publisher role on the topic, and push subscription points to Dify endpoint
- 401/403 at dispatch: OIDC settings mismatch; verify service account and audience
historyIdout of date: plugin resets checkpoint to the latest notification and skips the batch- OAuth issues: re-authorize the provider; token is validated by
users/me/profile
References