@openfn/language-gmail 1.1.4 → 1.3.0
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.
- package/README.md +74 -15
- package/ast.json +65 -0
- package/dist/index.cjs +93 -11
- package/dist/index.js +92 -11
- package/package.json +4 -4
- package/types/Adaptor.d.ts +54 -2
- package/types/Utils.d.ts +1 -0
package/README.md
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
# Gmail
|
|
1
|
+
# Gmail adaptor
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Send and receive Gmail messages with file attachments using simple, configurable logic.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
# `getContentsFromMessages`
|
|
6
|
+
|
|
7
|
+
This function is used to extract specific content from Gmail messages using custom desired "content" configurations. The sample code specifies how to query Gmail for messages and identify desired attachments and metadata.
|
|
6
8
|
|
|
7
9
|
Without any parameters, the `getContentsFromMessages()` function will return an array containing every message in the account of the authenticated user including `from`, `date` and `subject`.
|
|
8
10
|
|
|
9
11
|
A number of options are available to isolated the desired messages and to customize the output.
|
|
10
12
|
|
|
11
|
-
##
|
|
13
|
+
## Parameters
|
|
12
14
|
|
|
13
|
-
Optional parameters include: `contents`, `query`, `email`, `processedIds`, `maxResults`
|
|
15
|
+
An `options` object can configure the results of the function call. Optional parameters include: `contents`, `query`, `email`, `processedIds`, `maxResults`
|
|
14
16
|
|
|
15
17
|
### options.contents
|
|
16
18
|
|
|
@@ -90,8 +92,8 @@ Use a `query` parameter to filter the messages returned.
|
|
|
90
92
|
|
|
91
93
|
The query syntax supports the same query format as the Gmail `search` box.
|
|
92
94
|
|
|
93
|
-
```
|
|
94
|
-
options.query = 'from:
|
|
95
|
+
```js
|
|
96
|
+
options.query = 'from:ple.com rfc822msgid:<somemsgid@example.com> is:unread';
|
|
95
97
|
```
|
|
96
98
|
|
|
97
99
|
A full list of supported search operations can be found here: [Refine searches in Gmail](https://support.google.com/mail/answer/7190)
|
|
@@ -100,7 +102,7 @@ A full list of supported search operations can be found here: [Refine searches i
|
|
|
100
102
|
|
|
101
103
|
Optionally specify the email address used for the Gmail account. This almost always the same email associated with the authenticated user so this parameter is optional.
|
|
102
104
|
|
|
103
|
-
```
|
|
105
|
+
```js
|
|
104
106
|
options.email = '<EMAIL>';
|
|
105
107
|
```
|
|
106
108
|
|
|
@@ -108,7 +110,7 @@ options.email = '<EMAIL>';
|
|
|
108
110
|
|
|
109
111
|
In some scenarios, it may be necessary to skip certain messages to prevent the retrieval of duplicate data. Passing an array of messageIds will allow the function to skip these messages if any of the ids are encountered in the returned messages.
|
|
110
112
|
|
|
111
|
-
```
|
|
113
|
+
```js
|
|
112
114
|
options.processedIds = [
|
|
113
115
|
'194e3cf1ca0ccd66',
|
|
114
116
|
'283e2df2ca0ecd75',
|
|
@@ -161,7 +163,7 @@ const contents = [metadataFile, dataFile];
|
|
|
161
163
|
getContentsFromMessages({ query, email, contents });
|
|
162
164
|
```
|
|
163
165
|
|
|
164
|
-
## Sample `state.data`
|
|
166
|
+
## Sample `state.data` output
|
|
165
167
|
|
|
166
168
|
For each matched message, the extracted content is returned as a message object of content properties. Here's an example `state.data` for a single matched message:
|
|
167
169
|
|
|
@@ -193,13 +195,68 @@ Each property on the message object represents a specific piece of information e
|
|
|
193
195
|
- **metadata**: Metadata-named file content, with its matched file name.
|
|
194
196
|
- **data**: Data-named archive file content, with its matched archive name and file name.
|
|
195
197
|
|
|
196
|
-
|
|
198
|
+
# `sendMessage`
|
|
199
|
+
|
|
200
|
+
Use `sendMessage()` to send an email with optional file attachments. This function supports plain text messages as well as attachments and archives.
|
|
201
|
+
|
|
202
|
+
## Parameters
|
|
203
|
+
|
|
204
|
+
Pass a single `message` object with the following fields:
|
|
205
|
+
|
|
206
|
+
- `to` (string): Required. The recipient's email address.
|
|
207
|
+
- `subject` (string): Required. The email subject.
|
|
208
|
+
- `body` (string): Required. The message body as plain text.
|
|
209
|
+
- `attachments` (array): Optional. List of file attachments or archives.
|
|
210
|
+
|
|
211
|
+
Each item in the `attachments` array must include:
|
|
212
|
+
|
|
213
|
+
- `filename` (string): The name of the file.
|
|
214
|
+
- Either `content` or `archive`:
|
|
215
|
+
- `content` (string): The file content.
|
|
216
|
+
- `archive` (array): Use this to send a `.zip` file. Provide an array of `{ filename, content }` objects.
|
|
217
|
+
|
|
218
|
+
## Example jobs
|
|
219
|
+
|
|
220
|
+
```js
|
|
221
|
+
sendMessage({
|
|
222
|
+
to: "recipient@gmail.com",
|
|
223
|
+
subject: "Device Summary",
|
|
224
|
+
body: "Here is the latest device summary.",
|
|
225
|
+
attachments: [
|
|
226
|
+
{
|
|
227
|
+
filename: "summary.txt",
|
|
228
|
+
content: "This is the summary file.",
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
filename: "report.json",
|
|
232
|
+
content: '{ "status": "OK" }',
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
filename: "data.zip",
|
|
236
|
+
archive: [
|
|
237
|
+
{
|
|
238
|
+
filename: "one.json",
|
|
239
|
+
content: '{ "value": 1 }',
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
filename: "two.json",
|
|
243
|
+
content: '{ "value": 2 }',
|
|
244
|
+
},
|
|
245
|
+
],
|
|
246
|
+
},
|
|
247
|
+
],
|
|
248
|
+
});
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
This will send an email with two plain attachments and one ZIP archive containing two files.
|
|
252
|
+
|
|
253
|
+
# Acquiring an access token
|
|
197
254
|
|
|
198
255
|
The Gmail adaptor implicitly uses the Gmail account of the Google account that is used to authenticate the application.
|
|
199
256
|
|
|
200
257
|
Allowing the Gmail adaptor to access a Gmail account is a multi-step process.
|
|
201
258
|
|
|
202
|
-
|
|
259
|
+
## Create an OAuth 2.0 client ID
|
|
203
260
|
|
|
204
261
|
Follow the instructions are found here:
|
|
205
262
|
https://support.google.com/googleapi/answer/6158849
|
|
@@ -215,7 +272,7 @@ https://support.google.com/googleapi/answer/6158849
|
|
|
215
272
|
- Click "Create"
|
|
216
273
|
- On the resulting popup screen, find and click "DOWNLOAD JSON" and save this file to a secure location.
|
|
217
274
|
|
|
218
|
-
|
|
275
|
+
## Use the Postman application to query the OAuth enpoint and retrieve an access token
|
|
219
276
|
|
|
220
277
|
Initially, you'll need to configure an authentication request using Postman's built-in OAuth 2.0 implementation:
|
|
221
278
|
|
|
@@ -232,7 +289,9 @@ Initially, you'll need to configure an authentication request using Postman's bu
|
|
|
232
289
|
- Access Token URL: (found in the json file as token_url)
|
|
233
290
|
- Client ID: (found in the json file as client_id)
|
|
234
291
|
- Client Secret: (found in the json file as client_secret)
|
|
235
|
-
- Scope:
|
|
292
|
+
- Scope:
|
|
293
|
+
https://www.googleapis.com/auth/gmail.readonly
|
|
294
|
+
https://www.googleapis.com/auth/gmail.send
|
|
236
295
|
- State: (any random string is fine)
|
|
237
296
|
- Client Authentication: Send as Basic Auth header
|
|
238
297
|
|
|
@@ -245,7 +304,7 @@ access token:
|
|
|
245
304
|
- In the MANAGE ACCESS TOKENS popup, find and copy the new Access Token
|
|
246
305
|
- This access token will be valid for 1 hour.
|
|
247
306
|
|
|
248
|
-
|
|
307
|
+
## Configure OpenFn CLI to find the access token
|
|
249
308
|
|
|
250
309
|
The Gmail adaptor looks for the access token in the configuration section under `access_token`.
|
|
251
310
|
|
package/ast.json
CHANGED
|
@@ -56,6 +56,71 @@
|
|
|
56
56
|
]
|
|
57
57
|
},
|
|
58
58
|
"valid": true
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
"name": "sendMessage",
|
|
62
|
+
"params": [
|
|
63
|
+
"message"
|
|
64
|
+
],
|
|
65
|
+
"docs": {
|
|
66
|
+
"description": "Sends a Gmail message using the provided configuration.\nSupports attachments and standard email fields like subject, body, and recipients.",
|
|
67
|
+
"tags": [
|
|
68
|
+
{
|
|
69
|
+
"title": "public",
|
|
70
|
+
"description": null,
|
|
71
|
+
"type": null
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
"title": "function",
|
|
75
|
+
"description": null,
|
|
76
|
+
"name": null
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
"title": "param",
|
|
80
|
+
"description": "The message configuration object or array of objects.",
|
|
81
|
+
"type": {
|
|
82
|
+
"type": "UnionType",
|
|
83
|
+
"elements": [
|
|
84
|
+
{
|
|
85
|
+
"type": "NameExpression",
|
|
86
|
+
"name": "SendMessageOptions"
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
"type": "TypeApplication",
|
|
90
|
+
"expression": {
|
|
91
|
+
"type": "NameExpression",
|
|
92
|
+
"name": "Array"
|
|
93
|
+
},
|
|
94
|
+
"applications": [
|
|
95
|
+
{
|
|
96
|
+
"type": "NameExpression",
|
|
97
|
+
"name": "SendMessageOptions"
|
|
98
|
+
}
|
|
99
|
+
]
|
|
100
|
+
}
|
|
101
|
+
]
|
|
102
|
+
},
|
|
103
|
+
"name": "message"
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
"title": "state",
|
|
107
|
+
"description": "{Object} data - The Gmail API response from sending the message."
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
"title": "returns",
|
|
111
|
+
"description": null,
|
|
112
|
+
"type": {
|
|
113
|
+
"type": "NameExpression",
|
|
114
|
+
"name": "Operation"
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
"title": "example",
|
|
119
|
+
"description": "sendMessage({\n to: 'recipient@example.org',\n subject: 'Test Message',\n body: 'Hello from OpenFn!',\n attachments: [\n { filename: 'test.txt', content: 'Some text content' }\n ]\n})"
|
|
120
|
+
}
|
|
121
|
+
]
|
|
122
|
+
},
|
|
123
|
+
"valid": true
|
|
59
124
|
}
|
|
60
125
|
],
|
|
61
126
|
"exports": [],
|
package/dist/index.cjs
CHANGED
|
@@ -41,6 +41,7 @@ __export(src_exports, {
|
|
|
41
41
|
http: () => import_language_common2.http,
|
|
42
42
|
lastReferenceValue: () => import_language_common2.lastReferenceValue,
|
|
43
43
|
merge: () => import_language_common2.merge,
|
|
44
|
+
sendMessage: () => sendMessage,
|
|
44
45
|
sourceValue: () => import_language_common2.sourceValue
|
|
45
46
|
});
|
|
46
47
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -63,14 +64,16 @@ __export(Adaptor_exports, {
|
|
|
63
64
|
http: () => import_language_common2.http,
|
|
64
65
|
lastReferenceValue: () => import_language_common2.lastReferenceValue,
|
|
65
66
|
merge: () => import_language_common2.merge,
|
|
67
|
+
sendMessage: () => sendMessage,
|
|
66
68
|
sourceValue: () => import_language_common2.sourceValue
|
|
67
69
|
});
|
|
68
70
|
var import_language_common = require("@openfn/language-common");
|
|
69
71
|
var import_util = require("@openfn/language-common/util");
|
|
70
72
|
|
|
71
73
|
// src/Utils.js
|
|
72
|
-
var
|
|
74
|
+
var import_jszip = __toESM(require("jszip"), 1);
|
|
73
75
|
var import_googleapis = require("googleapis");
|
|
76
|
+
var SEND_MESSAGE_BOUNDARY = "----=_Part_0_123456789.123456789";
|
|
74
77
|
var gmail;
|
|
75
78
|
async function getMessagesResult(userId, query, pageToken) {
|
|
76
79
|
try {
|
|
@@ -144,6 +147,72 @@ async function getMessageContent(message, desiredContent) {
|
|
|
144
147
|
return `Unsupported content type: ${desiredContent.type}`;
|
|
145
148
|
}
|
|
146
149
|
}
|
|
150
|
+
async function sendMessageWithAttachments(message) {
|
|
151
|
+
const attachments = await parseAttachments(message.attachments);
|
|
152
|
+
const lines = [
|
|
153
|
+
`To: ${message.to}`,
|
|
154
|
+
`Subject: ${message.subject}`,
|
|
155
|
+
"MIME-Version: 1.0",
|
|
156
|
+
`Content-Type: multipart/mixed; boundary="${SEND_MESSAGE_BOUNDARY}"`,
|
|
157
|
+
"",
|
|
158
|
+
`--${SEND_MESSAGE_BOUNDARY}`,
|
|
159
|
+
'Content-Type: text/plain; charset="UTF-8"',
|
|
160
|
+
"Content-Transfer-Encoding: 7bit",
|
|
161
|
+
"",
|
|
162
|
+
message.body
|
|
163
|
+
];
|
|
164
|
+
for (const attachment of attachments) {
|
|
165
|
+
const file = attachment.filename;
|
|
166
|
+
lines.push(
|
|
167
|
+
`--${SEND_MESSAGE_BOUNDARY}`,
|
|
168
|
+
`Content-Type: application/octet-stream; name="${file}"`,
|
|
169
|
+
"Content-Transfer-Encoding: base64",
|
|
170
|
+
`Content-Disposition: attachment; filename="${file}"`,
|
|
171
|
+
"",
|
|
172
|
+
attachment.content
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
lines.push(`--${SEND_MESSAGE_BOUNDARY}--`, "");
|
|
176
|
+
const rawMessage = lines.join("\r\n");
|
|
177
|
+
const encodedMessage = Buffer.from(rawMessage).toString("base64");
|
|
178
|
+
try {
|
|
179
|
+
const result = await gmail.users.messages.send({
|
|
180
|
+
userId: "me",
|
|
181
|
+
requestBody: { raw: encodedMessage }
|
|
182
|
+
});
|
|
183
|
+
return result.data;
|
|
184
|
+
} catch (error) {
|
|
185
|
+
throw new Error("Error fetching messages: " + error.message);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
async function parseAttachments(attachments) {
|
|
189
|
+
if (!attachments)
|
|
190
|
+
return null;
|
|
191
|
+
const parsedAttachments = [];
|
|
192
|
+
for (const attachment of attachments) {
|
|
193
|
+
if (attachment.archive) {
|
|
194
|
+
const archiveAttachment = await parseArchiveAttachment(attachment);
|
|
195
|
+
parsedAttachments.push(archiveAttachment);
|
|
196
|
+
} else {
|
|
197
|
+
parsedAttachments.push(attachment);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return parsedAttachments;
|
|
201
|
+
}
|
|
202
|
+
async function parseArchiveAttachment(attachment) {
|
|
203
|
+
const zip = new import_jszip.default();
|
|
204
|
+
for (const file of attachment.archive) {
|
|
205
|
+
zip.file(file.filename, file.content, {
|
|
206
|
+
compression: "DEFLATE",
|
|
207
|
+
compressionOptions: { level: 9 }
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
const content = await zip.generateAsync({ type: "base64" });
|
|
211
|
+
return {
|
|
212
|
+
filename: attachment.filename,
|
|
213
|
+
content
|
|
214
|
+
};
|
|
215
|
+
}
|
|
147
216
|
function createConnection(state) {
|
|
148
217
|
const { access_token } = state.configuration;
|
|
149
218
|
const auth = new import_googleapis.google.auth.OAuth2();
|
|
@@ -203,20 +272,19 @@ async function extractFileFromArchiveAttachment(attachment, desiredContent) {
|
|
|
203
272
|
return null;
|
|
204
273
|
}
|
|
205
274
|
const compressedBuffer = Buffer.from(attachment.data, "base64");
|
|
206
|
-
const
|
|
207
|
-
const
|
|
208
|
-
(
|
|
275
|
+
const zip = await import_jszip.default.loadAsync(compressedBuffer);
|
|
276
|
+
const filename = Object.keys(zip.files).find(
|
|
277
|
+
(name) => isExpressionMatch(name, desiredContent.file)
|
|
209
278
|
);
|
|
210
|
-
if (!
|
|
279
|
+
if (!filename) {
|
|
211
280
|
console.info(`File not found in the archive for: ${desiredContent.file}`);
|
|
212
281
|
return null;
|
|
213
282
|
}
|
|
214
|
-
const
|
|
215
|
-
const
|
|
216
|
-
const fileContent = Buffer.from(fileString, "base64").toString("utf-8");
|
|
283
|
+
const file = zip.files[filename];
|
|
284
|
+
const fileContent = await file.async("string");
|
|
217
285
|
return {
|
|
218
286
|
archiveFilename: attachment.filename,
|
|
219
|
-
filename
|
|
287
|
+
filename,
|
|
220
288
|
content: desiredContent.maxLength ? fileContent.substring(0, desiredContent.maxLength) : fileContent
|
|
221
289
|
};
|
|
222
290
|
}
|
|
@@ -338,11 +406,24 @@ function getContentsFromMessages(options) {
|
|
|
338
406
|
}
|
|
339
407
|
}
|
|
340
408
|
} while (nextPageToken);
|
|
341
|
-
|
|
409
|
+
return {
|
|
342
410
|
...(0, import_language_common.composeNextState)(state, contents),
|
|
343
411
|
processedIds: newIds
|
|
344
412
|
};
|
|
345
|
-
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
function sendMessage(message) {
|
|
416
|
+
return async (state) => {
|
|
417
|
+
const [resolvedMessage] = (0, import_util.expandReferences)(state, message);
|
|
418
|
+
const messages = Array.isArray(resolvedMessage) ? resolvedMessage : [resolvedMessage];
|
|
419
|
+
const results = [];
|
|
420
|
+
for (const msg of messages) {
|
|
421
|
+
const result = await sendMessageWithAttachments(msg);
|
|
422
|
+
results.push(result);
|
|
423
|
+
}
|
|
424
|
+
return {
|
|
425
|
+
...(0, import_language_common.composeNextState)(state, results)
|
|
426
|
+
};
|
|
346
427
|
};
|
|
347
428
|
}
|
|
348
429
|
function execute(...operations) {
|
|
@@ -382,5 +463,6 @@ var src_default = Adaptor_exports;
|
|
|
382
463
|
http,
|
|
383
464
|
lastReferenceValue,
|
|
384
465
|
merge,
|
|
466
|
+
sendMessage,
|
|
385
467
|
sourceValue
|
|
386
468
|
});
|
package/dist/index.js
CHANGED
|
@@ -22,6 +22,7 @@ __export(Adaptor_exports, {
|
|
|
22
22
|
http: () => http,
|
|
23
23
|
lastReferenceValue: () => lastReferenceValue,
|
|
24
24
|
merge: () => merge,
|
|
25
|
+
sendMessage: () => sendMessage,
|
|
25
26
|
sourceValue: () => sourceValue
|
|
26
27
|
});
|
|
27
28
|
import {
|
|
@@ -34,8 +35,9 @@ import {
|
|
|
34
35
|
} from "@openfn/language-common/util";
|
|
35
36
|
|
|
36
37
|
// src/Utils.js
|
|
37
|
-
import
|
|
38
|
+
import JSZip from "jszip";
|
|
38
39
|
import { google } from "googleapis";
|
|
40
|
+
var SEND_MESSAGE_BOUNDARY = "----=_Part_0_123456789.123456789";
|
|
39
41
|
var gmail;
|
|
40
42
|
async function getMessagesResult(userId, query, pageToken) {
|
|
41
43
|
try {
|
|
@@ -109,6 +111,72 @@ async function getMessageContent(message, desiredContent) {
|
|
|
109
111
|
return `Unsupported content type: ${desiredContent.type}`;
|
|
110
112
|
}
|
|
111
113
|
}
|
|
114
|
+
async function sendMessageWithAttachments(message) {
|
|
115
|
+
const attachments = await parseAttachments(message.attachments);
|
|
116
|
+
const lines = [
|
|
117
|
+
`To: ${message.to}`,
|
|
118
|
+
`Subject: ${message.subject}`,
|
|
119
|
+
"MIME-Version: 1.0",
|
|
120
|
+
`Content-Type: multipart/mixed; boundary="${SEND_MESSAGE_BOUNDARY}"`,
|
|
121
|
+
"",
|
|
122
|
+
`--${SEND_MESSAGE_BOUNDARY}`,
|
|
123
|
+
'Content-Type: text/plain; charset="UTF-8"',
|
|
124
|
+
"Content-Transfer-Encoding: 7bit",
|
|
125
|
+
"",
|
|
126
|
+
message.body
|
|
127
|
+
];
|
|
128
|
+
for (const attachment of attachments) {
|
|
129
|
+
const file = attachment.filename;
|
|
130
|
+
lines.push(
|
|
131
|
+
`--${SEND_MESSAGE_BOUNDARY}`,
|
|
132
|
+
`Content-Type: application/octet-stream; name="${file}"`,
|
|
133
|
+
"Content-Transfer-Encoding: base64",
|
|
134
|
+
`Content-Disposition: attachment; filename="${file}"`,
|
|
135
|
+
"",
|
|
136
|
+
attachment.content
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
lines.push(`--${SEND_MESSAGE_BOUNDARY}--`, "");
|
|
140
|
+
const rawMessage = lines.join("\r\n");
|
|
141
|
+
const encodedMessage = Buffer.from(rawMessage).toString("base64");
|
|
142
|
+
try {
|
|
143
|
+
const result = await gmail.users.messages.send({
|
|
144
|
+
userId: "me",
|
|
145
|
+
requestBody: { raw: encodedMessage }
|
|
146
|
+
});
|
|
147
|
+
return result.data;
|
|
148
|
+
} catch (error) {
|
|
149
|
+
throw new Error("Error fetching messages: " + error.message);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
async function parseAttachments(attachments) {
|
|
153
|
+
if (!attachments)
|
|
154
|
+
return null;
|
|
155
|
+
const parsedAttachments = [];
|
|
156
|
+
for (const attachment of attachments) {
|
|
157
|
+
if (attachment.archive) {
|
|
158
|
+
const archiveAttachment = await parseArchiveAttachment(attachment);
|
|
159
|
+
parsedAttachments.push(archiveAttachment);
|
|
160
|
+
} else {
|
|
161
|
+
parsedAttachments.push(attachment);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return parsedAttachments;
|
|
165
|
+
}
|
|
166
|
+
async function parseArchiveAttachment(attachment) {
|
|
167
|
+
const zip = new JSZip();
|
|
168
|
+
for (const file of attachment.archive) {
|
|
169
|
+
zip.file(file.filename, file.content, {
|
|
170
|
+
compression: "DEFLATE",
|
|
171
|
+
compressionOptions: { level: 9 }
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
const content = await zip.generateAsync({ type: "base64" });
|
|
175
|
+
return {
|
|
176
|
+
filename: attachment.filename,
|
|
177
|
+
content
|
|
178
|
+
};
|
|
179
|
+
}
|
|
112
180
|
function createConnection(state) {
|
|
113
181
|
const { access_token } = state.configuration;
|
|
114
182
|
const auth = new google.auth.OAuth2();
|
|
@@ -168,20 +236,19 @@ async function extractFileFromArchiveAttachment(attachment, desiredContent) {
|
|
|
168
236
|
return null;
|
|
169
237
|
}
|
|
170
238
|
const compressedBuffer = Buffer.from(attachment.data, "base64");
|
|
171
|
-
const
|
|
172
|
-
const
|
|
173
|
-
(
|
|
239
|
+
const zip = await JSZip.loadAsync(compressedBuffer);
|
|
240
|
+
const filename = Object.keys(zip.files).find(
|
|
241
|
+
(name) => isExpressionMatch(name, desiredContent.file)
|
|
174
242
|
);
|
|
175
|
-
if (!
|
|
243
|
+
if (!filename) {
|
|
176
244
|
console.info(`File not found in the archive for: ${desiredContent.file}`);
|
|
177
245
|
return null;
|
|
178
246
|
}
|
|
179
|
-
const
|
|
180
|
-
const
|
|
181
|
-
const fileContent = Buffer.from(fileString, "base64").toString("utf-8");
|
|
247
|
+
const file = zip.files[filename];
|
|
248
|
+
const fileContent = await file.async("string");
|
|
182
249
|
return {
|
|
183
250
|
archiveFilename: attachment.filename,
|
|
184
|
-
filename
|
|
251
|
+
filename,
|
|
185
252
|
content: desiredContent.maxLength ? fileContent.substring(0, desiredContent.maxLength) : fileContent
|
|
186
253
|
};
|
|
187
254
|
}
|
|
@@ -318,11 +385,24 @@ function getContentsFromMessages(options) {
|
|
|
318
385
|
}
|
|
319
386
|
}
|
|
320
387
|
} while (nextPageToken);
|
|
321
|
-
|
|
388
|
+
return {
|
|
322
389
|
...composeNextState(state, contents),
|
|
323
390
|
processedIds: newIds
|
|
324
391
|
};
|
|
325
|
-
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
function sendMessage(message) {
|
|
395
|
+
return async (state) => {
|
|
396
|
+
const [resolvedMessage] = expandReferences(state, message);
|
|
397
|
+
const messages = Array.isArray(resolvedMessage) ? resolvedMessage : [resolvedMessage];
|
|
398
|
+
const results = [];
|
|
399
|
+
for (const msg of messages) {
|
|
400
|
+
const result = await sendMessageWithAttachments(msg);
|
|
401
|
+
results.push(result);
|
|
402
|
+
}
|
|
403
|
+
return {
|
|
404
|
+
...composeNextState(state, results)
|
|
405
|
+
};
|
|
326
406
|
};
|
|
327
407
|
}
|
|
328
408
|
function execute(...operations) {
|
|
@@ -362,5 +442,6 @@ export {
|
|
|
362
442
|
http,
|
|
363
443
|
lastReferenceValue,
|
|
364
444
|
merge,
|
|
445
|
+
sendMessage,
|
|
365
446
|
sourceValue
|
|
366
447
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openfn/language-gmail",
|
|
3
|
-
"
|
|
3
|
+
"label": "Gmail",
|
|
4
|
+
"version": "1.3.0",
|
|
4
5
|
"description": "OpenFn gmail adaptor",
|
|
5
6
|
"type": "module",
|
|
6
7
|
"exports": {
|
|
@@ -21,14 +22,13 @@
|
|
|
21
22
|
],
|
|
22
23
|
"dependencies": {
|
|
23
24
|
"googleapis": "100.0.0",
|
|
24
|
-
"
|
|
25
|
-
"@openfn/language-common": "2.
|
|
25
|
+
"jszip": "^3.10.1",
|
|
26
|
+
"@openfn/language-common": "2.4.0"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
29
|
"assertion-error": "2.0.0",
|
|
29
30
|
"chai": "4.3.6",
|
|
30
31
|
"deep-eql": "4.1.1",
|
|
31
|
-
"esno": "^0.16.3",
|
|
32
32
|
"mocha": "^10.7.3",
|
|
33
33
|
"rimraf": "3.0.2",
|
|
34
34
|
"undici": "^5.22.1"
|
package/types/Adaptor.d.ts
CHANGED
|
@@ -47,14 +47,42 @@
|
|
|
47
47
|
* )
|
|
48
48
|
*/
|
|
49
49
|
export function getContentsFromMessages(options: Options): Operation;
|
|
50
|
+
/**
|
|
51
|
+
* Configurable fields for composing an outbound Gmail message.
|
|
52
|
+
* @typedef {Object} SendMessageOptions
|
|
53
|
+
* @property {string} to - Recipient email address.
|
|
54
|
+
* @property {string} subject - Subject line of the email.
|
|
55
|
+
* @property {string} body - Email body content.
|
|
56
|
+
* @property {Array<{ filename: string, content: string|Buffer }>} [attachments] - Optional list of files to attach.
|
|
57
|
+
*/
|
|
58
|
+
/**
|
|
59
|
+
* Sends a Gmail message using the provided configuration.
|
|
60
|
+
* Supports attachments and standard email fields like subject, body, and recipients.
|
|
61
|
+
*
|
|
62
|
+
* @public
|
|
63
|
+
* @function
|
|
64
|
+
* @param {SendMessageOptions|SendMessageOptions[]} message - The message configuration object or array of objects.
|
|
65
|
+
* @state {Object} data - The Gmail API response from sending the message.
|
|
66
|
+
* @returns {Operation}
|
|
67
|
+
* @example
|
|
68
|
+
* sendMessage({
|
|
69
|
+
* to: 'recipient@example.org',
|
|
70
|
+
* subject: 'Test Message',
|
|
71
|
+
* body: 'Hello from OpenFn!',
|
|
72
|
+
* attachments: [
|
|
73
|
+
* { filename: 'test.txt', content: 'Some text content' }
|
|
74
|
+
* ]
|
|
75
|
+
* })
|
|
76
|
+
*/
|
|
77
|
+
export function sendMessage(message: SendMessageOptions | SendMessageOptions[]): Operation;
|
|
50
78
|
/**
|
|
51
79
|
* Execute a sequence of operations.
|
|
52
80
|
* Wraps `language-common/execute`, and prepends initial state for http.
|
|
53
81
|
* @private
|
|
54
|
-
* @param {
|
|
82
|
+
* @param {...Function} operations - Operations to be performed.
|
|
55
83
|
* @returns {Operation}
|
|
56
84
|
*/
|
|
57
|
-
export function execute(...operations:
|
|
85
|
+
export function execute(...operations: Function[]): Operation;
|
|
58
86
|
/**
|
|
59
87
|
* Used to isolate the type of content to retrieve from the message.
|
|
60
88
|
*/
|
|
@@ -63,4 +91,28 @@ export type MessageContent = any;
|
|
|
63
91
|
* Configurable options provided to the Gmail adaptor.
|
|
64
92
|
*/
|
|
65
93
|
export type Options = any;
|
|
94
|
+
/**
|
|
95
|
+
* Configurable fields for composing an outbound Gmail message.
|
|
96
|
+
*/
|
|
97
|
+
export type SendMessageOptions = {
|
|
98
|
+
/**
|
|
99
|
+
* - Recipient email address.
|
|
100
|
+
*/
|
|
101
|
+
to: string;
|
|
102
|
+
/**
|
|
103
|
+
* - Subject line of the email.
|
|
104
|
+
*/
|
|
105
|
+
subject: string;
|
|
106
|
+
/**
|
|
107
|
+
* - Email body content.
|
|
108
|
+
*/
|
|
109
|
+
body: string;
|
|
110
|
+
/**
|
|
111
|
+
* - Optional list of files to attach.
|
|
112
|
+
*/
|
|
113
|
+
attachments?: Array<{
|
|
114
|
+
filename: string;
|
|
115
|
+
content: string | Buffer;
|
|
116
|
+
}>;
|
|
117
|
+
};
|
|
66
118
|
export { alterState, combine, cursor, dataPath, dataValue, each, field, fields, fn, fnIf, http, lastReferenceValue, merge, sourceValue } from "@openfn/language-common";
|
package/types/Utils.d.ts
CHANGED
|
@@ -10,5 +10,6 @@ export function getMessageResult(userId: any, messageId: any): Promise<{
|
|
|
10
10
|
}>;
|
|
11
11
|
export function getContentIndicators(defaultContentRequests?: any[], contentRequests?: any[]): any[];
|
|
12
12
|
export function getMessageContent(message: any, desiredContent: any): Promise<any>;
|
|
13
|
+
export function sendMessageWithAttachments(message: any): Promise<any>;
|
|
13
14
|
export function createConnection(state: any): any;
|
|
14
15
|
export function removeConnection(state: any): any;
|