Add Amazon Comprehend Node (#1415)

*  Add Amazon Comprehend node

*  Improvements to #1413

Co-authored-by: Rei Maruyama <rei.maruyama@serverworks.co.jp>
This commit is contained in:
Ricardo Espinoza 2021-02-07 17:42:59 -05:00 committed by GitHub
parent 60a2cff284
commit ba7c35dbf5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 325 additions and 0 deletions

View file

@ -0,0 +1,222 @@
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
awsApiRequestREST,
} from './GenericFunctions';
export class AwsComprehend implements INodeType {
description: INodeTypeDescription = {
displayName: 'AWS Comprehend',
name: 'awsComprehend',
icon: 'file:comprehend.svg',
group: ['output'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Sends data to Amazon Comprehend',
defaults: {
name: 'AWS Comprehend',
color: '#5aa08d',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'aws',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Text',
value: 'text',
},
],
default: 'text',
description: 'The resource to perform.',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
options: [
{
name: 'Detect Dominant Language',
value: 'detectDominantLanguage',
description: 'Identify the dominant language',
},
{
name: 'Detect Sentiment',
value: 'detectSentiment',
description: 'Analyse the sentiment of the text',
},
],
default: 'detectDominantLanguage',
description: 'The operation to perform.',
},
{
displayName: 'Language Code',
name: 'languageCode',
type: 'options',
options: [
{
name: 'Arabic',
value: 'ar',
},
{
name: 'Chinese',
value: 'zh',
},
{
name: 'Chinese (T)',
value: 'zh-TW',
},
{
name: 'English',
value: 'en',
},
{
name: 'French',
value: 'fr',
},
{
name: 'German',
value: 'de',
},
{
name: 'Hindi',
value: 'hi',
},
{
name: 'Italian',
value: 'it',
},
{
name: 'Japanese',
value: 'ja',
},
{
name: 'Korean',
value: 'ko',
},
{
name: 'Portuguese',
value: 'pt',
},
{
name: 'Spanish',
value: 'es',
},
],
default: 'en',
displayOptions: {
show: {
resource: [
'text',
],
operation: [
'detectSentiment',
],
},
},
description: 'The language code for text.',
},
{
displayName: 'Text',
name: 'text',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
displayOptions: {
show: {
resource: [
'text',
],
},
},
description: 'The text to send.',
},
{
displayName: 'Simple',
name: 'simple',
type: 'boolean',
displayOptions: {
show: {
resource: [
'text',
],
operation: [
'detectDominantLanguage',
],
},
},
default: true,
description: 'When set to true a simplify version of the response will be used else the raw data.',
},
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
let responseData;
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < items.length; i++) {
if (resource === 'text') {
//https://docs.aws.amazon.com/comprehend/latest/dg/API_DetectDominantLanguage.html
if (operation === 'detectDominantLanguage') {
const text = this.getNodeParameter('text', i) as string;
const simple = this.getNodeParameter('simple', i) as boolean;
const body: IDataObject = {
Text: text,
};
const action = 'Comprehend_20171127.DetectDominantLanguage';
responseData = await awsApiRequestREST.call(this, 'comprehend', 'POST', '', JSON.stringify(body), { 'x-amz-target': action, 'Content-Type': 'application/x-amz-json-1.1' });
if (simple === true) {
responseData = responseData.Languages.reduce((accumulator: { [key: string]: number }, currentValue: IDataObject) => {
accumulator[currentValue.LanguageCode as string] = currentValue.Score as number;
return accumulator;
}, {});
}
}
//https://docs.aws.amazon.com/comprehend/latest/dg/API_DetectSentiment.html
if (operation === 'detectSentiment') {
const action = 'Comprehend_20171127.DetectSentiment';
const text = this.getNodeParameter('text', i) as string;
const languageCode = this.getNodeParameter('languageCode', i) as string;
const body: IDataObject = {
Text: text,
LanguageCode: languageCode,
};
responseData = await awsApiRequestREST.call(this, 'comprehend', 'POST', '', JSON.stringify(body), { 'x-amz-target': action, 'Content-Type': 'application/x-amz-json-1.1' });
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else {
returnData.push(responseData as IDataObject);
}
}
return [this.helpers.returnJsonArray(returnData)];
}
}

View file

