Skip to content
This repository has been archived by the owner on Sep 14, 2024. It is now read-only.

Commit

Permalink
tambahan API
Browse files Browse the repository at this point in the history
  • Loading branch information
gms-renato committed Jan 12, 2024
1 parent 750b3dc commit 9034d0e
Show file tree
Hide file tree
Showing 4 changed files with 12,908 additions and 12,708 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
INodeProperties,
} from 'n8n-workflow';

export class Waha implements ICredentialType {
name = 'waha';
displayName = 'Waha';
export class WahaApi implements ICredentialType {
name = 'wahaApi';
displayName = 'Waha API';
properties: INodeProperties[] = [
{
displayName: 'Host URL',
Expand All @@ -18,8 +18,16 @@ export class Waha implements ICredentialType {
displayName: 'API Key',
name: 'apiKey',
type: 'string',
typeOptions: { password: true },
default: '',
},
{
displayName: 'Session',
name: 'session',
type: 'string',
typeOptions: { password: true },
default: 'default',
},
];

authenticate: IAuthenticateGeneric = {
Expand All @@ -32,7 +40,7 @@ export class Waha implements ICredentialType {
};
test: ICredentialTestRequest = {
request: {
baseURL: '{{$credentials.url}}/api',
baseURL: '={{$credentials.url}}/api',
url: '/sessions',
},
};
Expand Down
276 changes: 234 additions & 42 deletions nodes/Waha/Waha.node.ts
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 {
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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'],
Expand All @@ -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,
});
}
}
}
Expand Down
Loading

0 comments on commit 9034d0e

Please sign in to comment.