@tuanltntu/n8n-nodes-bitrix24
Version:
Comprehensive n8n community node for Bitrix24 API integration with CRM, Tasks, Chat, Telephony, and more
461 lines • 21.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatbotResourceHandler = void 0;
const ResourceHandlerBase_1 = require("./ResourceHandlerBase");
class ChatbotResourceHandler extends ResourceHandlerBase_1.ResourceHandlerBase {
constructor(executeFunctions, returnData, options = {}) {
super(executeFunctions, returnData, options);
this.resourceEndpoints = {
botManagement: {
registerBot: "imbot.register",
unregisterBot: "imbot.unregister",
updateBot: "imbot.update",
},
botCommand: {
registerCommand: "imbot.command.register",
unregisterCommand: "imbot.command.unregister",
updateCommand: "imbot.command.update",
answerCommand: "imbot.command.answer",
},
botMessage: {
addMessage: "imbot.message.add",
updateMessage: "imbot.message.update",
deleteMessage: "imbot.message.delete",
sendTyping: "imbot.chat.sendTyping",
},
botChat: {
chatGet: "imbot.chat.get",
chatLeave: "imbot.chat.leave",
chatSetOwner: "imbot.chat.setOwner",
chatUpdateAvatar: "imbot.chat.updateAvatar",
chatUpdateColor: "imbot.chat.updateColor",
chatUpdateTitle: "imbot.chat.updateTitle",
chatUserAdd: "imbot.chat.user.add",
chatUserList: "imbot.chat.user.list",
chatUserDelete: "imbot.chat.user.delete",
},
};
}
async process() {
for (let itemIndex = 0; itemIndex < this.executeFunctions.getInputData().length; itemIndex++) {
try {
const operation = this.executeFunctions.getNodeParameter("operation", itemIndex);
switch (operation) {
// Bot Management Operations
case "registerBot":
await this.handleRegisterBot(itemIndex);
break;
case "unregisterBot":
await this.handleUnregisterBot(itemIndex);
break;
case "updateBot":
await this.handleUpdateBot(itemIndex);
break;
// Bot Command Operations
case "registerCommand":
await this.handleRegisterCommand(itemIndex);
break;
case "unregisterCommand":
await this.handleUnregisterCommand(itemIndex);
break;
case "updateCommand":
await this.handleUpdateCommand(itemIndex);
break;
case "answerCommand":
await this.handleAnswerCommand(itemIndex);
break;
// Bot Message Operations
case "sendMessage":
await this.handleAddMessage(itemIndex);
break;
case "addMessage":
await this.handleAddMessage(itemIndex);
break;
case "updateMessage":
await this.handleUpdateMessage(itemIndex);
break;
case "deleteMessage":
await this.handleDeleteMessage(itemIndex);
break;
case "sendTyping":
await this.handleSendTyping(itemIndex);
break;
// Bot Chat Operations
case "chatGet":
await this.handleChatGet(itemIndex);
break;
case "chatLeave":
await this.handleChatLeave(itemIndex);
break;
case "chatSetOwner":
await this.handleChatSetOwner(itemIndex);
break;
case "chatUpdateAvatar":
await this.handleChatUpdateAvatar(itemIndex);
break;
case "chatUpdateColor":
await this.handleChatUpdateColor(itemIndex);
break;
case "chatUpdateTitle":
await this.handleChatUpdateTitle(itemIndex);
break;
case "chatUserAdd":
await this.handleChatUserAdd(itemIndex);
break;
case "chatUserList":
await this.handleChatUserList(itemIndex);
break;
case "chatUserDelete":
await this.handleChatUserDelete(itemIndex);
break;
default:
throw new Error(`The operation "${operation}" 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;
}
/* Bot Command Operations */
async handleRegisterCommand(itemIndex) {
const botId = this.executeFunctions.getNodeParameter("botId", itemIndex);
const command = this.executeFunctions.getNodeParameter("command", itemIndex);
const commandHandler = this.executeFunctions.getNodeParameter("commandHandler", itemIndex);
const options = this.executeFunctions.getNodeParameter("options", itemIndex, {});
const requestData = {
BOT_ID: botId,
COMMAND: command,
COMMON: "N",
HIDDEN: "N",
EXTRANET_SUPPORT: "N",
EVENT_COMMAND_ADD: commandHandler,
};
// Add optional parameters
if (options.commandDescription) {
requestData.COMMAND_PARAMS = options.commandDescription;
}
if (options.isCommon) {
requestData.COMMON = options.isCommon === true ? "Y" : "N";
}
if (options.isHidden) {
requestData.HIDDEN = options.isHidden === true ? "Y" : "N";
}
if (options.allowExtranet) {
requestData.EXTRANET_SUPPORT = options.allowExtranet === true ? "Y" : "N";
}
const response = await this.makeApiCall(this.resourceEndpoints.botCommand.registerCommand, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
async handleUnregisterCommand(itemIndex) {
const botId = this.executeFunctions.getNodeParameter("botId", itemIndex);
const commandId = this.executeFunctions.getNodeParameter("commandId", itemIndex);
const requestData = {
BOT_ID: botId,
COMMAND_ID: commandId,
};
const response = await this.makeApiCall(this.resourceEndpoints.botCommand.unregisterCommand, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
async handleUpdateCommand(itemIndex) {
const botId = this.executeFunctions.getNodeParameter("botId", itemIndex);
const commandId = this.executeFunctions.getNodeParameter("commandId", itemIndex);
const options = this.executeFunctions.getNodeParameter("options", itemIndex, {});
const requestData = {
BOT_ID: botId,
COMMAND_ID: commandId,
};
// Add optional parameters
if (options.command) {
requestData.COMMAND = options.command;
}
if (options.commandHandler) {
requestData.EVENT_COMMAND_ADD = options.commandHandler;
}
if (options.commandDescription) {
requestData.COMMAND_PARAMS = options.commandDescription;
}
if (options.isCommon !== undefined) {
requestData.COMMON = options.isCommon === true ? "Y" : "N";
}
if (options.isHidden !== undefined) {
requestData.HIDDEN = options.isHidden === true ? "Y" : "N";
}
if (options.allowExtranet !== undefined) {
requestData.EXTRANET_SUPPORT = options.allowExtranet === true ? "Y" : "N";
}
const response = await this.makeApiCall(this.resourceEndpoints.botCommand.updateCommand, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
async handleAnswerCommand(itemIndex) {
const botId = this.executeFunctions.getNodeParameter("botId", itemIndex);
const commandId = this.executeFunctions.getNodeParameter("commandId", itemIndex);
const messageText = this.executeFunctions.getNodeParameter("messageText", itemIndex);
const options = this.executeFunctions.getNodeParameter("options", itemIndex, {});
const requestData = {
BOT_ID: botId,
COMMAND_ID: commandId,
MESSAGE: messageText,
};
// Add optional parameters
if (options.messageType) {
requestData.MESSAGE_TYPE = options.messageType;
}
if (options.attachments) {
requestData.ATTACH = options.attachments;
}
if (options.keyboardButtons) {
requestData.KEYBOARD = options.keyboardButtons;
}
const response = await this.makeApiCall(this.resourceEndpoints.botCommand.answerCommand, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
/* Bot Message Operations */
async handleAddMessage(itemIndex) {
const botId = this.executeFunctions.getNodeParameter("botId", itemIndex);
const dialogId = this.executeFunctions.getNodeParameter("dialogId", itemIndex);
const messageText = this.executeFunctions.getNodeParameter("messageText", itemIndex);
const options = this.executeFunctions.getNodeParameter("options", itemIndex, {});
const requestData = {
BOT_ID: botId,
DIALOG_ID: dialogId,
MESSAGE: messageText,
};
// Add optional parameters
if (options.messageType) {
requestData.SYSTEM = options.messageType === "system" ? "Y" : "N";
}
if (options.attachments) {
requestData.ATTACH = options.attachments;
}
if (options.keyboardButtons) {
requestData.KEYBOARD = options.keyboardButtons;
}
if (options.urlPreview !== undefined) {
requestData.URL_PREVIEW = options.urlPreview === true ? "Y" : "N";
}
const response = await this.makeApiCall(this.resourceEndpoints.botMessage.addMessage, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
async handleUpdateMessage(itemIndex) {
const botId = this.executeFunctions.getNodeParameter("botId", itemIndex);
const messageId = this.executeFunctions.getNodeParameter("messageId", itemIndex);
const messageText = this.executeFunctions.getNodeParameter("messageText", itemIndex);
const options = this.executeFunctions.getNodeParameter("options", itemIndex, {});
const requestData = {
BOT_ID: botId,
MESSAGE_ID: messageId,
MESSAGE: messageText,
};
// Add optional parameters
if (options.attachments) {
requestData.ATTACH = options.attachments;
}
if (options.keyboardButtons) {
requestData.KEYBOARD = options.keyboardButtons;
}
if (options.urlPreview !== undefined) {
requestData.URL_PREVIEW = options.urlPreview === true ? "Y" : "N";
}
const response = await this.makeApiCall(this.resourceEndpoints.botMessage.updateMessage, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
async handleDeleteMessage(itemIndex) {
const botId = this.executeFunctions.getNodeParameter("botId", itemIndex);
const messageId = this.executeFunctions.getNodeParameter("messageId", itemIndex);
const requestData = {
BOT_ID: botId,
MESSAGE_ID: messageId,
};
const response = await this.makeApiCall(this.resourceEndpoints.botMessage.deleteMessage, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
async handleSendTyping(itemIndex) {
const botId = this.executeFunctions.getNodeParameter("botId", itemIndex);
const dialogId = this.executeFunctions.getNodeParameter("dialogId", itemIndex);
const requestData = {
BOT_ID: botId,
DIALOG_ID: dialogId,
};
const response = await this.makeApiCall(this.resourceEndpoints.botMessage.sendTyping, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
/* Bot Management Operations */
async handleRegisterBot(itemIndex) {
const botType = this.executeFunctions.getNodeParameter("botType", itemIndex);
const botName = this.executeFunctions.getNodeParameter("botName", itemIndex);
const botCode = this.executeFunctions.getNodeParameter("botCode", itemIndex);
const eventHandler = this.executeFunctions.getNodeParameter("eventHandler", itemIndex);
const options = this.executeFunctions.getNodeParameter("options", itemIndex, {});
const requestData = {
TYPE: botType,
CODE: botCode,
EVENT_HANDLER: eventHandler,
PROPERTIES: {
NAME: botName,
},
};
// Add optional parameters
if (options.openLineId) {
requestData.OPENLINE = options.openLineId;
}
if (options.clientId) {
requestData.CLIENT_ID = options.clientId;
}
if (options.languageId) {
requestData.LANG = options.languageId;
}
if (options.botDescription) {
requestData.PROPERTIES.DESCRIPTION =
options.botDescription;
}
if (options.botAvatar) {
requestData.PROPERTIES.AVATAR = options.botAvatar;
}
const response = await this.makeApiCall(this.resourceEndpoints.botManagement.registerBot, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
async handleUnregisterBot(itemIndex) {
const botId = this.executeFunctions.getNodeParameter("botId", itemIndex);
const requestData = {
BOT_ID: botId,
};
const response = await this.makeApiCall(this.resourceEndpoints.botManagement.unregisterBot, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
async handleUpdateBot(itemIndex) {
const botId = this.executeFunctions.getNodeParameter("botId", itemIndex);
const options = this.executeFunctions.getNodeParameter("options", itemIndex, {});
const requestData = {
BOT_ID: botId,
};
// Add optional parameters
if (options.botName) {
requestData.NAME = options.botName;
}
if (options.botDescription) {
requestData.PROPERTIES = {
DESCRIPTION: options.botDescription,
};
}
if (options.botAvatar) {
requestData.PROPERTIES = {
...requestData.PROPERTIES,
AVATAR: options.botAvatar,
};
}
const response = await this.makeApiCall(this.resourceEndpoints.botManagement.updateBot, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
/* Bot Chat Operations */
async handleChatGet(itemIndex) {
const botId = this.executeFunctions.getNodeParameter("botId", itemIndex);
const chatId = this.executeFunctions.getNodeParameter("chatId", itemIndex);
const requestData = {
BOT_ID: botId,
CHAT_ID: chatId,
};
const response = await this.makeApiCall(this.resourceEndpoints.botChat.chatGet, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
async handleChatLeave(itemIndex) {
const botId = this.executeFunctions.getNodeParameter("botId", itemIndex);
const chatId = this.executeFunctions.getNodeParameter("chatId", itemIndex);
const requestData = {
BOT_ID: botId,
CHAT_ID: chatId,
};
const response = await this.makeApiCall(this.resourceEndpoints.botChat.chatLeave, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
async handleChatSetOwner(itemIndex) {
const botId = this.executeFunctions.getNodeParameter("botId", itemIndex);
const chatId = this.executeFunctions.getNodeParameter("chatId", itemIndex);
const userId = this.executeFunctions.getNodeParameter("userId", itemIndex);
const requestData = {
BOT_ID: botId,
CHAT_ID: chatId,
USER_ID: userId,
};
const response = await this.makeApiCall(this.resourceEndpoints.botChat.chatSetOwner, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
async handleChatUpdateAvatar(itemIndex) {
const botId = this.executeFunctions.getNodeParameter("botId", itemIndex);
const chatId = this.executeFunctions.getNodeParameter("chatId", itemIndex);
const fileId = this.executeFunctions.getNodeParameter("fileId", itemIndex);
const requestData = {
BOT_ID: botId,
CHAT_ID: chatId,
FILE_ID: fileId,
};
const response = await this.makeApiCall(this.resourceEndpoints.botChat.chatUpdateAvatar, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
async handleChatUpdateColor(itemIndex) {
const botId = this.executeFunctions.getNodeParameter("botId", itemIndex);
const chatId = this.executeFunctions.getNodeParameter("chatId", itemIndex);
const color = this.executeFunctions.getNodeParameter("color", itemIndex);
const requestData = {
BOT_ID: botId,
CHAT_ID: chatId,
COLOR: color,
};
const response = await this.makeApiCall(this.resourceEndpoints.botChat.chatUpdateColor, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
async handleChatUpdateTitle(itemIndex) {
const botId = this.executeFunctions.getNodeParameter("botId", itemIndex);
const chatId = this.executeFunctions.getNodeParameter("chatId", itemIndex);
const title = this.executeFunctions.getNodeParameter("title", itemIndex);
const requestData = {
BOT_ID: botId,
CHAT_ID: chatId,
TITLE: title,
};
const response = await this.makeApiCall(this.resourceEndpoints.botChat.chatUpdateTitle, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
async handleChatUserAdd(itemIndex) {
const botId = this.executeFunctions.getNodeParameter("botId", itemIndex);
const chatId = this.executeFunctions.getNodeParameter("chatId", itemIndex);
const userId = this.executeFunctions.getNodeParameter("userId", itemIndex);
const requestData = {
BOT_ID: botId,
CHAT_ID: chatId,
USER_ID: userId,
};
const response = await this.makeApiCall(this.resourceEndpoints.botChat.chatUserAdd, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
async handleChatUserList(itemIndex) {
const botId = this.executeFunctions.getNodeParameter("botId", itemIndex);
const chatId = this.executeFunctions.getNodeParameter("chatId", itemIndex);
const requestData = {
BOT_ID: botId,
CHAT_ID: chatId,
};
const response = await this.makeApiCall(this.resourceEndpoints.botChat.chatUserList, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
async handleChatUserDelete(itemIndex) {
const botId = this.executeFunctions.getNodeParameter("botId", itemIndex);
const chatId = this.executeFunctions.getNodeParameter("chatId", itemIndex);
const userId = this.executeFunctions.getNodeParameter("userId", itemIndex);
const requestData = {
BOT_ID: botId,
CHAT_ID: chatId,
USER_ID: userId,
};
const response = await this.makeApiCall(this.resourceEndpoints.botChat.chatUserDelete, requestData, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
}
}
exports.ChatbotResourceHandler = ChatbotResourceHandler;
//# sourceMappingURL=ChatbotResourceHandler.js.map