UNPKG

node-red-contrib-feishu

Version:

Node-RED节点用于发送飞书消息

106 lines (94 loc) 3.65 kB
/** * 阿里云短信节点模块 * 该模块实现了一个Node-RED节点,用于发送阿里云短信 */ /** * 阿里云短信节点模块 * 该模块实现了Node-RED节点,用于发送阿里云短信服务的消息 * 包含配置节点和功能节点两部分 * * @module node-red-contrib-feishu/aliyun-sms * @requires @alicloud/pop-core */ module.exports = function(RED) { /** * 阿里云SMS配置节点构造函数 * @class AliyunSMSConfigNode * @param {Object} config - 节点配置对象 * @param {string} config.accessKeyId - 阿里云访问密钥ID * @param {string} config.accessKeySecret - 阿里云访问密钥密码 * @param {string} config.endpoint - 阿里云SMS服务接入点 */ function AliyunSMSConfigNode(config) { RED.nodes.createNode(this, config); this.accessKeyId = config.accessKeyId; this.accessKeySecret = config.accessKeySecret; this.endpoint = config.endpoint; } // 注册阿里云SMS配置节点 RED.nodes.registerType("aliyun-sms-config", AliyunSMSConfigNode); /** * 阿里云短信节点构造函数 * @param {Object} config - 节点配置对象 */ function AliyunSMSNode(config) { RED.nodes.createNode(this, config); const node = this; const Core = require('@alicloud/pop-core'); // 获取阿里云配置 const smsConfig = RED.nodes.getNode(config.smsConfig); // 创建阿里云客户端 const client = new Core({ accessKeyId: smsConfig.accessKeyId, accessKeySecret: smsConfig.accessKeySecret, endpoint: smsConfig.endpoint, apiVersion: '2017-05-25' }); node.on('input', async function(msg) { try { // 验证必要参数 if (!msg.phoneNumber) { throw new Error('手机号码是必需的'); } if (!msg.templateParam) { throw new Error('模板参数是必需的'); } // 构建请求参数 const params = { RegionId: "cn-hangzhou", PhoneNumbers: Array.isArray(msg.phoneNumber) ? msg.phoneNumber.join(',') : msg.phoneNumber, SignName: config.signName, TemplateCode: config.templateCode, TemplateParam: typeof msg.templateParam === 'string' ? msg.templateParam : JSON.stringify(msg.templateParam) }; // 添加可选参数 if (msg.smsUpExtendCode) { params.SmsUpExtendCode = msg.smsUpExtendCode; } if (msg.outId) { params.OutId = msg.outId; } // 发送短信 const result = await client.request('SendSms', params, { method: 'POST' }); // 发送成功 node.status({fill: "green", shape: "dot", text: "发送成功"}); msg.payload = result; node.send(msg); } catch (error) { // 发送失败 node.status({fill: "red", shape: "ring", text: "发送失败"}); node.error(error.message, msg); } }); // 节点关闭时清除状态 node.on('close', function() { node.status({}); }); } // 注册阿里云短信节点 RED.nodes.registerType('aliyun-sms', AliyunSMSNode); };