UNPKG

n8n-nodes-dingtalk

Version:

[![NPM badge](https://img.shields.io/npm/v/n8n-nodes-dingtalk.svg)](https://www.npmjs.com/package/n8n-nodes-dingtalk)

296 lines 12 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DingTalk = void 0; const n8n_workflow_1 = require("n8n-workflow"); const crypto_1 = __importDefault(require("crypto")); const axios_1 = __importDefault(require("axios")); const client_1 = __importStar(require("@alicloud/dingtalk/dist/robot_1_0/client")), $RobotClient = client_1; const OpenApi = __importStar(require("@alicloud/openapi-client")); const Util = __importStar(require("@alicloud/tea-util")); class DingTalk { constructor() { this.description = { displayName: 'DingDing', name: 'DingTalk', icon: 'file:dingtalk.svg', group: ['input'], version: 1, subtitle: '={{$parameter["type"]}}', description: 'DingDing API', defaults: { name: 'Ding Ding', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'dingtalkApi', required: true, displayOptions: { show: { type: ['dingtalk'], }, }, }, { name: 'dingTalkCustomRobotApi', required: true, displayOptions: { show: { type: ['customRobot'], }, }, }, ], properties: [ { displayName: 'Type', name: 'type', type: 'options', required: true, noDataExpression: true, options: [ { name: '自定义机器人', value: 'customRobot', description: '自定义机器人存在限流, 每分钟20次', action: '自定义机器人', }, { name: 'Webhook', value: 'webhook', action: 'Webhook', }, ], default: 'customRobot', }, { displayName: '消息类型', name: 'msgtype', type: 'options', required: true, noDataExpression: true, options: [ { name: 'ActionCard类型', value: 'actionCard', }, { name: 'FeedCard类型', value: 'feedCard', }, { name: 'Link类型', value: 'link', }, { name: 'Markdown类型', value: 'markdown', }, { name: 'Text类型', value: 'text', }, ], default: 'text', }, { displayName: '被@人的手机号', name: 'atMobiles', type: 'string', default: '', displayOptions: { show: { type: ['customRobot'], msgtype: ['text'], }, }, }, { displayName: '被@人的用户Userid', name: 'atUserIds', type: 'string', default: '', displayOptions: { show: { type: ['customRobot'], msgtype: ['text'], }, }, }, { displayName: '是否@所有人', name: 'isAtAll', type: 'boolean', default: false, placeholder: '', displayOptions: { show: { type: ['customRobot'], msgtype: ['text'], }, }, }, { displayName: '消息内容', name: 'content', type: 'json', default: '', required: true, placeholder: '', displayOptions: { show: { type: ['customRobot'], }, }, }, ], }; } async execute() { const type = this.getNodeParameter('type', 0); if (type === 'customRobot') { const credentials = await this.getCredentials('dingTalkCustomRobot'); const timestamp = Date.parse(new Date().toString()); const stringToSign = `${timestamp}\n${credentials.webhookSign}`; const signBase64 = crypto_1.default .createHmac('sha256', credentials.webhookSign) .update(stringToSign) .digest('base64'); const sign = encodeURIComponent(signBase64); const url = credentials.webhookSign ? `${credentials.webhookUrl}&timestamp=${timestamp}&sign=${sign}` : credentials.webhookUrl; const result = []; const items = this.getInputData(); let item; for (let itemIndex = 0; itemIndex < items.length; itemIndex++) { try { item = items[itemIndex]; const msgtype = this.getNodeParameter('msgtype', itemIndex); const content = this.getNodeParameter('content', itemIndex); const data = { msgtype }; if ('text' === msgtype || 'markdown' === msgtype) { const atMobiles = this.getNodeParameter('atMobiles', itemIndex); const atUserIds = this.getNodeParameter('atUserIds', itemIndex); const isAtAll = this.getNodeParameter('isAtAll', itemIndex); data.at = { isAtAll }; if (atMobiles && atMobiles.length > 0) { data.at.atMobiles = atMobiles; } if (atUserIds && atUserIds.length > 0) { data.at.atUserIds = atUserIds; } if ('text' === msgtype) { data.text = { content }; } else if ('markdown' === msgtype) { data.link = content; } } else if ('link' === msgtype) { data.link = content; } else if ('actionCard' === msgtype) { data.actionCard = content; } else if ('feedCard' === msgtype) { data.feedCard = content; } const res = await axios_1.default.post(url, data, { headers: { 'Content-Type': 'application/json', }, }); result.push({ json: res.data }); } catch (error) { if (this.continueOnFail()) { result.push({ json: this.getInputData(itemIndex)[0].json, error, pairedItem: itemIndex, }); } else { if (error.context) { error.context.itemIndex = itemIndex; throw error; } throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex, }); } } } return this.prepareOutputData(result); } const rbClient = new client_1.default(new OpenApi.Config({ endpoint: 'your endpoint', accessKeyId: 'your access key id', accessKeySecret: 'your access key secret', type: 'access_key', regionId: 'cn-hangzhou', })); const runtimeObject = new Util.RuntimeOptions({}); const items = this.getInputData(); let item; for (let itemIndex = 0; itemIndex < items.length; itemIndex++) { try { item = items[itemIndex]; const req = new $RobotClient.SendRobotDingMessageRequest(); rbClient.sendRobotDingMessage(req); const binaryPropertyName = this.getNodeParameter('binaryPropertyName', itemIndex); if (item.binary === undefined || item.binary[binaryPropertyName] === undefined) { continue; } const binaryPropertyOutputName = this.getNodeParameter('binaryPropertyOutputName', itemIndex, binaryPropertyName); const inputEncoding = this.getNodeParameter('inputEncoding', itemIndex); const outputEncoding = this.getNodeParameter('outputEncoding', itemIndex); } catch (error) { if (this.continueOnFail()) { items.push({ json: this.getInputData(itemIndex)[0].json, error, pairedItem: itemIndex }); } else { if (error.context) { error.context.itemIndex = itemIndex; throw error; } throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex, }); } } } return this.prepareOutputData(items); } } exports.DingTalk = DingTalk; //# sourceMappingURL=DingTalk.node.js.map