UNPKG

node-red-contrib-feishu

Version:

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

128 lines (118 loc) 4.69 kB
/** * 腾讯云短信节点模块 * 该模块实现了一个Node-RED节点,用于发送腾讯云短信 */ /** * 腾讯云短信节点模块 * 该模块实现了Node-RED节点,用于发送腾讯云短信服务的消息 * * @module node-red-contrib-feishu/tencent-sms * @requires tencentcloud-sdk-nodejs */ module.exports = function(RED) { /** * 腾讯云短信节点构造函数 * @class TencentSMSNode * @param {Object} config - 节点配置对象 * @param {string} config.secretId - 腾讯云访问密钥ID * @param {string} config.secretKey - 腾讯云访问密钥密码 * @param {string} config.region - 腾讯云区域 * @param {string} config.sdkAppId - 短信应用ID * @param {string} config.signName - 短信签名 * @param {string} [config.templateId] - 默认短信模板ID */ function TencentSMSNode(config) { RED.nodes.createNode(this, config); const node = this; const tencentcloud = require('tencentcloud-sdk-nodejs'); // 创建SMS客户端 const SmsClient = tencentcloud.sms.v20210111.Client; const clientConfig = { credential: { secretId: config.secretId, secretKey: config.secretKey }, region: config.region || 'ap-nanjing', profile: { signMethod: "HmacSHA256", httpProfile: { reqMethod: "POST", reqTimeout: 30, endpoint: "sms.tencentcloudapi.com" } } }; // 创建短信客户端实例 const client = new SmsClient(clientConfig); node.on('input', async function(msg) { try { // 验证必要参数 if (!msg.phoneNumber) { throw new Error('手机号码是必需的'); } if (!msg.templateParams) { throw new Error('模板参数是必需的'); } // 构建请求参数 const params = { SmsSdkAppId: config.sdkAppId, SignName: config.signName, ExtendCode: '', SenderId: '', SessionContext: msg.sessionContext || '', PhoneNumberSet: Array.isArray(msg.phoneNumber) ? msg.phoneNumber.map(phone => phone.startsWith('+') ? phone : '+86' + phone) : [msg.phoneNumber.startsWith('+') ? msg.phoneNumber : '+86' + msg.phoneNumber], TemplateId: msg.templateId || config.templateId, TemplateParamSet: Array.isArray(msg.templateParams) ? msg.templateParams : msg.templateParams ? Object.values(msg.templateParams) : [] }; // 发送短信 const result = await client.SendSms(params); // 检查发送结果 const success = result.SendStatusSet && result.SendStatusSet[0] && result.SendStatusSet[0].Code === 'Ok'; if (success) { // 发送成功 node.status({fill: "green", shape: "dot", text: "发送成功"}); msg.payload = result; node.send(msg); } else { // 发送失败 throw new Error(result.SendStatusSet[0].Message || '发送失败'); } } catch (error) { // 发送失败 node.status({fill: "red", shape: "ring", text: "发送失败"}); node.error(error.message, msg); } }); // 节点关闭时清除状态 node.on('close', function() { node.status({}); }); } // 注册腾讯云短信节点 RED.nodes.registerType('tencent-sms', TencentSMSNode, { defaults: { name: {value: ""}, secretId: {value: "", required: true}, secretKey: {value: "", required: true}, region: {value: "ap-guangzhou", required: true}, sdkAppId: {value: "", required: true}, signName: {value: "", required: true}, templateId: {value: ""} }, category: "social", color: "#3EBAEE", inputs: 1, outputs: 1, icon: "font-awesome/fa-comments", label: function() { return this.name || "腾讯云短信"; }, paletteLabel: "腾讯云短信" }); };