A short message module for Nest framework (node.js) using Alicloud SMS service.
English | 简体中文
- Create Alicloud account
- Purchase SMS. (read more)
- Apply for SMS sign name and message template, go to Dashboard > Short Message Service > Domestic Message > Add Singname
- Create message template, go to Dashboard > Short Message Service > Domestic Message > Templates Management > Add Template.
npm install --save nestjs-alicloud-sms
#or
yarn add nestjs-alicloud-sms
- 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 {}
- 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
}
}
}
The MIT License.