n8n-nodes-feishu-lark
Version:
n8n custom nodes for n8n to interact with Feishu/Lark, including Lark Bot, Lark MCP, and Lark Trigger.
344 lines • 14.9 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Lark = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const ResourceFactory_1 = __importDefault(require("../help/builder/ResourceFactory"));
const utils_1 = require("../help/utils");
const GenericFunctions_1 = require("./GenericFunctions");
const RequestUtils_1 = __importDefault(require("../help/utils/RequestUtils"));
const resourceBuilder = ResourceFactory_1.default.build(__dirname);
class Lark {
constructor() {
this.description = {
displayName: 'Lark',
name: 'lark',
icon: 'file:lark_icon.svg',
group: ['input'],
version: [1],
defaultVersion: 1,
description: 'Consume Lark API',
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
defaults: {
name: 'Lark',
},
usableAsTool: true,
inputs: ["main"],
outputs: `={{(${utils_1.configuredOutputs})($parameter)}}`,
credentials: [
{
name: "larkApi",
required: true,
displayOptions: {
show: {
authentication: ["larkApi"],
},
},
},
{
name: "larkOAuth2Api",
required: true,
displayOptions: {
show: {
authentication: ["larkOAuth2Api"],
},
},
},
],
webhooks: [
{
name: 'default',
httpMethod: 'GET',
responseMode: 'onReceived',
responseData: '',
path: '={{ $nodeId }}',
restartWebhook: true,
isFullPath: true,
},
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
responseData: '',
path: '={{ $nodeId }}',
restartWebhook: true,
isFullPath: true,
},
],
properties: resourceBuilder.build(),
};
this.methods = {
listSearch: {
async searchBitables() {
const bitables = await GenericFunctions_1.getFileList.call(this, [
"bitable",
]);
return {
results: bitables.map((bitable) => ({
name: bitable.name,
value: bitable.token,
url: bitable.url,
})),
};
},
async searchFolders() {
const folders = await GenericFunctions_1.getFileList.call(this, [
"folder",
]);
return {
results: folders.map((folder) => ({
name: folder.name,
value: folder.token,
url: folder.url,
})),
};
},
async searchTables() {
const appToken = this.getNodeParameter('app_token', undefined, {
extractValue: true,
});
const tables = await GenericFunctions_1.larkApiRequestTableList.call(this, appToken);
return {
results: tables.map((table) => ({
name: table.name,
value: table.table_id,
})),
};
},
async searchTableViews() {
const app_token = this.getNodeParameter('app_token', undefined, {
extractValue: true,
});
const table_id = this.getNodeParameter('table_id', undefined, {
extractValue: true,
});
const user_id_type = this.getNodeParameter('user_id_type', 'open_id');
const views = await GenericFunctions_1.larkApiRequestTableViewList.call(this, {
app_token,
table_id,
user_id_type,
});
return {
results: views.map((view) => ({
name: view.view_name,
value: view.view_id,
})),
};
},
async searchTableFields() {
const app_token = this.getNodeParameter('app_token', undefined, {
extractValue: true,
});
const table_id = this.getNodeParameter('table_id', undefined, {
extractValue: true,
});
const fields = await GenericFunctions_1.larkApiRequestTableFieldList.call(this, {
app_token,
table_id,
});
return {
results: fields.map((field) => ({
name: field.field_name,
value: field.field_id,
})),
};
},
async searchBaseRoles() {
const app_token = this.getNodeParameter('app_token', undefined, {
extractValue: true,
});
const roles = await GenericFunctions_1.larkApiRequestBaseRoleList.call(this, {
app_token,
});
return {
results: roles.map((role) => ({
name: role.role_name,
value: role.role_id,
})),
};
},
async searchUserIds(query) {
var _a;
if (!query) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Query required for search');
}
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const body = emailRegex.test(query) ? { emails: [query] } : { mobiles: [query] };
const results = [];
const userIdTypes = ['open_id', 'user_id', 'union_id'];
for (const userIdType of userIdTypes) {
const { data: { user_list: users }, } = await RequestUtils_1.default.request.call(this, {
method: 'POST',
url: '/open-apis/contact/v3/users/batch_get_id',
qs: {
user_id_type: userIdType,
},
body,
});
if (!users[0].user_id) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `No user found for: ${query}`);
}
results.push({
name: userIdType,
value: ((_a = users[0]) === null || _a === void 0 ? void 0 : _a.user_id) || '',
});
}
return {
results,
};
},
async searchDocuments() {
const allDocs = [];
const docxList = await GenericFunctions_1.getFileList.call(this, [
"docx",
"doc",
]);
allDocs.push(...docxList);
return {
results: allDocs.map((doc) => ({
name: doc.name,
value: doc.token,
url: doc.url,
})),
};
},
async searchSpreadsheets() {
const spreadsheets = await GenericFunctions_1.getFileList.call(this, [
"sheet",
]);
return {
results: spreadsheets.map((spreadsheet) => ({
name: spreadsheet.name,
value: spreadsheet.token,
url: spreadsheet.url,
})),
};
},
async searchFiles() {
const fileType = this.getNodeParameter('space_file_type', undefined);
if (!fileType) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'File type is required');
}
const files = await GenericFunctions_1.getFileList.call(this, fileType);
return {
results: files.map((file) => ({
name: file.name,
value: file.token,
url: file.url,
})),
};
},
async searchCalendars() {
const calendars = await GenericFunctions_1.larkApiRequestCalendarList.call(this);
return {
results: calendars.map((calendar) => ({
name: calendar.summary,
value: calendar.calendar_id,
})),
};
},
async searchCalendarEvents(query) {
if (!query) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Query required for search');
}
const calendarId = this.getNodeParameter('calendar_id', undefined, {
extractValue: true,
});
if (!calendarId) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Calendar ID required for search');
}
const user_id_type = this.getNodeParameter('user_id_type', 'open_id');
const events = await GenericFunctions_1.larkApiRequestCalendarEventList.call(this, {
calendarId,
query,
user_id_type,
});
return {
results: events.map((event) => ({
name: event.summary,
value: event.event_id,
url: event.app_link,
})),
};
},
async searchSheets() {
const spreadsheetId = this.getNodeParameter('spreadsheet_id', undefined, {
extractValue: true,
});
if (!spreadsheetId) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Spreadsheet ID required for search');
}
const sheets = await GenericFunctions_1.larkApiRequestSheetList.call(this, spreadsheetId);
return {
results: sheets.map((sheet) => ({
name: sheet.title,
value: sheet.sheet_id,
})),
};
},
},
};
this.webhook = GenericFunctions_1.sendAndWaitWebhook;
}
async execute() {
var _a;
const items = this.getInputData();
let returnData = Array.from({ length: 1 }, () => []);
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
const callFunc = resourceBuilder.getCall(resource, operation);
if (!callFunc) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No resources and operatons find: ' + resource + '.' + operation);
}
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
try {
this.logger.debug('call function', {
resource,
operation,
itemIndex,
});
const responseData = await callFunc.call(this, itemIndex);
const { outputType } = responseData;
if (!outputType) {
const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item: itemIndex } });
returnData[0].push(...executionData);
}
else if (outputType === "multiple") {
const { outputData } = responseData;
returnData = outputData;
}
else {
return [];
}
}
catch (error) {
this.logger.error('call function error', {
resource,
operation,
itemIndex,
errorMessage: error.message,
stack: error.stack,
});
if (this.continueOnFail()) {
const executionErrorData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ error: (_a = error.description) !== null && _a !== void 0 ? _a : error.message }), { itemData: { item: itemIndex } });
returnData[0].push(...executionErrorData);
continue;
}
else if (error.name === 'NodeApiError') {
throw error;
}
else {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, {
message: error.message,
itemIndex,
});
}
}
}
return returnData;
}
}
exports.Lark = Lark;
//# sourceMappingURL=Lark.node.js.map