0.1.1 • Published 5 years ago

@axolo/egg-tencent-sms v0.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

egg-tencent-sms

Tencent Cloud SMS for Egg.js.

Install

npm i @axolo/egg-tencent-sms --save

Usage

// {app_root}/config/plugin.js
exports.tencentSms = {
  enable: true,
  package: '@axolo/egg-tencent-sms',
};

Configuration

see config/config.default.js for more detail.

// {app_root}/config/config.default.js
exports.tencentSms = {
  SecretId: 'SMS_SECRETID',
  SecretKey: 'SMS_SECRETKEY',
  SendSms: { // Tencent Cloud SDK Action name, cover transfer params
    SmsSdkAppid: 'SMS_SDK_APPID',
    Sign: 'SMS_SIGN',
    TemplateID: 'SMS_TEMPLATEID',
  },
};

API

exec(model, action, params)

params

paramstypedescription
modelstringmodel of Tencent Cloud SMS
actionstringaction of Tencent Cloud SMS
paramsobjectparams of Tencent Cloud SMS action

return

Promise of Tencent Cloud SMS response.

send(params)

alias this.exec('SendSmsRequest', 'SendSms', params).

Example

Send captcha to single phone number.

'use strict';

const Controller = require('egg').Controller;

class SmsController extends Controller {
  async create() {
    const { phone } = this.ctx.reqeust.body; // E.164 format
    const rand = parseInt(Math.random()*10000);
    const code = rand > 1000 ? rand : rand + 1000;
    const sms = this.app.tencentSms;
    const send = await sms.send({
      PhoneNumberSet: [ phone ],
      TemplateParamSet: [ code ],
    });
    ctx.body = send;
  }
}

module.exports = SmsController;

Questions & Suggestions

Please open an issue here.

License

MIT