Nodejs sending verification codes to users via the Smart technology API

Source: Internet
Author: User

In the study not to delve into the people will not ask questions, in the cause of the lack of breakthrough in the people will not be innovative.

First of all, in the course of the development of an order page, I wanted to send a verification code to the user through the front-end technology.

So I spent the whole night on the idea of this need and the way to realize it:

1: Verification code data must not exist in the HTML page and JS file, I think this is a security issue, so the verification code data should be generated automatically by the background, then what kind of backend language should I use?

2: In daily life, send SMS must have cost, so we certainly is not possible through one or two code on the free verification code sent to the user's phone, then the verification code data should be sent to the user designated mobile phone number?

Thinking Solution:

1: The backend languages I have come across include: The C#,java of university studies and the familiar Nodejsin front-end work. So as the front-end developers I finally chose to use Nodejs to build the server to send verification code data, so this article is mainly about how to use Nodejs to send the user verification code data. Of course, other backend languages must also have corresponding solutions to send verification codes.

2: Almost one months ago in the development process used to use GPS positioning, so at that time to study Baidu Map API, in view of this experience, I quickly thought that there will be a network of corresponding third party can help me. Through the full support of Niang, I finally query to two API can help send verification code. It is the intelligent technology and cloud communication respectively. Because in the cloud communication only PHP, Java, Python, C # Demo sample, and intelligent technology in addition to support in the cloud communications supported in the four kinds of background language, but also support go and nodejs, so I finally chose the wisdom of technology . But the wisdom of science and technology is a little bad, that is Nodejs's demo sample does not have detailed comments, but by looking at the demo to contact with themselves, I still make it clear, the following detailed introduction of how to send a verification code through the intelligent technology.

Final Learning Results:

The first step, the intellectual technology registration

The official website of Intellectual examination: http://www.zhiyan.net/

Registration Invitation code:273a7g (Activate the invitation code to receive free SMS.) Of course this invitation code is mine, you would like to fill this out by your own decision. )

Step two, create an app app

After logging in, go to Admin Center , click on the left side of app management , then create your app . Create an app and fill it out without too much explanation.

Third step, custom SMS Signature

Also enter the Management Center , click on the left message signature , and then add a signature . (no signature, SMS is not sent successfully.) )

Fourth step, write Nodejs configuration

1. First write the Sms_send module, which will eventually return the object in the module

File name: Sms_send.js

Import request module and Crypto module * Annotations 1

var request = require (' request ');
var crypto = require (' crypto ');

var Zy = {
Config: {},
Set Setconfig (config) {
this.config = config;
},
Sendsms:function (mobile, code, callback) {
var timestamp = Math.Round (new Date (). GetTime ()/1000);
var sign = Crypto.createhash (' MD5 '). Update (This.config.appKey + This.config.token
+ this.config.templateId + Mobile + code + timestamp, ' UTF8 '). Digest ("Hex");
var url = ' https://sms.zhiyan.net/sms/sms/single/' + This.config.appKey + '/' +
This.config.token + '/' + This.config.templateId + '? timestamp= ' + timestamp + ' &sign= ' + sign;
var data = Json.stringify ({
Mobile:mobile,
Param:code,
Extend: "
});
var opt = {
Rejectunauthorized:false,
Url:url,
Method: ' POST ',
Form: {
Data:data
}
};
Request (opt, callback);
}
};
Module.exports = Zy;

2. Execute the file Import sms_send module, call its method and pass the parameters.

File name: Test.js

Import Sms_send Module

var Zy = require (' sms_send.js ');

Configuration

Zy.setconfig = {

AppKey login can be viewed in the Admin center → account Information
AppKey: ' ************************** ',

Taken login can be viewed in Central administration → app management
Token: ' ************ ',

TemplateID This is your SMS signature, after login can be viewed in the Admin center → SMS Signature

TemplateID: ' ******* '

};

/**

/* Send SMS

/*phone user's mobile phone number to receive SMS

Verification code data sent by/*code (how to generate it yourself)

/*callback callback function

*/

Zy.sendsms (

Phone

Code

/**

/* Callback function

/*err error messages, such as network connectivity issues ... )

/*data All data (verification code sent result exists with body)

/*mess Verification Code Send results

/* (the data contains all the return parameters, while the mess contains only the JSON string that the verification code sends.) JSON strings are also converted to JSON values by Json.parse ()

*/

function (err,data,mess) {

if (err) {

Console.log (ERR);

}else{

Console.log (data);

Console.log (mess);

Console.log (Json.parse (data.body). Reason);
Console.log (Json.parse (mess). Reason);

}
}

);

3. Program Run Results

A. Not connected cable times wrong, print full err:

B. Successful function execution, print full data (too much content, just to show it):

C. Successful function execution, print full mess:

Here because I am also registered account, only apply for the signature, so can not be sent successfully, but this return value can be determined, I use the correct.

I'll try again after my SMS signature is approved.

Well, Nodejs through the intelligent testing technology to send the user SMS verification code to chat here.

This article all by my hand, we have doubts or find where I have the wrong also hope to actively put forward, we learn together, progress together.

* Note 1: When importing the request module my Nodejs error, first of all, the report did not find the request module, and then reported other errors, so here to say two more sentences, if your above program runs successfully, then you can not look at this paragraph of the note.

A. Update Nodejs. Download the installation from the https://nodejs.org/en/website.

B. Enter the current project file in cmd, NPM install request.

Nodejs sending verification codes to users via the Smart technology API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.