|
| 1 | +--- |
| 2 | +description: How do I trigger scripts and flows from Nextcloud or Google Drive/Calendar events in Windmill? |
| 3 | +--- |
| 4 | + |
| 5 | +import DocCard from '@site/src/components/DocCard'; |
| 6 | + |
| 7 | +# Native triggers |
| 8 | + |
| 9 | +Native triggers allow external services to push events directly to Windmill and trigger [scripts](../../script_editor/index.mdx) and [flows](../../flows/1_flow_editor.mdx). Unlike [scheduled polls](../../flows/10_flow_trigger.mdx), native triggers receive real-time push notifications from the external service via webhooks, so your runnables execute as soon as events occur. |
| 10 | + |
| 11 | +Currently supported services: |
| 12 | + |
| 13 | +- [Nextcloud](#nextcloud-triggers) - file, folder, and calendar change events |
| 14 | +- [Google](#google-triggers) - drive and calendar change events |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## How it works |
| 19 | + |
| 20 | +Native triggers use [OAuth](../../misc/2_setup_oauth/index.mdx) to authenticate with the external service. Each trigger registers a webhook or watch channel on the external service, which sends notifications to Windmill when events occur. Windmill then executes the configured script or flow with the event data. |
| 21 | + |
| 22 | +The general setup follows two steps: |
| 23 | + |
| 24 | +1. **Connect the service** via workspace settings (OAuth authentication) |
| 25 | +2. **Create a trigger** on a script or flow, selecting the service and configuring which events to watch |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## Setup |
| 30 | + |
| 31 | +### Workspace integration |
| 32 | + |
| 33 | +Before creating native triggers, you need to connect the external service in your workspace settings. |
| 34 | + |
| 35 | +1. Go to **Workspace settings** > **Integrations** > **Native triggers** |
| 36 | +2. Click **Connect** on the service you want to use (Nextcloud or Google) |
| 37 | +3. Provide the [OAuth](../../misc/2_setup_oauth/index.mdx) credentials (client ID and client secret) |
| 38 | +4. Complete the OAuth authorization flow |
| 39 | +5. Choose a [resource](../3_resources_and_types/index.mdx) path where the connection will be stored in the workspace |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +The integration creates a [resource](../3_resources_and_types/index.mdx) in your workspace at the path you specify. The OAuth token is refreshed automatically, so you don't need to re-authenticate manually. You can use this resource in your scripts to query the service API with the same credentials used by the trigger. |
| 44 | + |
| 45 | +For Google integrations, the created [resource](../3_resources_and_types/index.mdx) is of type `gworkspace`. The OAuth scopes used are: |
| 46 | +- `https://www.googleapis.com/auth/drive.readonly` |
| 47 | +- `https://www.googleapis.com/auth/calendar.readonly` |
| 48 | +- `https://www.googleapis.com/auth/calendar.events` |
| 49 | + |
| 50 | +If you need additional scopes (e.g. write access), set up a separate resource with the desired scopes. |
| 51 | + |
| 52 | +A superadmin can share [instance-level](../../advanced/18_instance_settings/index.mdx) Google workspace settings so that workspace admins can connect Google native triggers without configuring their own OAuth client. |
| 53 | +The credentials are not exposed to workspace admins. |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +For Nextcloud, you also need to provide the base URL of your Nextcloud instance. |
| 58 | + |
| 59 | +### Create a trigger |
| 60 | + |
| 61 | +Once the workspace integration is configured: |
| 62 | + |
| 63 | +1. Go to **Nextcloud** or **Google** tab (only visible once the corresponding [workspace integration](#workspace-integration) is configured) |
| 64 | +2. Click **New trigger** |
| 65 | +3. Select the script or flow to trigger |
| 66 | +4. Configure the service-specific options (see sections below) |
| 67 | +5. Save the trigger |
| 68 | + |
| 69 | +You can also create native triggers directly from a script or flow's **Triggers** tab. |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +## Nextcloud triggers |
| 74 | + |
| 75 | +Nextcloud native triggers watch for file, folder, and calendar changes on a [Nextcloud](https://nextcloud.com/) instance and trigger a script or flow when events occur. |
| 76 | + |
| 77 | +### Prerequisites |
| 78 | + |
| 79 | +- Nextcloud 33 or later |
| 80 | +- The [Windmill integration app](https://apps.nextcloud.com/apps/integration_windmill) installed on your Nextcloud instance |
| 81 | +- [Pretty URLs](https://docs.nextcloud.com/server/latest/admin_manual/installation/source_installation.html#pretty-urls) enabled on your Nextcloud instance |
| 82 | + |
| 83 | +### Configuration |
| 84 | + |
| 85 | +When creating a Nextcloud trigger, pick a script or flow (or use **Create from template**), then configure: |
| 86 | + |
| 87 | +- **Event** - select the event type to listen for. Available events include: |
| 88 | + - Calendar events: object created, moved, trashed, restored, or changed in a Nextcloud calendar |
| 89 | + - File/folder events: node created, changed, or written |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +### Event payload |
| 94 | + |
| 95 | +Nextcloud sends the full event data to your script. The payload includes the authenticated user, a timestamp, and the event details: |
| 96 | + |
| 97 | +```typescript |
| 98 | +export async function main( |
| 99 | + authentication: { |
| 100 | + owner: RT.Nextcloud; |
| 101 | + trigger: RT.Nextcloud; |
| 102 | + }, |
| 103 | + user: { |
| 104 | + uid: string; |
| 105 | + displayName: string; |
| 106 | + }, |
| 107 | + time: number, |
| 108 | + event: any |
| 109 | +) { |
| 110 | + // event contains the Nextcloud event details (file, folder, or calendar object) |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +--- |
| 115 | + |
| 116 | +## Google triggers |
| 117 | + |
| 118 | +Google native triggers watch for changes in [Google Drive](https://drive.google.com/) or [Google Calendar](https://calendar.google.com/) and trigger a script or flow when events occur. |
| 119 | + |
| 120 | +### Prerequisites |
| 121 | + |
| 122 | +- A Google Cloud project with the relevant API enabled ([Drive API](https://developers.google.com/drive/api), [Calendar API](https://developers.google.com/calendar/api), or both) |
| 123 | +- [OAuth 2.0](https://console.cloud.google.com/apis/credentials) credentials (client ID and client secret) configured with the appropriate scopes |
| 124 | + |
| 125 | +### Configuration |
| 126 | + |
| 127 | +When creating a Google trigger, pick a script or flow (or use **Create from template**), then configure: |
| 128 | + |
| 129 | +- **Trigger Type** - choose between **Drive** and **Calendar** |
| 130 | +- **Watch Mode** (Drive only) - choose between: |
| 131 | + - **Specific file** - watch a single file or folder from My Drive, Shared with me, or Shared drives |
| 132 | + - **All changes** - watch all changes across your Google Drive (fires whenever any file is created, modified, or deleted) |
| 133 | +- **Calendar** (Calendar only) - select a calendar from your Google account to watch for event changes |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | +Both use push notifications via Google watch channels. Windmill automatically renews channels before they expire. The expiration period differs by service: 24 hours for Drive, 7 days for Calendar. |
| 138 | + |
| 139 | +### Event payload |
| 140 | + |
| 141 | +Google push notifications only contain metadata about the change, not the full event details. To get the actual content of the change, use the [`gworkspace` resource](../3_resources_and_types/index.mdx) created during workspace integration to query the Google API. |
| 142 | + |
| 143 | +```typescript |
| 144 | +type GoogleTriggerPayload = { |
| 145 | + channel_id: string; |
| 146 | + resource_id: string; |
| 147 | + resource_state: string; // "sync" | "exists" | "not_exists" | "update" |
| 148 | + resource_uri: string; |
| 149 | + message_number: string; |
| 150 | + channel_expiration: string; |
| 151 | + channel_token: string; // custom token set when creating the watch |
| 152 | + changed: string; // Drive-only: comma-separated list (e.g. "content,properties,permissions") |
| 153 | +}; |
| 154 | + |
| 155 | +export async function main(payload: GoogleTriggerPayload) { |
| 156 | + // Use the gworkspace resource to query the Google API for full details |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +Use the [Google native trigger template script](https://hub.windmill.dev/scripts/gworkspace/22221/google-native-trigger-template-script-gworkspace) (also available from the UI when creating a trigger) as a starting point. |
| 161 | + |
| 162 | +--- |
0 commit comments