n8n-nodes-chat-data
Version:
Chatdata integration for n8n. Manage chatbots, send messages, and retrieve leads from your Chatdata account.
249 lines • 11.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatDataTrigger = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const n8n_workflow_2 = require("n8n-workflow");
const TriggerDescription_1 = require("./TriggerDescription");
class ChatDataTrigger {
constructor() {
this.description = {
displayName: 'Chat Data Trigger',
name: 'chatDataTrigger',
icon: 'file:ChatData.svg',
group: ['trigger'],
version: 1,
subtitle: '={{$parameter["operation"]}}',
description: 'Triggers workflows on Chat Data events',
defaults: {
name: 'Chat Data Trigger',
},
inputs: [],
outputs: [n8n_workflow_1.NodeConnectionTypes.Main],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
isFullPath: false,
},
],
credentials: [
{
name: 'chatDataApi',
required: true,
},
],
properties: [
TriggerDescription_1.triggerResource,
...TriggerDescription_1.triggerOperations,
...TriggerDescription_1.triggerFields,
],
};
this.methods = {
loadOptions: {
async getChatbots() {
const credentials = await this.getCredentials('chatDataApi');
if (!credentials.baseUrl) {
throw new n8n_workflow_2.NodeOperationError(this.getNode(), new Error('Base URL is missing in credentials. Please check your credentials configuration.'), { itemIndex: 0 });
}
const baseUrl = credentials.baseUrl;
const baseUrlFormatted = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
const fullUrl = `${baseUrlFormatted}/api/v2/get-chatbots`;
try {
const response = await this.helpers.httpRequest({
url: fullUrl,
method: 'GET',
headers: {
Authorization: `Bearer ${credentials.apiKey}`,
'Accept': 'application/json',
'Content-Type': 'application/json',
},
json: true,
ignoreHttpStatusErrors: true,
});
if (response.status === 'error') {
throw new n8n_workflow_2.NodeOperationError(this.getNode(), response.message, { itemIndex: 0 });
}
if (!response.chatbots || !Array.isArray(response.chatbots)) {
throw new n8n_workflow_2.NodeOperationError(this.getNode(), new Error('Invalid response format. Expected chatbots array.'), {
itemIndex: 0,
});
}
const options = response.chatbots.map((chatbot) => ({
name: chatbot.chatbotName,
value: chatbot.chatbotId,
}));
return options;
}
catch (error) {
throw new n8n_workflow_2.NodeOperationError(this.getNode(), new Error(`Failed to fetch chatbots: ${error.message}`), { itemIndex: 0 });
}
},
},
};
this.webhookMethods = {
default: {
async checkExists() {
const webhookData = this.getWorkflowStaticData('node');
const chatbotId = webhookData.chatbotId;
const webhookUrl = webhookData.webhookUrl;
const eventType = webhookData.eventType;
if (chatbotId && webhookUrl && eventType) {
const currentWebhookUrl = this.getNodeWebhookUrl('default');
if (webhookUrl === currentWebhookUrl) {
return true;
}
else {
delete webhookData.chatbotId;
delete webhookData.eventType;
delete webhookData.webhookUrl;
return false;
}
}
return false;
},
async create() {
try {
const webhookUrl = this.getNodeWebhookUrl('default');
const operation = this.getNodeParameter('operation');
let eventType = '';
if (operation === 'onLeadSubmission') {
eventType = 'lead-submission';
}
else if (operation === 'onLiveChatEscalation') {
eventType = 'live-chat-escalation';
}
else if (operation === 'onNewMessage') {
eventType = 'chat';
}
else {
return false;
}
const chatbotId = this.getNodeParameter('chatbot_id');
const credentials = await this.getCredentials('chatDataApi');
const baseUrl = credentials.baseUrl;
const baseUrlFormatted = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
const fullUrl = `${baseUrlFormatted}/api/v2/add-webhook`;
const response = await this.helpers.httpRequest({
url: fullUrl,
method: 'POST',
body: {
url: webhookUrl,
event: eventType,
chatbotId,
},
headers: {
Authorization: `Bearer ${credentials.apiKey}`,
'Accept': 'application/json',
'Content-Type': 'application/json',
},
json: true,
ignoreHttpStatusErrors: true,
});
if (response.status === 'error') {
throw new n8n_workflow_2.NodeOperationError(this.getNode(), response.message || 'Failed to register webhook');
}
const webhookData = this.getWorkflowStaticData('node');
webhookData.chatbotId = chatbotId;
webhookData.eventType = eventType;
webhookData.webhookUrl = webhookUrl;
return true;
}
catch (error) {
throw error;
}
},
async delete() {
try {
const webhookData = this.getWorkflowStaticData('node');
const nodeSettings = this.getNode();
const workflowMode = this.getMode();
const isNodeRemoved = nodeSettings.disabled === true;
const isWorkflowDeleted = workflowMode === 'internal';
if (!isNodeRemoved && !isWorkflowDeleted) {
return true;
}
const chatbotId = webhookData.chatbotId;
const webhookUrl = webhookData.webhookUrl;
if (chatbotId && webhookUrl) {
const credentials = await this.getCredentials('chatDataApi');
const baseUrl = credentials.baseUrl;
const baseUrlFormatted = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
const fullUrl = `${baseUrlFormatted}/api/v2/delete-webhook`;
const response = await this.helpers.httpRequest({
url: fullUrl,
method: 'POST',
body: {
url: webhookUrl,
chatbotId,
},
headers: {
Authorization: `Bearer ${credentials.apiKey}`,
'Accept': 'application/json',
'Content-Type': 'application/json',
},
json: true,
ignoreHttpStatusErrors: true,
});
if (response.status === 'error') {
throw new n8n_workflow_2.NodeOperationError(this.getNode(), response.message, { itemIndex: 0 });
}
delete webhookData.chatbotId;
delete webhookData.eventType;
delete webhookData.webhookUrl;
return true;
}
return false;
}
catch (error) {
throw error;
}
},
},
};
}
async webhook() {
const req = this.getRequestObject();
let bodyData;
if (req.body && typeof req.body === 'string') {
try {
bodyData = JSON.parse(req.body);
}
catch (e) {
bodyData = this.getBodyData();
}
}
else {
bodyData = this.getBodyData();
}
if (typeof bodyData.messages === 'string') {
try {
bodyData.messages = JSON.parse(bodyData.messages);
}
catch (e) {
}
}
if (typeof bodyData.answer === 'string') {
try {
bodyData.answer = JSON.parse(bodyData.answer);
}
catch (e) {
}
}
if (typeof bodyData.lead === 'string') {
try {
bodyData.lead = JSON.parse(bodyData.lead);
}
catch (e) {
}
}
return {
workflowData: [
this.helpers.returnJsonArray(bodyData)
],
};
}
}
exports.ChatDataTrigger = ChatDataTrigger;
//# sourceMappingURL=ChatDataTrigger.node.js.map