Skip to content

YuchenWell/nestjs-alicloud-sms

Repository files navigation

Nest Logo

A short message module for Nest framework (node.js) using Alicloud SMS service.

NPM Version Package License

English | 简体中文

Before Installation

  1. Create Alicloud account
  2. Purchase SMS. (read more)
  3. Apply for SMS sign name and message template, go to Dashboard > Short Message Service > Domestic Message > Add Singname
  4. Create message template, go to Dashboard > Short Message Service > Domestic Message > Templates Management > Add Template.

Installation

npm install --save nestjs-alicloud-sms
#or
yarn add nestjs-alicloud-sms

Documentation

  1. Import the AlicloudSmsModule with the following configuration:
import { Module } from '@nestjs/common';
import { AlicloudSmsModule } from 'nestjs-alicloud-sms';

import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppController],
  providers: [AppService],
  imports: [
    AlicloudSmsModule.forRoot({
      config: { accessKeyId: '***', accessKeySecret: '***' },
      defaults: { signName: '***', regionId: 'cn-hangzhou' },
    }),
  ],
})
export class AppModule {}
  1. Send Message
import { Controller, Logger, Post } from '@nestjs/common';
import { AlicloudSmsService } from 'nestjs-alicloud-sms';

@Controller()
export class AppController {
  constructor(private readonly smsService: AlicloudSmsService) {}

  @Post('sms/send')
  async sendSms(): Promise<any> {
    const templateCodeId = 'SMS_1490999999';
    const phoneNumber = '1388886666';
    const templateParam = { code: '888666' };

    const sendResponse = await this.smsService.sendSms(templateCodeId, phoneNumber, templateParam);

    if (sendResponse.Code === 'OK') {
      // success
    } else {
      // failed
    }
  }
}

License

The MIT License.