feat(gmail): add new emails trigger

This commit is contained in:
Rıdvan Akca
2024-04-22 14:46:19 +02:00
parent b594a8e0f3
commit b6b4ed5ad2
8 changed files with 115 additions and 0 deletions

View File

@@ -36,6 +36,7 @@ const verifyCredentials = async ($) => {
refreshToken: data.refresh_token,
resourceName: currentUser.resourceName,
screenName: `${displayName} - ${email}`,
userId: email,
});
};

View File

@@ -0,0 +1,3 @@
import listLabels from './list-labels/index.js';
export default [listLabels];

View File

@@ -0,0 +1,22 @@
export default {
name: 'List labels',
key: 'listLabels',
async run($) {
const labels = {
data: [],
};
const userId = $.auth.data.userId;
const { data } = await $.http.get(`/gmail/v1/users/${userId}/labels`);
for (const label of data.labels) {
labels.data.push({
value: label.id,
name: label.name,
});
}
return labels;
},
};

View File

@@ -1,6 +1,9 @@
import defineApp from '../../helpers/define-app.js';
import addAuthHeader from './common/add-auth-header.js';
import auth from './auth/index.js';
import triggers from './triggers/index.js';
import dynamicData from './dynamic-data/index.js';
export default defineApp({
name: 'Gmail',
key: 'gmail',
@@ -12,4 +15,6 @@ export default defineApp({
supportsConnections: true,
beforeRequest: [addAuthHeader],
auth,
triggers,
dynamicData,
});

View File

@@ -0,0 +1,3 @@
import newEmails from './new-emails/index.js';
export default [newEmails];

View File

@@ -0,0 +1,68 @@
import defineTrigger from '../../../../helpers/define-trigger.js';
export default defineTrigger({
name: 'New emails',
key: 'newEmails',
pollInterval: 15,
description:
'Triggers when a new email is received in the specified mailbox.',
arguments: [
{
label: 'Label',
key: 'labelId',
type: 'dropdown',
required: false,
description:
"If you don't choose a label, this Zap will trigger for all emails, including Drafts.",
variables: true,
source: {
type: 'query',
name: 'getDynamicData',
arguments: [
{
name: 'key',
value: 'listLabels',
},
],
},
},
],
async run($) {
const userId = $.auth.data.userId;
const labelId = $.step.parameters.labelId;
const params = {
maxResults: 500,
pageToken: undefined,
};
if (labelId) {
params.labelIds = labelId;
}
do {
const { data } = await $.http.get(`/gmail/v1/users/${userId}/messages`, {
params,
});
params.pageToken = data.nextPageToken;
if (!data?.messages?.length) {
return;
}
for (const message of data.messages) {
const { data: messageData } = await $.http.get(
`/gmail/v1/users/${userId}/messages/${message.id}`
);
$.pushTriggerItem({
raw: messageData,
meta: {
internalId: messageData.id,
},
});
}
} while (params.pageToken);
},
});

View File

@@ -0,0 +1,12 @@
---
favicon: /favicons/gmail.svg
items:
- name: New emails
desc: Triggers when a new email is received in the specified mailbox.
---
<script setup>
import CustomListing from '../../components/CustomListing.vue'
</script>
<CustomListing />

View File

@@ -14,6 +14,7 @@ The following integrations are currently supported by Automatisch.
- [Ghost](/apps/ghost/triggers)
- [GitHub](/apps/github/triggers)
- [GitLab](/apps/gitlab/triggers)
- [Gmail](/apps/gmail/triggers)
- [Google Calendar](/apps/google-calendar/triggers)
- [Google Drive](/apps/google-drive/triggers)
- [Google Forms](/apps/google-forms/triggers)