@ -0,0 +1,101 @@
import {
URL,
} from 'url';
import {
sign,
} from 'aws4';
import {
OptionsWithUri,
} from 'request';
import {
parseString,
} from 'xml2js';
import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
ICredentialDataDecryptedObject,
} from 'n8n-workflow';
function getEndpointForService(service: string, credentials: ICredentialDataDecryptedObject): string {
let endpoint;
if (service === 'lambda' && credentials.lambdaEndpoint) {
endpoint = credentials.lambdaEndpoint;
} else if (service === 'sns' && credentials.snsEndpoint) {
endpoint = credentials.snsEndpoint;
} else {
endpoint = `https://${service}.${credentials.region}.amazonaws.com`;
}
return (endpoint as string).replace('{region}', credentials.region as string);
}
export async function awsApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions, service: string, method: string, path: string, body?: string, headers?: object): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('aws');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
// Concatenate path and instantiate URL object so it parses correctly query strings
const endpoint = new URL(getEndpointForService(service, credentials) + path);
// Sign AWS API request with the user credentials
const signOpts = { headers: headers || {}, host: endpoint.host, method, path, body };
sign(signOpts, { accessKeyId: `${credentials.accessKeyId}`.trim(), secretAccessKey: `${credentials.secretAccessKey}`.trim() });
const options: OptionsWithUri = {
headers: signOpts.headers,
method,
uri: endpoint.href,
body: signOpts.body,
};
try {
return await this.helpers.request!(options);
} catch (error) {
const errorMessage = (error.response && error.response.body.message) || (error.response && error.response.body.Message) || error.message;
if (error.statusCode === 403) {
if (errorMessage === 'The security token included in the request is invalid.') {
throw new Error('The AWS credentials are not valid!');
} else if (errorMessage.startsWith('The request signature we calculated does not match the signature you provided')) {
throw new Error('The AWS credentials are not valid!');
}
}
throw new Error(`AWS error response [${error.statusCode}]: ${errorMessage}`);
}
}
export async function awsApiRequestREST(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, service: string, method: string, path: string, body?: string, headers?: object): Promise<any> { // tslint:disable-line:no-any
const response = await awsApiRequest.call(this, service, method, path, body, headers);
try {
return JSON.parse(response);
} catch (e) {
return response;
}
}
export async function awsApiRequestSOAP(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions, service: string, method: string, path: string, body?: string, headers?: object): Promise<any> { // tslint:disable-line:no-any
const response = await awsApiRequest.call(this, service, method, path, body, headers);
try {
return await new Promise((resolve, reject) => {
parseString(response, { explicitArray: false }, (err, data) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
} catch (e) {
return response;
}
}

View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 75 75"><defs><style>.cls-1{fill:url(#TurquoiseGradient);}.cls-2{fill:#fff;}</style><linearGradient id="TurquoiseGradient" x1="617.46" y1="-674.53" x2="723.53" y2="-568.46" gradientTransform="translate(659 708) rotate(-90)" gradientUnits="userSpaceOnUse"><stop offset="0" stop-color="#055f4e"/><stop offset="1" stop-color="#56c0a7"/></linearGradient></defs><title>Amazon-Comprehend</title><g id="Reference"><rect id="Turquoise_Gradient" data-name="Turquoise Gradient" class="cls-1" width="75" height="75"/><g id="Icon_Test" data-name="Icon Test"><path class="cls-2" d="M44.5,34.2V24.5a1,1,0,0,0-.29-.71l-11-11a1,1,0,0,0-.71-.29h-19a1,1,0,0,0-1,1v43a1,1,0,0,0,1,1h30a1,1,0,0,0,1-1V52.06a11.8,11.8,0,0,1-2-2.3V55.5h-28v-41h17v10a1,1,0,0,0,1,1h10v11A11.56,11.56,0,0,1,44.5,34.2Zm-11-10.7V15.91l7.59,7.59Zm-10,8h-6v-2h6Zm16,0h-14v-2h14Zm0,6h-22v-2h22Zm15.44,25H50.06a1,1,0,0,1-.93-.62l-1.21-3a1,1,0,0,1,.09-.94,1,1,0,0,1,.83-.44h7.32a1,1,0,0,1,.83.44,1,1,0,0,1,.09.94l-1.21,3A1,1,0,0,1,54.94,62.5Zm-4.21-2h3.54l.4-1H50.33Zm11.64-19A10,10,0,0,0,42.5,43.12a10,10,0,0,0,4.28,8.2,3.88,3.88,0,0,1,.72.59V55.5a1,1,0,0,0,1,1h8a1,1,0,0,0,1-1V51.9a4.33,4.33,0,0,1,.71-.57,9.92,9.92,0,0,0,4.29-8.2A10.19,10.19,0,0,0,62.37,41.48ZM57.05,49.7c-.58.4-1.55,1.07-1.55,2.1v2.7h-2v-7h2v-2h-6v2h2v7h-2V51.82c0-1-1-1.73-1.58-2.14A8,8,0,1,1,58,37.32a7.89,7.89,0,0,1,2.39,4.47A8,8,0,0,1,57.05,49.7ZM28.5,25.5h-11v-2h11Zm1,18h-12v-2h12Zm10,0h-8v-2h8Zm-9,6h-13v-2h13Z"/></g></g></svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

View file

@ -265,6 +265,7 @@
"dist/nodes/Affinity/AffinityTrigger.node.js",
"dist/nodes/Automizy/Automizy.node.js",
"dist/nodes/Aws/AwsLambda.node.js",
"dist/nodes/Aws/Comprehend/AwsComprehend.node.js",
"dist/nodes/Aws/Rekognition/AwsRekognition.node.js",
"dist/nodes/Aws/S3/AwsS3.node.js",
"dist/nodes/Aws/SES/AwsSes.node.js",