send-sms.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (C) 2020 Tencent Cloud.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. 'use strict';
  17. const { request } = require('./utils');
  18. const { appId, appSign } = require('./config');
  19. /**
  20. * 发送短信
  21. * @param {object} params - 参数包装对象
  22. * @param {string[]} params.phoneNumbers - 手机号列表
  23. * @param {string} params.templateId - 短信模板ID
  24. * @param {string[]} params.templateParams - 短信模板参数列表
  25. * @return {Promise<object>} 短信发送状态信息,详见文档 https://cloud.tencent.com/document/api/382/38778
  26. */
  27. async function sendSMS({ phoneNumbers, templateId, templateParams }) {
  28. // 配置校验
  29. if (!appId || !appSign) {
  30. throw new Error('请在云函数SMS模块中配置appId和appSign');
  31. }
  32. // 调用腾讯云发送SMS接口
  33. const result = await request('SendSms', {
  34. SmsSdkAppid: appId,
  35. Sign: appSign,
  36. PhoneNumberSet: phoneNumbers,
  37. TemplateID: templateId,
  38. TemplateParamSet: templateParams
  39. });
  40. return result;
  41. }
  42. module.exports = sendSMS;