This repository has been archived by the owner on Sep 14, 2024. It is now read-only.
generated from n8n-io/n8n-nodes-starter
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
750b3dc
commit 9034d0e
Showing
4 changed files
with
12,908 additions
and
12,708 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
|
||
import { IExecuteFunctions } from 'n8n-core'; | ||
import { | ||
IExecuteFunctions, | ||
} from 'n8n-core'; | ||
import { INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow'; | ||
IBinaryData, | ||
IBinaryKeyData, | ||
INodeExecutionData, | ||
INodeType, | ||
INodeTypeDescription, | ||
} from 'n8n-workflow'; | ||
import { OptionsWithUri } from 'request'; | ||
|
||
export class Waha implements INodeType { | ||
|
@@ -16,10 +20,19 @@ export class Waha implements INodeType { | |
noDataExpression: true, | ||
options: [ | ||
{ | ||
name: 'Chatting', value: 'chatting' | ||
} | ||
name: 'Chatting', | ||
value: 'chatting', | ||
}, | ||
{ | ||
name: 'Session', | ||
value: 'session', | ||
}, | ||
{ | ||
name: 'Auth', | ||
value: 'auth', | ||
}, | ||
], | ||
default: 'chatting' | ||
default: 'chatting', | ||
}, | ||
{ | ||
displayName: 'Operation', | ||
|
@@ -28,20 +41,57 @@ export class Waha implements INodeType { | |
noDataExpression: true, | ||
displayOptions: { | ||
show: { | ||
resource: [ | ||
'chatting', | ||
], | ||
resource: ['chatting'], | ||
}, | ||
}, | ||
options: [ | ||
{ | ||
name: 'SendText', | ||
value: 'sendText', | ||
action: 'Send Text', | ||
description: 'Send Text Message' | ||
} | ||
action: 'Send text', | ||
description: 'Send Text Message', | ||
}, | ||
], | ||
default: 'sendText', | ||
}, | ||
{ | ||
displayName: 'Operation', | ||
name: 'operation', | ||
type: 'options', | ||
noDataExpression: true, | ||
displayOptions: { | ||
show: { | ||
resource: ['session'], | ||
}, | ||
}, | ||
options: [ | ||
{ | ||
name: 'Log Out', | ||
value: 'logout', | ||
action: 'Log out', | ||
}, | ||
{ | ||
name: 'Me', | ||
value: 'me', | ||
action: 'Me', | ||
}, | ||
{ | ||
name: 'Sessions', | ||
value: 'sessions', | ||
action: 'Sessions', | ||
}, | ||
{ | ||
name: 'Start', | ||
value: 'start', | ||
action: 'Start', | ||
}, | ||
{ | ||
name: 'Stop', | ||
value: 'stop', | ||
action: 'Stop', | ||
}, | ||
], | ||
default: 'sendText' | ||
default: 'start', | ||
}, | ||
{ | ||
displayName: 'Chat ID', | ||
|
@@ -51,12 +101,11 @@ export class Waha implements INodeType { | |
displayOptions: { | ||
show: { | ||
operation: ['sendText'], | ||
resource: ['chatting'] | ||
} | ||
resource: ['chatting'], | ||
}, | ||
}, | ||
default: '', | ||
placeholder: '[email protected]', | ||
description: 'Chat ID', | ||
}, | ||
{ | ||
displayName: 'Text', | ||
|
@@ -66,17 +115,50 @@ export class Waha implements INodeType { | |
displayOptions: { | ||
show: { | ||
operation: ['sendText'], | ||
resource: ['chatting'] | ||
} | ||
resource: ['chatting'], | ||
}, | ||
}, | ||
default: '', | ||
placeholder: '', | ||
description: 'Text to send', | ||
} | ||
}, | ||
{ | ||
displayName: 'Operation', | ||
name: 'operation', | ||
type: 'options', | ||
noDataExpression: true, | ||
displayOptions: { | ||
show: { | ||
resource: ['auth'], | ||
}, | ||
}, | ||
options: [ | ||
{ | ||
name: 'QR', | ||
value: 'qr', | ||
action: 'QR', | ||
}, | ||
], | ||
default: 'qr', | ||
}, | ||
{ | ||
displayName: 'Webhook URL', | ||
name: 'webhookUrl', | ||
type: 'string', | ||
required: true, | ||
displayOptions: { | ||
show: { | ||
operation: ['start'], | ||
resource: ['session'], | ||
}, | ||
}, | ||
default: '', | ||
placeholder: 'https://n8n.gms.church/xxxxxxxxx', | ||
}, | ||
], | ||
version: 1, | ||
version: 2, | ||
defaults: { | ||
name: 'Waha' | ||
name: 'Waha', | ||
}, | ||
inputs: ['main'], | ||
outputs: ['main'], | ||
|
@@ -87,45 +169,155 @@ export class Waha implements INodeType { | |
description: 'Connect with Whatsapp HTTP API', | ||
credentials: [ | ||
{ | ||
name: 'Waha', | ||
required: true | ||
} | ||
name: 'wahaApi', | ||
required: true, | ||
}, | ||
], | ||
requestDefaults: { | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
} | ||
baseURL: '={{$credentials.url}}/api', | ||
}, | ||
}; | ||
|
||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { | ||
const cred = await this.getCredentials('wahaApi'); | ||
const items = this.getInputData(); | ||
const returnData: INodeExecutionData[] = []; | ||
const resource = this.getNodeParameter('resource', 0); | ||
const operation = this.getNodeParameter('operation', 0) as string; | ||
var options: OptionsWithUri = { | ||
uri: `${cred.url}/api`, | ||
json: true, | ||
}; | ||
for (let i = 0; i < items.length; i++) { | ||
if (resource === "chatting") { | ||
if (operation === "sendText") { | ||
if (resource === 'chatting') { | ||
if (operation === 'sendText') { | ||
const chatId = this.getNodeParameter('chatId', i) as string; | ||
const text = this.getNodeParameter('text', i) as string; | ||
const session = 'default'; | ||
const options: OptionsWithUri = { | ||
headers: { | ||
Accept: 'application/json', | ||
}, | ||
method: 'POST', | ||
body: { | ||
chatId: chatId, | ||
text: text, | ||
session: session | ||
options.method = 'POST'; | ||
options.uri += '/sendText'; | ||
options.body = { | ||
chatId: chatId, | ||
text: text, | ||
session: session, | ||
}; | ||
const responseData = await this.helpers.requestWithAuthentication.call( | ||
this, | ||
'wahaApi', | ||
options, | ||
); | ||
returnData.push({ | ||
json: responseData, | ||
}); | ||
} | ||
} else if (resource === 'session') { | ||
if (operation === 'start') { | ||
const url = this.getNodeParameter('webhookUrl', i) as string; | ||
options.method = 'POST'; | ||
options.uri += '/sessions/start'; | ||
options.body = { | ||
name: 'default', | ||
config: { | ||
proxy: null, | ||
webhooks: [ | ||
{ | ||
url: url, | ||
events: ['message', 'session.status'], | ||
hmac: null, | ||
retries: null, | ||
customHeaders: null, | ||
}, | ||
], | ||
}, | ||
baseUrl: '={{$credentials.url}}/api', | ||
uri: 'sendText', | ||
json: true | ||
} | ||
const responseData = await this.helpers.requestWithAuthentication.call(this, 'waha', options); | ||
returnData.push(responseData) | ||
}; | ||
const responseData = await this.helpers.requestWithAuthentication.call( | ||
this, | ||
'wahaApi', | ||
options, | ||
); | ||
returnData.push({ | ||
json: responseData, | ||
}); | ||
} else if (operation === 'stop') { | ||
options.method = 'POST'; | ||
options.uri += '/sessions/stop'; | ||
options.body = { | ||
name: cred.session, | ||
logout: false, | ||
}; | ||
const responseData = await this.helpers.requestWithAuthentication.call( | ||
this, | ||
'wahaApi', | ||
options, | ||
); | ||
returnData.push({ | ||
json: responseData, | ||
}); | ||
} else if (operation === 'logout') { | ||
options.method = 'POST'; | ||
options.uri += '/sessions/stop'; | ||
options.body = { | ||
name: cred.session, | ||
}; | ||
const responseData = await this.helpers.requestWithAuthentication.call( | ||
this, | ||
'wahaApi', | ||
options, | ||
); | ||
returnData.push({ | ||
json: responseData, | ||
}); | ||
} else if (operation === 'sessions') { | ||
options.method = 'GET'; | ||
options.uri += '/sessions'; | ||
options.qs = { | ||
all: 'true', | ||
}; | ||
const responseData = await this.helpers.requestWithAuthentication.call( | ||
this, | ||
'wahaApi', | ||
options, | ||
); | ||
returnData.push({ | ||
json: responseData, | ||
}); | ||
} else if (operation === 'me') { | ||
options.method = 'GET'; | ||
options.uri += `/sessions/${cred.session}/me`; | ||
const responseData = await this.helpers.requestWithAuthentication.call( | ||
this, | ||
'wahaApi', | ||
options, | ||
); | ||
returnData.push({ | ||
json: responseData, | ||
}); | ||
} | ||
} else if (resource === 'auth') { | ||
if (operation === 'qr') { | ||
options.method = 'GET'; | ||
options.uri += `/${cred.session}/auth/qr?format=image`; | ||
const responseData = await this.helpers.requestWithAuthentication.call( | ||
this, | ||
'wahaApi', | ||
options, | ||
); | ||
let fileName = 'qrcode.png'; | ||
const mimeType = responseData.mimetype; | ||
// binaryData = Buffer.from(JSON.stringify(responseData)); | ||
const binaryPropertyName = 'qrcode'; | ||
const data = responseData.data; | ||
const binary = { | ||
[binaryPropertyName]: { data, fileName, mimeType } as IBinaryData, | ||
} as IBinaryKeyData; | ||
returnData.push({ | ||
json: responseData, | ||
binary, | ||
}); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.