139 lines
3.4 KiB
TypeScript
139 lines
3.4 KiB
TypeScript
import { IExecuteFunctions } from 'n8n-core';
|
|
import {
|
|
INodeExecutionData,
|
|
INodeType,
|
|
INodeTypeDescription,
|
|
NodeOperationError,
|
|
} from 'n8n-workflow';
|
|
import { client, xml } from '@xmpp/client';
|
|
|
|
export class Xmpp implements INodeType {
|
|
description: INodeTypeDescription = {
|
|
displayName: 'XMPP',
|
|
name: 'xmpp',
|
|
icon: 'file:xmpp.svg',
|
|
group: ['communication'],
|
|
version: 1,
|
|
subtitle: '={{$parameter["operation"]}}',
|
|
description: 'Send messages via XMPP protocol',
|
|
defaults: {
|
|
name: 'XMPP',
|
|
},
|
|
inputs: ['main'],
|
|
outputs: ['main'],
|
|
credentials: [
|
|
{
|
|
name: 'xmppApi',
|
|
required: true,
|
|
},
|
|
],
|
|
requestDefaults: {
|
|
headers: {
|
|
Accept: 'application/json',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
},
|
|
properties: [
|
|
{
|
|
displayName: 'Operation',
|
|
name: 'operation',
|
|
type: 'options',
|
|
noDataExpression: false,
|
|
options: [
|
|
{
|
|
name: 'Send Message',
|
|
value: 'sendMessage',
|
|
description: 'Send a message to a user or room',
|
|
action: 'Send a message',
|
|
},
|
|
],
|
|
default: 'sendMessage',
|
|
},
|
|
{
|
|
displayName: 'To',
|
|
name: 'to',
|
|
type: 'string',
|
|
default: '',
|
|
placeholder: 'user@domain.com',
|
|
description: 'Recipient JID',
|
|
required: true,
|
|
displayOptions: {
|
|
show: {
|
|
operation: ['sendMessage'],
|
|
},
|
|
},
|
|
},
|
|
{
|
|
displayName: 'Message',
|
|
name: 'message',
|
|
type: 'string',
|
|
default: '',
|
|
description: 'Message to send',
|
|
required: true,
|
|
displayOptions: {
|
|
show: {
|
|
operation: ['sendMessage'],
|
|
},
|
|
},
|
|
},
|
|
],
|
|
};
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
const items = this.getInputData();
|
|
const returnData: INodeExecutionData[] = [];
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
try {
|
|
const operation = this.getNodeParameter('operation', i) as string;
|
|
const credentials = await this.getCredentials('xmppApi', i);
|
|
|
|
if (operation === 'sendMessage') {
|
|
const to = this.getNodeParameter('to', i) as string;
|
|
const message = this.getNodeParameter('message', i) as string;
|
|
|
|
// Create XMPP client
|
|
const xmppClient = client({
|
|
service: `xmpp://${credentials.domain}:${credentials.port}`,
|
|
username: credentials.username,
|
|
password: credentials.password,
|
|
});
|
|
|
|
// Connect
|
|
await xmppClient.start();
|
|
|
|
// Send message
|
|
const messageStanza = xml(
|
|
'message',
|
|
{ to, type: 'chat' },
|
|
xml('body', {}, message),
|
|
);
|
|
|
|
await xmppClient.send(messageStanza);
|
|
await xmppClient.stop();
|
|
|
|
returnData.push({
|
|
json: {
|
|
success: true,
|
|
to,
|
|
message,
|
|
timestamp: new Date().toISOString(),
|
|
},
|
|
});
|
|
}
|
|
} catch (error) {
|
|
if (this.continueOnFail()) {
|
|
returnData.push({
|
|
json: {
|
|
error: error.message,
|
|
},
|
|
});
|
|
continue;
|
|
}
|
|
throw new NodeOperationError(this.getNode(), error);
|
|
}
|
|
}
|
|
|
|
return [returnData];
|
|
}
|
|
}
|