UNPKG

notification-service-sdk

Version:

A Node.js notification service SDK supporting email and SMS providers

65 lines 2.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VolcSmsProvider = void 0; const sms_1 = require("@volcengine/openapi/lib/services/sms"); const interfaces_1 = require("../interfaces"); class VolcSmsProvider { constructor(config) { if (!config.accessKeyId || !config.accessKeySecret || !config.signName) { throw new interfaces_1.NotificationError('Invalid Volc SMS configuration', 'INVALID_CONFIG'); } this.config = config; this.service = new sms_1.SmsService({ accessKeyId: config.accessKeyId, secretKey: config.accessKeySecret, region: config.region || 'cn-north-1', serviceName: 'volcSMS', }); } async send(message) { var _a; try { if (!message.phoneNumbers) { throw new interfaces_1.NotificationError('Phone numbers are required', 'MISSING_PHONE_NUMBERS'); } if (!message.templateId) { throw new interfaces_1.NotificationError('Template ID is required', 'MISSING_TEMPLATE_ID'); } // 处理电话号码格式,PhoneNumbers 需为 string const phoneNumbers = Array.isArray(message.phoneNumbers) ? message.phoneNumbers.join(',') : message.phoneNumbers; const params = { SmsAccount: this.config.signName, Sign: this.config.signName, TemplateID: message.templateId, TemplateParam: JSON.stringify(message.templateParams || {}), PhoneNumbers: phoneNumbers, Tag: message.tag || '', // 可根据需要传递 UserExtCode: '', // 可根据需要传递 }; // 调用 Send 方法 const response = await this.service.Send(params); const data = (response === null || response === void 0 ? void 0 : response.Result) || {}; const error = (_a = response === null || response === void 0 ? void 0 : response.ResponseMetadata) === null || _a === void 0 ? void 0 : _a.Error; if (error && error.Code) { return { success: false, error: new interfaces_1.NotificationError(`Failed to send SMS: ${error.Message}`, error.Code), }; } return { success: true, messageId: (data && Array.isArray(data.MessageID)) ? data.MessageID[0] : undefined, }; } catch (error) { return { success: false, error: error instanceof Error ? error : new Error(String(error)), }; } } } exports.VolcSmsProvider = VolcSmsProvider; //# sourceMappingURL=volcSms.js.map