UNPKG

@tuanltntu/n8n-nodes-bitrix24

Version:

Comprehensive n8n community node for Bitrix24 API integration with CRM, Tasks, Chat, Telephony, and more

197 lines 8.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ChatboxResourceHandler = void 0; const n8n_workflow_1 = require("n8n-workflow"); const ResourceHandlerBase_1 = require("./ResourceHandlerBase"); class ChatboxResourceHandler extends ResourceHandlerBase_1.ResourceHandlerBase { constructor(executeFunctions, returnData) { super(executeFunctions, returnData); this.resourceEndpoints = { chat: { send: "im.message.add", update: "im.message.update", delete: "im.message.delete", createChat: "im.chat.add", addUsersToChat: "im.chat.user.add", }, notification: { sendNotification: "im.notify", }, }; } async process() { for (let itemIndex = 0; itemIndex < this.executeFunctions.getInputData().length; itemIndex++) { try { const chatResource = this.executeFunctions.getNodeParameter("chatResource", itemIndex); const operation = this.executeFunctions.getNodeParameter("operation", itemIndex); switch (chatResource) { case "chat": await this.processChatOperation(operation, itemIndex); break; case "notification": await this.processNotificationOperation(operation, itemIndex); break; default: throw new Error(`The chat resource "${chatResource}" is not supported!`); } } catch (error) { if (error instanceof Error) { this.addResponseToReturnData({ error: error.message }, itemIndex); } else { this.addResponseToReturnData({ error: "Unknown error occurred" }, itemIndex); } } } return this.returnData; } async processChatOperation(operation, itemIndex) { switch (operation) { case "send": await this.handleSendMessage(itemIndex); break; case "update": await this.handleUpdateMessage(itemIndex); break; case "delete": await this.handleDeleteMessage(itemIndex); break; case "createChat": await this.handleCreateChat(itemIndex); break; case "addUsersToChat": await this.handleAddUsersToChat(itemIndex); break; default: throw new Error(`The operation "${operation}" is not supported for chat resource!`); } } async processNotificationOperation(operation, itemIndex) { switch (operation) { case "sendNotification": await this.handleSendNotification(itemIndex); break; default: throw new Error(`The operation "${operation}" is not supported for notification resource!`); } } async handleSendMessage(itemIndex) { const chatId = this.executeFunctions.getNodeParameter("chatId", itemIndex); const message = this.executeFunctions.getNodeParameter("message", itemIndex); const options = this.executeFunctions.getNodeParameter("options", itemIndex, {}); const requestData = { DIALOG_ID: chatId, MESSAGE: message, }; if (options.system) { requestData.SYSTEM = "Y"; } // Handle file attachments if needed if (options.attachFiles && options.binaryPropertyName) { const binaryData = this.executeFunctions.getInputData()[itemIndex].binary; if (binaryData && binaryData[options.binaryPropertyName]) { const fileData = binaryData[options.binaryPropertyName]; if (!requestData.ATTACH) { requestData.ATTACH = []; } const attachItem = { NAME: fileData.fileName || "file", DATA: fileData.data, }; requestData.ATTACH.push(attachItem); } } // Handle custom parameters if provided if (options.customParameters) { try { const customParams = this.parseJsonParameter(options.customParameters, "Failed to parse custom parameters as JSON", itemIndex); Object.assign(requestData, customParams); } catch (error) { // If parsing fails, continue without custom parameters console.error("Error parsing custom parameters:", error); } } const response = await this.makeApiCall(this.resourceEndpoints.chat.send, requestData, {}, itemIndex); this.addResponseToReturnData(response, itemIndex); } async handleUpdateMessage(itemIndex) { const messageId = this.executeFunctions.getNodeParameter("messageId", itemIndex); const message = this.executeFunctions.getNodeParameter("message", itemIndex); const requestData = { MESSAGE_ID: messageId, MESSAGE: message, }; const response = await this.makeApiCall(this.resourceEndpoints.chat.update, requestData, {}, itemIndex); this.addResponseToReturnData(response, itemIndex); } async handleDeleteMessage(itemIndex) { const messageId = this.executeFunctions.getNodeParameter("messageId", itemIndex); const requestData = { MESSAGE_ID: messageId, }; const response = await this.makeApiCall(this.resourceEndpoints.chat.delete, requestData, {}, itemIndex); this.addResponseToReturnData(response, itemIndex); } async handleCreateChat(itemIndex) { const title = this.executeFunctions.getNodeParameter("title", itemIndex); const userIdsString = this.executeFunctions.getNodeParameter("userIds", itemIndex); // Split the comma-separated user IDs and trim whitespace const userIds = userIdsString .split(",") .map((id) => id.trim()) .filter((id) => id.length > 0); const requestData = { TITLE: title, USERS: userIds, }; const response = await this.makeApiCall(this.resourceEndpoints.chat.createChat, requestData, {}, itemIndex); this.addResponseToReturnData(response, itemIndex); } async handleAddUsersToChat(itemIndex) { const chatId = this.executeFunctions.getNodeParameter("chatId", itemIndex); const userIdsString = this.executeFunctions.getNodeParameter("userIds", itemIndex); // Split the comma-separated user IDs and trim whitespace const userIds = userIdsString .split(",") .map((id) => id.trim()) .filter((id) => id.length > 0); const requestData = { CHAT_ID: chatId, USERS: userIds, }; const response = await this.makeApiCall(this.resourceEndpoints.chat.addUsersToChat, requestData, {}, itemIndex); this.addResponseToReturnData(response, itemIndex); } async handleSendNotification(itemIndex) { const userId = this.executeFunctions.getNodeParameter("userId", itemIndex); const message = this.executeFunctions.getNodeParameter("message", itemIndex); const tag = this.executeFunctions.getNodeParameter("tag", itemIndex, ""); const customParamsStr = this.executeFunctions.getNodeParameter("customParameters", itemIndex, ""); const requestData = { USER_ID: userId, MESSAGE: message, }; if (tag) { requestData.TAG = tag; } // Handle custom parameters if provided if (customParamsStr && customParamsStr.trim() !== "") { try { const customParams = this.parseJsonParameter(customParamsStr, "Failed to parse custom parameters as JSON", itemIndex); Object.assign(requestData, customParams); } catch (error) { // If parsing fails, continue without custom parameters if (error instanceof Error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `Failed to parse custom parameters: ${error.message}`, { itemIndex }); } } } const response = await this.makeApiCall(this.resourceEndpoints.notification.sendNotification, requestData, {}, itemIndex); this.addResponseToReturnData(response, itemIndex); } } exports.ChatboxResourceHandler = ChatboxResourceHandler; //# sourceMappingURL=ChatboxResourceHandler.js.map