nestjs-tencent-sms

A nestjs Tencent Sms Service

Usage no npm install needed!

<script type="module">
  import nestjsTencentSms from 'https://cdn.skypack.dev/nestjs-tencent-sms';
</script>

README

Nestjs 腾讯云短信发送服务

安装

Yarn

yarn add nestjs-tencent-sms

NPM

npm install nestjs-tencent-sms --save

使用方式

import { Module } from '@nestjs/common'
import { TencentSmsModule } from 'nestjs-tencent-sms'

@Module({
    imports: [
        TencentSmsModule.forRoot(options)
    ],
})
export class AppModule {}

或者异步

import { Module } from '@nestjs/common';
import { TencentSmsModule } from 'nestjs-tencent-sms'

@Module({
    imports: [
        TencentSmsModule.forRootAsync({
            useFactory: (configService: ConfigService) => configService.get('sms'),         // or use async method
            //useFactory: async (configService: ConfigService) => configService.get('sms'),
            inject:[ConfigService]
        }),
    ],
})
export class AppModule {}

配置文件

export default {
  TemplateSign: 'admin...',
  TemplateID: '47...',
  SmsSdkAppid: '140....',
  credential: {
    secretId: 'AKI...',
    secretKey: 'R5S....',
  },
  profile: {
    httpProfile: {
      endpoint: 'sms.tencentcloudapi.com',
    },
  },
}

服务的调用

import { Injectable } from '@nestjs/common';
import { TencentSmsService } from 'nestjs-tencent-sms';

@Injectable()
export class TestService {
  constructor(
    private readonly smsService: TencentSmsService,
  ) { }
  async root(): Promise<boolean> {
    const client = await this.smsService.sendSms('phone', 'new_code', '2');
    // {
    //     "SerialNo": "2019:....",
    //     "PhoneNumber": "+861....",
    //     "Fee": 1,
    //     "SessionContext": "",
    //     "Code": "Ok",
    //     "Message": "send success",
    //     "IsoCode": "CN"
    // }
    return client.Code === 'Ok';
  }
}

配置

interface RedisOptions {
  TemplateSign: string;
  TemplateID: string;
  SmsSdkAppid: string;
  credential: {
    secretId: string;
    secretKey: string;
  };
  profile: {
    httpProfile: {
      endpoint: string;
    };
  };
}

就是这样!