UNPKG

n8n-nodes-agentic-hub

Version:

n8n community node for Agentic Hub API integration - Send messages to conversational AI workflows

500 lines 21.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AgenticHubMessage = void 0; const n8n_workflow_1 = require("n8n-workflow"); class AgenticHubMessage { constructor() { this.description = { displayName: 'Agentic Hub Message', name: 'agenticHubMessage', icon: 'file:agentic-hub.svg', group: ['transform'], version: 1, description: 'Send messages to Agentic Hub API for conversational AI workflows', defaults: { name: 'Agentic Hub Message', }, subtitle: '={{$parameter["resource"] === "message" ? ($parameter["direction"] === 0 ? "Client Message" : $parameter["direction"] === 1 ? "AI Agent Message" : "Human Message") : $parameter["agentOperation"]}}', inputs: ["main"], outputs: ["main"], usableAsTool: true, credentials: [ { name: 'agenticHubApi', required: true, }, ], properties: [ { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'Message', value: 'message', description: 'Send messages to conversations', }, { name: 'Agent', value: 'agent', description: 'Get agent information', }, ], default: 'message', }, { displayName: 'Direction', name: 'direction', type: 'options', default: 0, required: true, description: 'Message direction', options: [ { name: 'Client', value: 0, description: 'Message from client/user', action: 'Send client message', }, { name: 'AI Agent', value: 1, description: 'Message from AI agent', action: 'Send AI agent message', }, { name: 'Human', value: 2, description: 'Message from human agent', action: 'Send human message', }, ], displayOptions: { show: { resource: ['message'], }, }, }, { displayName: 'Workflow ID', name: 'workflowId', type: 'string', default: '', required: true, placeholder: 'Enter Workflow ID', description: 'The unique identifier of the workflow', displayOptions: { show: { resource: ['message'], }, }, }, { displayName: 'Session ID', name: 'sessionId', type: 'string', default: '', required: true, placeholder: 'Enter session ID', description: 'Unique session identifier', displayOptions: { show: { resource: ['message'], }, }, }, { displayName: 'Phone Number', name: 'phoneNumber', type: 'string', default: '', required: true, placeholder: 'Enter phone number', description: 'Phone number associated with the conversation', displayOptions: { show: { resource: ['message'], }, }, }, { displayName: 'Content', name: 'content', type: 'string', typeOptions: { rows: 4, }, default: '', required: true, placeholder: 'Enter your message content', description: 'Message content', displayOptions: { show: { resource: ['message'], }, }, }, { displayName: 'Additional Options', name: 'additionalOptions', type: 'collection', placeholder: 'Add Option', default: {}, displayOptions: { show: { resource: ['message'], }, }, options: [ { displayName: 'Conversation Status', name: 'conversationStatus', type: 'options', default: 1, options: [ { name: 'Inactive', value: 0, }, { name: 'Active', value: 1, }, ], }, { displayName: 'Created At', name: 'createdAt', type: 'dateTime', default: '', description: 'When the conversation was created (ISO 8601 format)', }, { displayName: 'Error Message', name: 'errorMessage', type: 'string', default: '', description: 'Error message if any', }, { displayName: 'Human Handling Time', name: 'humanHandlingTime', type: 'string', default: '', description: 'Time spent by human handling the conversation', }, { displayName: 'IP Address', name: 'ipAddress', type: 'string', default: '', description: 'IP address of the user', }, { displayName: 'Is Handled By Human', name: 'isHandledByHuman', type: 'boolean', default: false, description: 'Whether conversation is currently handled by human', }, { displayName: 'Last Context', name: 'lastContext', type: 'string', default: '', description: 'Last context or summary of the conversation', }, { displayName: 'Last Human Activity', name: 'lastHumanActivity', type: 'dateTime', default: '', description: 'When human last interacted with the conversation', }, { displayName: 'Message SID', name: 'messageSid', type: 'string', default: '', description: 'Message SID from external service', }, { displayName: 'Order ID', name: 'orderId', type: 'number', default: 0, description: 'Associated order ID', }, { displayName: 'Overall Sentiment', name: 'overallSentiment', type: 'number', default: 100, description: 'Overall sentiment score (0-100)', typeOptions: { minValue: 0, maxValue: 100, }, }, { displayName: 'Requires Human Attention', name: 'requiresHumanAttention', type: 'boolean', default: false, description: 'Whether conversation requires human intervention', }, { displayName: 'Sent At', name: 'sentAt', type: 'dateTime', default: '', description: 'When the message was sent (ISO 8601 format)', }, { displayName: 'Sentiment Score', name: 'sentimentScore', type: 'number', default: 100, description: 'Sentiment score of the message (0-100)', typeOptions: { minValue: 0, maxValue: 100, }, }, { displayName: 'Status', name: 'status', type: 'options', default: 1, description: 'Message status', options: [ { name: 'Failed', value: 0, }, { name: 'Sent', value: 1, }, ], }, { displayName: 'Status Updated At', name: 'statusUpdatedAt', type: 'dateTime', default: '', description: 'When the status was last updated (ISO 8601 format)', }, { displayName: 'Template ID', name: 'templateId', type: 'string', default: '', description: 'Template ID used for the message', }, { displayName: 'Updated At', name: 'updatedAt', type: 'dateTime', default: '', description: 'When the conversation was last updated (ISO 8601 format)', }, ], }, { displayName: 'Operation', name: 'agentOperation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['agent'], }, }, options: [ { name: 'Get Agent Info', value: 'getAgentInfo', description: 'Get information about a specific agent', action: 'Get agent information', }, { name: 'Get All Agents', value: 'getAllAgents', description: 'Get all agents by workflow', action: 'Get all agents', }, ], default: 'getAgentInfo', }, { displayName: 'WorkflowPublicId', name: 'agentWorkflowPublicId', type: 'string', default: '', placeholder: 'Enter workflow public ID (UUID)', description: 'The public UUID identifier for the workflow', required: true, displayOptions: { show: { resource: ['agent'], }, }, }, { displayName: 'AgentPublicId', name: 'agentPublicId', type: 'string', default: '', placeholder: 'Enter agent public ID (UUID)', description: 'The public UUID identifier for the agent', required: true, displayOptions: { show: { resource: ['agent'], agentOperation: ['getAgentInfo'], }, }, }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; for (let itemIndex = 0; itemIndex < items.length; itemIndex++) { try { const credentials = await this.getCredentials('agenticHubApi'); const resource = this.getNodeParameter('resource', itemIndex); let response; const now = new Date().toISOString(); if (resource === 'message') { const workflowId = this.getNodeParameter('workflowId', itemIndex); const sessionId = this.getNodeParameter('sessionId', itemIndex); const phoneNumber = this.getNodeParameter('phoneNumber', itemIndex); const content = this.getNodeParameter('content', itemIndex); const direction = this.getNodeParameter('direction', itemIndex); const additionalOptions = this.getNodeParameter('additionalOptions', itemIndex, {}); const message = { content, sessionId, direction, sentAt: additionalOptions.sentAt || now, templateId: additionalOptions.templateId || '', errorMessage: additionalOptions.errorMessage || '', messageSid: additionalOptions.messageSid || '', sentimentScore: additionalOptions.sentimentScore || 100, status: additionalOptions.status || 1, statusUpdatedAt: additionalOptions.statusUpdatedAt || now, }; const conversation = { sessionid: sessionId, createdAt: additionalOptions.createdAt || now, updatedAt: additionalOptions.updatedAt || now, lastContext: additionalOptions.lastContext || '', ipAddress: additionalOptions.ipAddress || '', phoneNumber, status: additionalOptions.conversationStatus || 1, overallSentiment: additionalOptions.overallSentiment || 100, requiresHumanAttention: additionalOptions.requiresHumanAttention || false, humanHandlingTime: additionalOptions.humanHandlingTime || '', isHandledByHuman: additionalOptions.isHandledByHuman || false, lastHumanActivity: additionalOptions.lastHumanActivity || now, orderId: additionalOptions.orderId || 0, isUnread: true, }; const requestBody = { conversation, message, }; const options = { method: 'POST', url: `https://app.agentic-hub.ai/api/v1/workflows/${workflowId}/conversations-with-message`, headers: { 'x-company-id': credentials.companyId, 'x-api-key': credentials.apiKey, }, body: requestBody, json: true, }; response = await this.helpers.httpRequest(options); returnData.push({ json: { success: true, response, requestBody, timestamp: now, operation: 'send message', }, pairedItem: itemIndex, }); } else if (resource === 'agent') { const agentWorkflowPublicId = this.getNodeParameter('agentWorkflowPublicId', itemIndex); const operation = this.getNodeParameter('agentOperation', itemIndex); if (operation === 'getAgentInfo') { const agentPublicId = this.getNodeParameter('agentPublicId', itemIndex); const options = { method: 'GET', url: `https://app.agentic-hub.ai/api/v1/agents/${agentPublicId}`, headers: { 'x-company-id': credentials.companyId, 'x-api-key': credentials.apiKey, }, json: true, }; response = await this.helpers.httpRequest(options); } else if (operation === 'getAllAgents') { const options = { method: 'GET', url: `https://app.agentic-hub.ai/api/v1/workflows/${agentWorkflowPublicId}/agents`, headers: { 'x-company-id': credentials.companyId, 'x-api-key': credentials.apiKey, }, json: true, }; response = await this.helpers.httpRequest(options); } returnData.push({ json: { success: true, response, timestamp: now, operation: `${operation} agent`, }, pairedItem: itemIndex, }); } } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { success: false, error: error.message, timestamp: new Date().toISOString(), }, error, pairedItem: itemIndex, }); } else { if (error.context) { error.context.itemIndex = itemIndex; throw error; } throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex, }); } } } return [returnData]; } } exports.AgenticHubMessage = AgenticHubMessage; //# sourceMappingURL=AgenticHubMessage.node.js.map