This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.
preface
Before introduced how to realize the verification of mailbox, and mobile phone authentication, better, can verify the user’s mobile phone number, prevent others from malicious registration, this paper introduces how to realize SMS sending in NodeJS, taking Tencent cloud SMS verification as an example.
However, SMS verification requires payment and some authentication information, which may not be as convenient as email registration. If you are developing a website, you are advised to go to email to send verification code: Nodejs enables email to send verification code.
Prepare in advance
-
First of all, Tencent cloud personal or enterprise authentication account, individuals will give one hundred gifts, enterprises give one thousand, can be used for testing, address: Tencent cloud SMS service.
-
Then we need to sign the certification, we need to have their own company or personal website for the record, certification, that is, the part in front of the certification message: [xx platform]. Address: Tencent cloud SMS signature.
-
The last is to prepare templates, using standard templates is easier to pass the audit. Address: Tencent cloud SMS template.
Use nodejs
Depend on the package
npm i tencentcloud-sdk-nodejs
Copy the code
code
Parameters you need to modify yourself:
-
Obtaining secretId and secretKey: API key management.
-
SMS application SmsSdkAppId: indicates the SDKAppID of the default application.
- Approved template
TemplateId
:Tencent Cloud SMS template management.
-
PhoneNumberSet Enter the phone number you want to send to.
-
TemplateParamSet Fill in the template parameters, like template if you are applying for mobile phone registration, the verification code is: {1}, {2} valid in minutes! Where {1} and {2} are parameters, we can dynamically pass in an array of parameters: a verification code and a valid time.
import tencentcloud from "tencentcloud-sdk-nodejs"
// Import the client Models for the corresponding product module.
const smsClient = tencentcloud.sms.v20210111.Client
/* Instantiate the client object */ to request the product (in the case of SMS)
const client = new smsClient({
credential: {
/* Required: Tencent cloud account key pair secretId, secretKey. * You can also write dead key pairs in code, but be careful not to copy, upload, or share the code with others, * lest you compromise your property by revealing the key pair. * SecretId, SecretKey query: https://console.cloud.tencent.com/cam/capi * /
secretId: 'your secretId'.secretKey: 'your secretKey',},/ * required: For regional information, you can directly fill in the string ap-Guangzhou. Support regional reference https://cloud.tencent.com/document/api/382/52071#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8 * / list
region: "ap-guangzhou"./* Optional: * Client configuration object, which can specify the timeout duration */
profile: {
/* the SDK uses tc3-hmac-sha256 by default. Do not change this field unless necessary
signMethod: "HmacSHA256".httpProfile: {
/* The SDK uses POST by default. * If you must use the GET method, you can set it here. The GET method cannot handle some large requests */
reqMethod: "POST"./* The SDK has a default timeout, do not adjust it unless necessary * * Check the code if necessary for the latest default value */
reqTimeout: 30./ * * * to specify access geographical domain, the default regional access to the nearest domain name at sms.tencentcloudapi.com, and also support access to a designated area domain name, such as the guangzhou region of domain for sms.ap-guangzhou.tencentcloudapi.com * /
endpoint: "sms.tencentcloudapi.com"}},})/* Request parameters, depending on the interface called and the actual situation, can be further set request parameters * attributes may be basic type, may also reference another data structure * recommended to use IDE for development, you can easily jump to refer to the various interface and data structure documentation */
const params = {
/* SMS application ID: SmsSdkAppId Indicates the actual SmsSdkAppId generated after the application is added to the SMS console, for example, 1400006666 */
SmsSdkAppId: "Your SDKAppID"./* SMS signature content: Use UTF-8 encoding, must fill in the approved signature, signature information can be logged in [SMS console] to view */
SignName: "Your tag name"./* EXTENSION of SMS code: disabled by default. To enable the extension, contact [SMS Helper] */
ExtendCode: ""./* International/Hong Kong/Macao/Taiwan SMS senderID: Fill in the blank for domestic SMS, which is disabled by default. For enabling SMS, please contact [SMS Helper] */
SenderId: ""./* User session content: can carry the user side ID and other context information, the server will return the same as */
SessionContext: "".+[Country or region code][Mobile phone number] * For example, +8613711112222, 86 indicates the country code, 13711112222 indicates the mobile phone number, and a maximum of 200 mobile phone numbers */
PhoneNumberSet: ["+ 8613711112222"]./* Template ID: Enter the ID of the approved template. Template ID You can log in to the SMS console to view */
TemplateId: "Your template ID"./* Template parameter: If there is no template parameter, set this parameter to empty */
TemplateParamSet: ["666666".'10'],}// Calling the desired interface through the client object requires passing in the request object and the response callback function
client.SendSms(params, function (err, response) {
// An exception message is displayed
if (err) {
console.log(err)
return
}
// The request returns normally, printing the response object
console.log(response)
})
Copy the code
The effect
Stern said
If you feel that the article is good, welcome to like collection oh, what mistakes or suggestions can also leave a message, thank you ~