2022-08-17 08:50:24 -07:00
import { IExecuteFunctions } from 'n8n-core' ;
2020-11-19 00:04:20 -08:00
2022-11-20 23:27:48 -08:00
import { INodeExecutionData , INodeType , INodeTypeDescription } from 'n8n-workflow' ;
2020-11-19 00:04:20 -08:00
2022-08-17 08:50:24 -07:00
import { IData } from './Interface' ;
2020-11-19 00:04:20 -08:00
2022-08-17 08:50:24 -07:00
import { googleApiRequest } from './GenericFunctions' ;
2020-11-19 00:04:20 -08:00
export class GoogleCloudNaturalLanguage implements INodeType {
description : INodeTypeDescription = {
displayName : 'Google Cloud Natural Language' ,
name : 'googleCloudNaturalLanguage' ,
2022-06-20 07:54:01 -07:00
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
2020-11-19 00:06:03 -08:00
icon : 'file:googlecloudnaturallanguage.png' ,
2020-11-19 00:04:20 -08:00
group : [ 'input' , 'output' ] ,
version : 1 ,
description : 'Consume Google Cloud Natural Language API' ,
subtitle : '={{$parameter["operation"] + ": " + $parameter["resource"]}}' ,
defaults : {
name : 'Google Cloud Natural Language' ,
} ,
inputs : [ 'main' ] ,
outputs : [ 'main' ] ,
credentials : [
{
name : 'googleCloudNaturalLanguageOAuth2Api' ,
required : true ,
} ,
] ,
properties : [
{
displayName : 'Resource' ,
name : 'resource' ,
type : 'options' ,
2022-05-20 14:47:24 -07:00
noDataExpression : true ,
2020-11-19 00:04:20 -08:00
options : [
{
name : 'Document' ,
value : 'document' ,
} ,
] ,
default : 'document' ,
} ,
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
2022-05-20 14:47:24 -07:00
noDataExpression : true ,
2020-11-19 00:04:20 -08:00
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
resource : [ 'document' ] ,
2020-11-19 00:04:20 -08:00
} ,
} ,
options : [
{
name : 'Analyze Sentiment' ,
value : 'analyzeSentiment' ,
2022-07-10 13:50:51 -07:00
action : 'Analyze sentiment' ,
2020-11-19 00:04:20 -08:00
} ,
] ,
default : 'analyzeSentiment' ,
} ,
// ----------------------------------
// All
// ----------------------------------
{
displayName : 'Source' ,
name : 'source' ,
type : 'options' ,
options : [
{
name : 'Content' ,
value : 'content' ,
} ,
{
name : 'Google Cloud Storage URI' ,
value : 'gcsContentUri' ,
} ,
] ,
default : 'content' ,
2022-08-17 08:50:24 -07:00
description :
'The source of the document: a string containing the content or a Google Cloud Storage URI' ,
2020-11-19 00:04:20 -08:00
required : true ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
operation : [ 'analyzeSentiment' ] ,
2020-11-19 00:04:20 -08:00
} ,
} ,
} ,
{
displayName : 'Content' ,
name : 'content' ,
type : 'string' ,
default : '' ,
2022-08-17 08:50:24 -07:00
description :
'The content of the input in string format. Cloud audit logging exempt since it is based on user data.' ,
2020-11-19 00:04:20 -08:00
required : true ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
operation : [ 'analyzeSentiment' ] ,
source : [ 'content' ] ,
2020-11-19 00:04:20 -08:00
} ,
} ,
} ,
{
displayName : 'Google Cloud Storage URI' ,
name : 'gcsContentUri' ,
type : 'string' ,
default : '' ,
2022-08-17 08:50:24 -07:00
description :
'The Google Cloud Storage URI where the file content is located. This URI must be of the form: <code>gs://bucket_name/object_name</code>. For more details, see <a href="https://cloud.google.com/storage/docs/reference-uris.">reference</a>.' ,
2020-11-19 00:04:20 -08:00
required : true ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
operation : [ 'analyzeSentiment' ] ,
source : [ 'gcsContentUri' ] ,
2020-11-19 00:04:20 -08:00
} ,
} ,
} ,
{
displayName : 'Options' ,
name : 'options' ,
type : 'collection' ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
operation : [ 'analyzeSentiment' ] ,
2020-11-19 00:04:20 -08:00
} ,
} ,
default : { } ,
placeholder : 'Add Option' ,
options : [
2020-11-19 00:06:03 -08:00
{
displayName : 'Document Type' ,
name : 'documentType' ,
type : 'options' ,
options : [
{
name : 'HTML' ,
value : 'HTML' ,
} ,
{
name : 'Plain Text' ,
value : 'PLAIN_TEXT' ,
} ,
] ,
default : 'PLAIN_TEXT' ,
2022-05-06 14:01:25 -07:00
description : 'The type of input document' ,
2020-11-19 00:06:03 -08:00
} ,
{
displayName : 'Encoding Type' ,
name : 'encodingType' ,
type : 'options' ,
options : [
{
name : 'None' ,
value : 'NONE' ,
} ,
{
name : 'UTF-8' ,
value : 'UTF8' ,
} ,
{
name : 'UTF-16' ,
value : 'UTF16' ,
} ,
{
name : 'UTF-32' ,
value : 'UTF32' ,
} ,
] ,
default : 'UTF16' ,
2022-05-06 14:01:25 -07:00
description : 'The encoding type used by the API to calculate sentence offsets' ,
2020-11-19 00:06:03 -08:00
} ,
2020-11-19 00:04:20 -08:00
{
displayName : 'Language' ,
name : 'language' ,
type : 'options' ,
options : [
{
name : 'Arabic' ,
value : 'ar' ,
} ,
{
2022-05-20 14:47:24 -07:00
name : 'Chinese (Simplified)' ,
2020-11-19 00:04:20 -08:00
value : 'zh' ,
} ,
{
name : 'Chinese (Traditional)' ,
value : 'zh-Hant' ,
} ,
{
name : 'Dutch' ,
value : 'nl' ,
} ,
{
name : 'English' ,
value : 'en' ,
} ,
{
name : 'French' ,
value : 'fr' ,
} ,
{
name : 'German' ,
value : 'de' ,
} ,
{
name : 'Indonesian' ,
value : 'id' ,
} ,
{
name : 'Italian' ,
value : 'it' ,
} ,
{
name : 'Japanese' ,
value : 'ja' ,
} ,
{
name : 'Korean' ,
value : 'ko' ,
} ,
{
name : 'Portuguese (Brazilian & Continental)' ,
value : 'pt' ,
} ,
{
name : 'Spanish' ,
value : 'es' ,
} ,
{
name : 'Thai' ,
value : 'th' ,
} ,
{
name : 'Turkish' ,
value : 'tr' ,
} ,
{
name : 'Vietnamese' ,
value : 'vi' ,
} ,
] ,
default : 'en' ,
placeholder : '' ,
2022-08-17 08:50:24 -07:00
description :
'The language of the document (if not specified, the language is automatically detected). Both ISO and BCP-47 language codes are accepted.' ,
2020-11-19 00:04:20 -08:00
} ,
] ,
} ,
] ,
} ;
async execute ( this : IExecuteFunctions ) : Promise < INodeExecutionData [ ] [ ] > {
const items = this . getInputData ( ) ;
2022-04-22 09:29:51 -07:00
const length = items . length ;
2022-12-02 03:53:59 -08:00
const resource = this . getNodeParameter ( 'resource' , 0 ) ;
const operation = this . getNodeParameter ( 'operation' , 0 ) ;
2020-11-19 00:04:20 -08:00
const responseData = [ ] ;
for ( let i = 0 ; i < length ; i ++ ) {
if ( resource === 'document' ) {
if ( operation === 'analyzeSentiment' ) {
const source = this . getNodeParameter ( 'source' , i ) as string ;
2022-11-18 07:29:44 -08:00
const options = this . getNodeParameter ( 'options' , i ) ;
2023-01-13 09:11:56 -08:00
const encodingType = ( options . encodingType as string | undefined ) ? ? 'UTF16' ;
const documentType = ( options . documentType as string | undefined ) ? ? 'PLAIN_TEXT' ;
2020-11-19 00:04:20 -08:00
const body : IData = {
document : {
type : documentType ,
} ,
encodingType ,
} ;
if ( source === 'content' ) {
const content = this . getNodeParameter ( 'content' , i ) as string ;
body . document . content = content ;
} else {
const gcsContentUri = this . getNodeParameter ( 'gcsContentUri' , i ) as string ;
body . document . gcsContentUri = gcsContentUri ;
}
if ( options . language ) {
body . document . language = options . language as string ;
}
2022-08-17 08:50:24 -07:00
const response = await googleApiRequest . call (
this ,
'POST' ,
2022-12-29 03:20:43 -08:00
'/v1/documents:analyzeSentiment' ,
2022-08-17 08:50:24 -07:00
body ,
) ;
2020-11-19 00:04:20 -08:00
responseData . push ( response ) ;
}
}
}
return [ this . helpers . returnJsonArray ( responseData ) ] ;
}
}