@guayaba/workflow-piece-googlechat 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,111 @@
1
+ import { createTrigger, TriggerStrategy } from '@guayaba/workflows-framework';
2
+ import { propsValidation } from '@guayaba/workflows-common';
3
+ import { googleChatApiAuth, googleChatCommon } from '../common';
4
+ import { projectsDropdown, spacesDropdown, spacesMembersDropdown } from '../common/props';
5
+ import { googleChatAPIService } from '../common/requests';
6
+
7
+ export const newMention = createTrigger({
8
+ auth: googleChatApiAuth,
9
+ name: 'newMention',
10
+ displayName: 'New Mention',
11
+ description: 'Triggers when a new mention is received in Google Chat.',
12
+ props: {
13
+ projectId: projectsDropdown(['auth']),
14
+ spaceId: spacesDropdown({refreshers: ['auth']}),
15
+ spaceMemberId: spacesMembersDropdown(['auth', 'spaceId']),
16
+ },
17
+ sampleData: {},
18
+ type: TriggerStrategy.WEBHOOK,
19
+ async onEnable({ auth, propsValue, webhookUrl, store }) {
20
+ await propsValidation.validateZod(propsValue, googleChatCommon.newMentionTriggerSchema);
21
+
22
+ const { projectId, spaceId } = propsValue;
23
+ const accessToken = auth.access_token;
24
+
25
+ const topicName = `acp-topic-${Date.now()}`;
26
+ const subscriptionName = `acp-sub-${Date.now()}`;
27
+
28
+ await googleChatAPIService
29
+ .cleanupWebhookResources({
30
+ accessToken,
31
+ event_type: 'google.workspace.chat.message.v1.created',
32
+ projectId: projectId as string,
33
+ })
34
+ .catch((err) => {
35
+ console.log('Error cleaning up webhook resources', err);
36
+ });
37
+
38
+ await googleChatAPIService.createPubSubTopic({
39
+ accessToken,
40
+ projectId: projectId as string,
41
+ topicName,
42
+ });
43
+
44
+ await googleChatAPIService.grantTopicPermissions({
45
+ accessToken,
46
+ projectId: projectId as string,
47
+ topicName,
48
+ });
49
+
50
+ const targetResource = `//chat.googleapis.com/${
51
+ spaceId ? spaceId : 'spaces/-'
52
+ }`;
53
+
54
+ await googleChatAPIService.createWebhookSubscription({
55
+ accessToken,
56
+ projectId: projectId as string,
57
+ topic: topicName,
58
+ subscriptionName,
59
+ webhookUrl,
60
+ eventTypes: ['google.workspace.chat.message.v1.created'],
61
+ targetResource,
62
+ });
63
+ },
64
+ async onDisable({ auth, propsValue: { projectId }, store }) {
65
+ const accessToken = auth.access_token;
66
+
67
+ await googleChatAPIService
68
+ .cleanupWebhookResources({
69
+ accessToken,
70
+ event_type: 'google.workspace.chat.message.v1.created',
71
+ projectId: projectId as string,
72
+ })
73
+ .catch((err) => {
74
+ console.log('Error cleaning up webhook resources during disable', err);
75
+ });
76
+ },
77
+ async run(context) {
78
+ const messageData = JSON.parse(
79
+ Buffer.from(
80
+ (context.payload.body as any).message.data,
81
+ 'base64'
82
+ ).toString('utf-8')
83
+ );
84
+
85
+ const { spaceMemberId } = context.propsValue;
86
+
87
+ if (!messageData.message?.annotations) {
88
+ return [];
89
+ }
90
+
91
+ const mentions = messageData.message.annotations.filter(
92
+ (a: any) => a.type === 'USER_MENTION'
93
+ );
94
+
95
+ if (mentions.length === 0) {
96
+ return [];
97
+ }
98
+
99
+ if (spaceMemberId) {
100
+ const isMatch = mentions.some(
101
+ (m: any) => m.userMention?.user?.name === spaceMemberId
102
+ );
103
+
104
+ if (!isMatch) {
105
+ return [];
106
+ }
107
+ }
108
+
109
+ return [messageData];
110
+ },
111
+ });
@@ -0,0 +1,86 @@
1
+ import { createTrigger, TriggerStrategy } from '@guayaba/workflows-framework';
2
+ import { propsValidation } from '@guayaba/workflows-common';
3
+ import { googleChatApiAuth, googleChatCommon } from '../common';
4
+ import { projectsDropdown, spacesDropdown } from '../common/props';
5
+ import { googleChatAPIService } from '../common/requests';
6
+
7
+ export const newMessage = createTrigger({
8
+ auth: googleChatApiAuth,
9
+ name: 'newMessage',
10
+ displayName: 'New Message',
11
+ description: 'Triggers when a new message is received in Google Chat.',
12
+ props: {
13
+ projectId: projectsDropdown(['auth']),
14
+ spaceId: spacesDropdown({ refreshers: ['auth'] }),
15
+ },
16
+ sampleData: {},
17
+ type: TriggerStrategy.WEBHOOK,
18
+ async onEnable({ auth, propsValue, webhookUrl, store }) {
19
+ await propsValidation.validateZod(propsValue, googleChatCommon.newMessageTriggerSchema);
20
+
21
+ const { projectId, spaceId } = propsValue;
22
+ const accessToken = auth.access_token;
23
+
24
+ const topicName = `acp-topic-${Date.now()}`;
25
+ const subscriptionName = `acp-sub-${Date.now()}`;
26
+
27
+ await googleChatAPIService
28
+ .cleanupWebhookResources({
29
+ accessToken,
30
+ event_type: 'google.workspace.chat.message.v1.created',
31
+ projectId: projectId as string,
32
+ })
33
+ .catch((err) => {
34
+ console.log('Error cleaning up webhook resources', err);
35
+ });
36
+
37
+ await googleChatAPIService.createPubSubTopic({
38
+ accessToken,
39
+ projectId: projectId as string,
40
+ topicName,
41
+ });
42
+
43
+ await googleChatAPIService.grantTopicPermissions({
44
+ accessToken,
45
+ projectId: projectId as string,
46
+ topicName,
47
+ });
48
+
49
+ const targetResource = `//chat.googleapis.com/${
50
+ spaceId ? spaceId : 'spaces/-'
51
+ }`;
52
+
53
+ await googleChatAPIService.createWebhookSubscription({
54
+ accessToken,
55
+ projectId: projectId as string,
56
+ topic: topicName,
57
+ subscriptionName,
58
+ webhookUrl,
59
+ eventTypes: ['google.workspace.chat.message.v1.created'],
60
+ targetResource,
61
+ });
62
+ },
63
+ async onDisable({ auth, propsValue: { projectId }, store }) {
64
+ const accessToken = auth.access_token;
65
+
66
+ await googleChatAPIService
67
+ .cleanupWebhookResources({
68
+ accessToken,
69
+ event_type: 'google.workspace.chat.message.v1.created',
70
+ projectId: projectId as string,
71
+ })
72
+ .catch((err) => {
73
+ console.log('Error cleaning up webhook resources during disable', err);
74
+ });
75
+ },
76
+ async run(context) {
77
+ const messageData = JSON.parse(
78
+ Buffer.from(
79
+ (context.payload.body as any).message.data,
80
+ 'base64'
81
+ ).toString('utf-8')
82
+ );
83
+
84
+ return [messageData];
85
+ },
86
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "extends": "../../../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "module": "commonjs",
5
+ "forceConsistentCasingInFileNames": true,
6
+ "strict": true,
7
+ "importHelpers": true,
8
+ "noImplicitOverride": true,
9
+ "noImplicitReturns": true,
10
+ "noFallthroughCasesInSwitch": true,
11
+ "noPropertyAccessFromIndexSignature": true
12
+ },
13
+ "files": [],
14
+ "include": [],
15
+ "references": [
16
+ {
17
+ "path": "./tsconfig.lib.json"
18
+ }
19
+ ]
20
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": ".",
5
+ "baseUrl": ".",
6
+ "paths": {},
7
+ "outDir": "./dist",
8
+ "declaration": true,
9
+ "declarationMap": true,
10
+ "types": ["node"]
11
+ },
12
+ "include": ["src/**/*.ts"]
13
+ }