2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2022-02-19 02:18:43 -08:00
|
|
|
ICredentialsDecrypted,
|
|
|
|
ICredentialTestFunctions,
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeCredentialTestResult,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeOperationError } from 'n8n-workflow';
|
2022-02-19 02:18:43 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2022-04-08 14:32:08 -07:00
|
|
|
import moment from 'moment-timezone';
|
|
|
|
import jwt from 'jsonwebtoken';
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IMessage, IMessageUi } from './MessageInterface';
|
2022-02-19 02:18:43 -08:00
|
|
|
|
|
|
|
import {
|
|
|
|
// attachmentFields,
|
|
|
|
// attachmentOperations,
|
|
|
|
// incomingWebhookFields,
|
|
|
|
// incomingWebhookOperations,
|
|
|
|
// mediaFields,
|
|
|
|
// mediaOperations,
|
|
|
|
memberFields,
|
|
|
|
memberOperations,
|
|
|
|
messageFields,
|
|
|
|
messageOperations,
|
|
|
|
spaceFields,
|
2022-08-17 08:50:24 -07:00
|
|
|
spaceOperations,
|
2022-02-19 02:18:43 -08:00
|
|
|
} from './descriptions';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { googleApiRequest, googleApiRequestAllItems, validateJSON } from './GenericFunctions';
|
2022-02-19 02:18:43 -08:00
|
|
|
|
|
|
|
export class GoogleChat implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Google Chat',
|
|
|
|
name: 'googleChat',
|
|
|
|
icon: 'file:googleChat.svg',
|
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume Google Chat API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Google Chat',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'googleApi',
|
|
|
|
required: true,
|
|
|
|
testedBy: 'testGoogleTokenAuth',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
required: true,
|
|
|
|
noDataExpression: true,
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
// {
|
|
|
|
// name: 'Attachment',
|
|
|
|
// value: 'attachment',
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// name: 'Incoming Webhook',
|
|
|
|
// value: 'incomingWebhook',
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// name: 'Media',
|
|
|
|
// value: 'media',
|
|
|
|
// },
|
|
|
|
{
|
|
|
|
name: 'Member',
|
|
|
|
value: 'member',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Message',
|
|
|
|
value: 'message',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Space',
|
|
|
|
value: 'space',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'message',
|
|
|
|
},
|
|
|
|
// ...attachmentOperations,
|
|
|
|
// ...attachmentFields,
|
|
|
|
// ...incomingWebhookOperations,
|
|
|
|
// ...incomingWebhookFields,
|
|
|
|
// ...mediaOperations,
|
|
|
|
// ...mediaFields,
|
|
|
|
...memberOperations,
|
|
|
|
...memberFields,
|
|
|
|
...messageOperations,
|
|
|
|
...messageFields,
|
|
|
|
...spaceOperations,
|
|
|
|
...spaceFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the spaces to display them to user so that they can
|
2022-02-19 02:18:43 -08:00
|
|
|
// select them easily
|
2022-08-17 08:50:24 -07:00
|
|
|
async getSpaces(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2022-02-19 02:18:43 -08:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-12-29 03:20:43 -08:00
|
|
|
const spaces = await googleApiRequestAllItems.call(this, 'spaces', 'GET', '/v1/spaces');
|
2022-02-19 02:18:43 -08:00
|
|
|
for (const space of spaces) {
|
|
|
|
returnData.push({
|
|
|
|
name: space.displayName,
|
|
|
|
value: space.name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
credentialTest: {
|
2022-08-17 08:50:24 -07:00
|
|
|
async testGoogleTokenAuth(
|
|
|
|
this: ICredentialTestFunctions,
|
|
|
|
credential: ICredentialsDecrypted,
|
|
|
|
): Promise<INodeCredentialTestResult> {
|
|
|
|
const scopes = ['https://www.googleapis.com/auth/chat.bot'];
|
2022-02-19 02:18:43 -08:00
|
|
|
|
|
|
|
const now = moment().unix();
|
|
|
|
|
|
|
|
const email = (credential.data!.email as string).trim();
|
|
|
|
const privateKey = (credential.data!.privateKey as string).replace(/\\n/g, '\n').trim();
|
|
|
|
|
|
|
|
try {
|
|
|
|
const signature = jwt.sign(
|
|
|
|
{
|
2022-08-17 08:50:24 -07:00
|
|
|
iss: email,
|
|
|
|
sub: credential.data!.delegatedEmail || email,
|
|
|
|
scope: scopes.join(' '),
|
2022-12-29 03:20:43 -08:00
|
|
|
aud: 'https://oauth2.googleapis.com/token',
|
2022-08-17 08:50:24 -07:00
|
|
|
iat: now,
|
|
|
|
exp: now,
|
2022-02-19 02:18:43 -08:00
|
|
|
},
|
|
|
|
privateKey,
|
|
|
|
{
|
|
|
|
algorithm: 'RS256',
|
|
|
|
header: {
|
2022-08-17 08:50:24 -07:00
|
|
|
kid: privateKey,
|
|
|
|
typ: 'JWT',
|
|
|
|
alg: 'RS256',
|
2022-02-19 02:18:43 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
form: {
|
|
|
|
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
|
|
|
|
assertion: signature,
|
|
|
|
},
|
|
|
|
uri: 'https://oauth2.googleapis.com/token',
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const response = await this.helpers.request(options);
|
|
|
|
|
|
|
|
if (!response.access_token) {
|
|
|
|
return {
|
|
|
|
status: 'Error',
|
|
|
|
message: JSON.stringify(response),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return {
|
|
|
|
status: 'Error',
|
|
|
|
message: `${err.message}`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
status: 'OK',
|
|
|
|
message: 'Connection successful!',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2022-02-19 02:18:43 -08:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
let responseData;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2022-02-19 02:18:43 -08:00
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
try {
|
|
|
|
if (resource === 'media') {
|
|
|
|
if (operation === 'download') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// media: download
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://developers.google.com/chat/reference/rest/v1/media/download
|
|
|
|
|
|
|
|
const resourceName = this.getNodeParameter('resourceName', i) as string;
|
|
|
|
|
|
|
|
const endpoint = `/v1/media/${resourceName}?alt=media`;
|
|
|
|
|
|
|
|
// Return the data as a buffer
|
|
|
|
const encoding = null;
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
endpoint,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
encoding,
|
|
|
|
);
|
|
|
|
|
|
|
|
const newItem: INodeExecutionData = {
|
|
|
|
json: items[i].json,
|
|
|
|
binary: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (items[i].binary !== undefined) {
|
|
|
|
// Create a shallow copy of the binary data so that the old
|
|
|
|
// data references which do not get changed still stay behind
|
|
|
|
// but the incoming data does not get changed.
|
2022-08-30 08:55:33 -07:00
|
|
|
Object.assign(newItem.binary!, items[i].binary);
|
2022-02-19 02:18:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
items[i] = newItem;
|
|
|
|
|
2023-01-06 06:09:32 -08:00
|
|
|
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
|
2022-02-19 02:18:43 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
items[i].binary![binaryPropertyName] = await this.helpers.prepareBinaryData(
|
2023-02-27 19:39:43 -08:00
|
|
|
responseData as Buffer,
|
2022-08-17 08:50:24 -07:00
|
|
|
endpoint,
|
|
|
|
);
|
2022-02-19 02:18:43 -08:00
|
|
|
}
|
|
|
|
} else if (resource === 'space') {
|
|
|
|
if (operation === 'get') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// space: get
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://developers.google.com/chat/reference/rest/v1/spaces/get
|
|
|
|
|
|
|
|
const spaceId = this.getNodeParameter('spaceId', i) as string;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await googleApiRequest.call(this, 'GET', `/v1/${spaceId}`);
|
2022-02-19 02:18:43 -08:00
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// space: getAll
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://developers.google.com/chat/reference/rest/v1/spaces/list
|
|
|
|
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0);
|
2022-02-19 02:18:43 -08:00
|
|
|
if (returnAll) {
|
|
|
|
responseData = await googleApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'spaces',
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/v1/spaces',
|
2022-02-19 02:18:43 -08:00
|
|
|
);
|
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2022-02-19 02:18:43 -08:00
|
|
|
qs.pageSize = limit;
|
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
responseData = await googleApiRequest.call(this, 'GET', '/v1/spaces', undefined, qs);
|
2022-02-19 02:18:43 -08:00
|
|
|
responseData = responseData.spaces;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (resource === 'member') {
|
|
|
|
if (operation === 'get') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// member: get
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://developers.google.com/chat/reference/rest/v1/spaces.members/get
|
|
|
|
|
|
|
|
const memberId = this.getNodeParameter('memberId', i) as string;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await googleApiRequest.call(this, 'GET', `/v1/${memberId}`);
|
2022-02-19 02:18:43 -08:00
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// member: getAll
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://developers.google.com/chat/reference/rest/v1/spaces.members/list
|
|
|
|
|
|
|
|
const spaceId = this.getNodeParameter('spaceId', i) as string;
|
|
|
|
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0);
|
2022-02-19 02:18:43 -08:00
|
|
|
if (returnAll) {
|
|
|
|
responseData = await googleApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'memberships',
|
|
|
|
'GET',
|
|
|
|
`/v1/${spaceId}/members`,
|
|
|
|
undefined,
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2022-02-19 02:18:43 -08:00
|
|
|
qs.pageSize = limit;
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/v1/${spaceId}/members`,
|
|
|
|
undefined,
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
responseData = responseData.memberships;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (resource === 'message') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// message: create
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://developers.google.com/chat/reference/rest/v1/spaces.messages/create
|
|
|
|
|
|
|
|
const spaceId = this.getNodeParameter('spaceId', i) as string;
|
|
|
|
|
|
|
|
// get additional fields for threadKey and requestId
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-02-19 02:18:43 -08:00
|
|
|
if (additionalFields.threadKey) {
|
|
|
|
qs.threadKey = additionalFields.threadKey;
|
|
|
|
}
|
|
|
|
if (additionalFields.requestId) {
|
|
|
|
qs.requestId = additionalFields.requestId;
|
|
|
|
}
|
|
|
|
|
|
|
|
let message: IMessage = {};
|
2022-11-18 05:31:38 -08:00
|
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', i);
|
2022-02-19 02:18:43 -08:00
|
|
|
if (jsonParameters) {
|
|
|
|
const messageJson = this.getNodeParameter('messageJson', i);
|
|
|
|
|
|
|
|
if (messageJson instanceof Object) {
|
|
|
|
// if it is an object
|
|
|
|
message = messageJson as IMessage;
|
|
|
|
} else {
|
|
|
|
// if it is a string
|
|
|
|
if (validateJSON(messageJson as string) !== undefined) {
|
|
|
|
message = JSON.parse(messageJson as string) as IMessage;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'Message (JSON) must be a valid json',
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2022-02-19 02:18:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const messageUi = this.getNodeParameter('messageUi', i) as IMessageUi;
|
|
|
|
if (messageUi.text && messageUi.text !== '') {
|
|
|
|
message.text = messageUi.text;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'Message Text must be provided.', {
|
|
|
|
itemIndex: i,
|
|
|
|
});
|
2022-02-19 02:18:43 -08:00
|
|
|
}
|
|
|
|
// // TODO: get cards from the UI
|
|
|
|
// if (messageUi?.cards?.metadataValues && messageUi?.cards?.metadataValues.length !== 0) {
|
|
|
|
// const cards = messageUi.cards.metadataValues as IDataObject[]; // TODO: map cards to messageUi.cards.metadataValues
|
|
|
|
// message.cards = cards;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
const body: IDataObject = {};
|
|
|
|
Object.assign(body, message);
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/v1/${spaceId}/messages`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// message: delete
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://developers.google.com/chat/reference/rest/v1/spaces.messages/delete
|
|
|
|
|
|
|
|
const messageId = this.getNodeParameter('messageId', i) as string;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await googleApiRequest.call(this, 'DELETE', `/v1/${messageId}`);
|
2022-02-19 02:18:43 -08:00
|
|
|
} else if (operation === 'get') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// message: get
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://developers.google.com/chat/reference/rest/v1/spaces.messages/get
|
|
|
|
|
|
|
|
const messageId = this.getNodeParameter('messageId', i) as string;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await googleApiRequest.call(this, 'GET', `/v1/${messageId}`);
|
2022-02-19 02:18:43 -08:00
|
|
|
} else if (operation === 'update') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// message: update
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://developers.google.com/chat/reference/rest/v1/spaces.messages/update
|
|
|
|
|
|
|
|
const messageId = this.getNodeParameter('messageId', i) as string;
|
|
|
|
|
|
|
|
let message: IMessage = {};
|
2022-11-18 05:31:38 -08:00
|
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', i);
|
2022-02-19 02:18:43 -08:00
|
|
|
if (jsonParameters) {
|
|
|
|
const updateFieldsJson = this.getNodeParameter('updateFieldsJson', i);
|
|
|
|
|
|
|
|
if (updateFieldsJson instanceof Object) {
|
|
|
|
// if it is an object
|
|
|
|
message = updateFieldsJson as IMessage;
|
|
|
|
} else {
|
|
|
|
// if it is a string
|
|
|
|
if (validateJSON(updateFieldsJson as string) !== undefined) {
|
|
|
|
message = JSON.parse(updateFieldsJson as string) as IMessage;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'Update Fields (JSON) must be a valid json',
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2022-02-19 02:18:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const updateFieldsUi = this.getNodeParameter('updateFieldsUi', i) as IDataObject;
|
|
|
|
if (updateFieldsUi.text) {
|
|
|
|
message.text = updateFieldsUi.text as string;
|
|
|
|
}
|
|
|
|
// // TODO: get cards from the UI
|
|
|
|
// if (updateFieldsUi.cards) {
|
|
|
|
// message.cards = updateFieldsUi.cards as IDataObject[];
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
const body: IDataObject = {};
|
|
|
|
Object.assign(body, message);
|
|
|
|
|
|
|
|
// get update mask
|
|
|
|
let updateMask = '';
|
|
|
|
if (message.text) {
|
|
|
|
updateMask += 'text,';
|
|
|
|
}
|
|
|
|
if (message.cards) {
|
|
|
|
updateMask += 'cards,';
|
|
|
|
}
|
|
|
|
updateMask = updateMask.slice(0, -1); // remove trailing comma
|
|
|
|
qs.updateMask = updateMask;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await googleApiRequest.call(this, 'PUT', `/v1/${messageId}`, body, qs);
|
2022-02-19 02:18:43 -08:00
|
|
|
}
|
|
|
|
} else if (resource === 'attachment') {
|
|
|
|
if (operation === 'get') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// attachment: get
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://developers.google.com/chat/reference/rest/v1/spaces.messages.attachments/get
|
|
|
|
|
|
|
|
const attachmentName = this.getNodeParameter('attachmentName', i) as string;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await googleApiRequest.call(this, 'GET', `/v1/${attachmentName}`);
|
2022-02-19 02:18:43 -08:00
|
|
|
}
|
|
|
|
} else if (resource === 'incomingWebhook') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// incomingWebhook: create
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://developers.google.com/chat/how-tos/webhooks
|
|
|
|
|
|
|
|
const uri = this.getNodeParameter('incomingWebhookUrl', i) as string;
|
|
|
|
|
|
|
|
// get additional fields for threadKey
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-02-19 02:18:43 -08:00
|
|
|
if (additionalFields.threadKey) {
|
|
|
|
qs.threadKey = additionalFields.threadKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
let message: IMessage = {};
|
2022-11-18 05:31:38 -08:00
|
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', i);
|
2022-02-19 02:18:43 -08:00
|
|
|
if (jsonParameters) {
|
|
|
|
const messageJson = this.getNodeParameter('messageJson', i);
|
|
|
|
|
|
|
|
if (messageJson instanceof Object) {
|
|
|
|
// if it is an object
|
|
|
|
message = messageJson as IMessage;
|
|
|
|
} else {
|
|
|
|
// if it is a string
|
|
|
|
if (validateJSON(messageJson as string) !== undefined) {
|
|
|
|
message = JSON.parse(messageJson as string) as IMessage;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'Message (JSON) must be a valid json',
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2022-02-19 02:18:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const messageUi = this.getNodeParameter('messageUi', i) as IMessageUi;
|
|
|
|
if (messageUi.text && messageUi.text !== '') {
|
|
|
|
message.text = messageUi.text;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'Message Text must be provided.', {
|
|
|
|
itemIndex: i,
|
|
|
|
});
|
2022-02-19 02:18:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const body: IDataObject = {};
|
|
|
|
Object.assign(body, message);
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await googleApiRequest.call(this, 'POST', '', body, qs, uri, true);
|
2022-02-19 02:18:43 -08:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2022-02-19 02:18:43 -08:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
// Return the actual reason as error
|
|
|
|
if (operation === 'download') {
|
|
|
|
items[i].json = { error: error.message };
|
|
|
|
} else {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2022-02-19 02:18:43 -08:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'download') {
|
|
|
|
// For file downloads the files get attached to the existing items
|
2023-09-05 03:59:02 -07:00
|
|
|
return [items];
|
2022-02-19 02:18:43 -08:00
|
|
|
} else {
|
|
|
|
// For all other ones does the output get replaced
|
2023-09-05 03:59:02 -07:00
|
|
|
return [returnData];
|
2022-02-19 02:18:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|