@tuanltntu/n8n-nodes-bitrix24
Version:
Comprehensive n8n community node for Bitrix24 API integration with CRM, Tasks, Chat, Telephony, and more
185 lines • 6.92 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatResourceHandler = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const ResourceHandlerBase_1 = require("./ResourceHandlerBase");
class ChatResourceHandler extends ResourceHandlerBase_1.ResourceHandlerBase {
constructor(executeFunctions, returnData, options = {}) {
super(executeFunctions, returnData, options);
this.resourceEndpoints = {
sendMessage: "im.message.add",
getMessages: "im.dialog.messages.get",
getChat: "im.chat.get",
getChats: "im.recent.get",
createChat: "im.chat.add",
updateChat: "im.chat.update",
};
}
async process() {
for (let itemIndex = 0; itemIndex < this.items.length; itemIndex++) {
try {
const operation = this.getNodeParameter("operation", itemIndex);
switch (operation) {
case "sendMessage":
await this.handleSendMessage(itemIndex);
break;
case "getMessages":
await this.handleGetMessages(itemIndex);
break;
case "getChat":
await this.handleGetChat(itemIndex);
break;
case "getChats":
await this.handleGetChats(itemIndex);
break;
case "createChat":
await this.handleCreateChat(itemIndex);
break;
case "updateChat":
await this.handleUpdateChat(itemIndex);
break;
default:
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `Unsupported operation "${operation}" for Chat resource`, { itemIndex });
}
}
catch (error) {
if (this.continueOnFail()) {
this.addErrorToReturnData(error, itemIndex);
}
else {
throw error;
}
}
}
return this.returnData;
}
/**
* Handle send message operation
*/
async handleSendMessage(itemIndex) {
const chatId = this.getNodeParameter("chatId", itemIndex);
const message = this.getNodeParameter("message", itemIndex);
const additionalFields = this.getNodeParameter("additionalMessageFields", itemIndex, {});
const body = {
DIALOG_ID: chatId,
MESSAGE: message,
};
if (additionalFields.attach) {
try {
body.ATTACH = JSON.parse(additionalFields.attach);
}
catch (error) {
// Ignore invalid JSON
}
}
if (additionalFields.system) {
body.SYSTEM = additionalFields.system ? "Y" : "N";
}
if (additionalFields.urlPreview !== undefined) {
body.URL_PREVIEW = additionalFields.urlPreview ? "Y" : "N";
}
const responseData = await this.makeApiCall(this.resourceEndpoints.sendMessage, body, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle get messages operation
*/
async handleGetMessages(itemIndex) {
const chatId = this.getNodeParameter("chatId", itemIndex);
const options = this.getNodeParameter("options", itemIndex, {});
const body = {
DIALOG_ID: chatId,
};
if (options.lastId) {
body.LAST_ID = options.lastId;
}
if (options.firstId) {
body.FIRST_ID = options.firstId;
}
if (options.limit) {
body.LIMIT = options.limit;
}
const responseData = await this.makeApiCall(this.resourceEndpoints.getMessages, body, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle get chat operation
*/
async handleGetChat(itemIndex) {
const chatId = this.getNodeParameter("chatId", itemIndex);
const body = {
CHAT_ID: chatId,
};
const responseData = await this.makeApiCall(this.resourceEndpoints.getChat, body, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle get chats operation
*/
async handleGetChats(itemIndex) {
const options = this.getNodeParameter("options", itemIndex, {});
const body = {};
if (options.filter) {
try {
body.filter = JSON.parse(options.filter);
}
catch (error) {
// Ignore invalid JSON
}
}
if (options.order) {
try {
body.order = JSON.parse(options.order);
}
catch (error) {
// Ignore invalid JSON
}
}
if (options.start) {
body.start = options.start;
}
const responseData = await this.makeApiCall(this.resourceEndpoints.getChats, body, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle create chat operation
*/
async handleCreateChat(itemIndex) {
const chatTitle = this.getNodeParameter("chatTitle", itemIndex);
const chatType = this.getNodeParameter("chatType", itemIndex);
const users = this.getNodeParameter("users", itemIndex, "");
const body = {
TITLE: chatTitle,
TYPE: chatType,
};
if (users) {
const userIds = users.split(",").map((id) => id.trim());
body.USERS = userIds;
}
const responseData = await this.makeApiCall(this.resourceEndpoints.createChat, body, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle update chat operation
*/
async handleUpdateChat(itemIndex) {
const chatId = this.getNodeParameter("chatId", itemIndex);
const updateFields = this.getNodeParameter("updateFields", itemIndex, {});
const body = {
CHAT_ID: chatId,
};
if (updateFields.title) {
body.TITLE = updateFields.title;
}
if (updateFields.description) {
body.DESCRIPTION = updateFields.description;
}
if (updateFields.color) {
body.COLOR = updateFields.color;
}
const responseData = await this.makeApiCall(this.resourceEndpoints.updateChat, body, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
}
exports.ChatResourceHandler = ChatResourceHandler;
//# sourceMappingURL=ChatResourceHandler.js.map