Nodejs Realizes SMS Verification Code Function

  • 2021-07-18 06:58:36
  • OfStack

There are more and more developers using Nodejs, and there are more background development based on Nodejs. Requirements such as SMS verification code, SMS group sending and international SMS can be realized by the third-party interface, which is provided by cloud chip.

Nodejs


//  Modify to your apikey. Available in official website ( https://www.yunpian.com) Get after logging in 
var https = require('https');
var qs = require('querystring');
var apikey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
//  Modify to the mobile phone number you want to send, and separate multiple numbers with commas 
var mobile = 'xxxxxxxxxxx';
//  Modify to the content of the SMS you want to send 
var text = ' [Cloud Network] Your verification code is 1234';
//  Specify the template number to send 
var tpl_id = 1;
//  Specify the contents of the send template 
var tpl_value = {'#code#':'1234','#company#':'yunpian'};
//  Content of voice short message 
var code = '1234';
//  Inquire about account information https Address 
var get_user_info_uri = '/v2/user/get.json';
//  Intelligent matching template sending https Address 
var sms_host = 'sms.yunpian.com';
var voice_host = 'voice.yunpian.com';
send_sms_uri = '/v2/sms/single_send.json';
//  Specify the template sending interface https Address 
send_tpl_sms_uri = '/v2/sms/tpl_single_send.json';
//  Send voice verification code interface https Address 
send_voice_uri = '/v2/voice/send.json';
query_user_info(get_user_info_uri,apikey);
send_sms(send_sms_uri,apikey,mobile,text);
send_tpl_sms(send_tpl_sms_uri,apikey,mobile,tpl_id,tpl_value);
send_voice_sms(send_voice_uri,apikey,mobile,code);
function query_user_info(uri,apikey){
 var post_data = { 
 'apikey': apikey, 
 };// This is the data that needs to be submitted 
 var content = qs.stringify(post_data); 
 post(uri,content,sms_host);
}
function send_sms(uri,apikey,mobile,text){
 var post_data = { 
 'apikey': apikey, 
 'mobile':mobile,
 'text':text,
 };// This is the data that needs to be submitted  
 var content = qs.stringify(post_data); 
 post(uri,content,sms_host);
}
function send_tpl_sms(uri,apikey,mobile,tpl_id,tpl_value){
 var post_data = { 
 'apikey': apikey,
 'mobile':mobile,
 'tpl_id':tpl_id,
 'tpl_value':qs.stringify(tpl_value), 
 };// This is the data that needs to be submitted  
 var content = qs.stringify(post_data); 
 post(uri,content,sms_host); 
}
function send_voice_sms(uri,apikey,mobile,code){
 var post_data = { 
 'apikey': apikey,
 'mobile':mobile,
 'code':code,
 };// This is the data that needs to be submitted  
 var content = qs.stringify(post_data); 
 console.log(content);
 post(uri,content,voice_host); 
}
function post(uri,content,host){
 var options = { 
  hostname: host,
  port: 443, 
  path: uri, 
  method: 'POST', 
  headers: { 
   'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' 
  } 
 };
 var req = https.request(options, function (res) { 
  // console.log('STATUS: ' + res.statusCode); 
  // console.log('HEADERS: ' + JSON.stringify(res.headers)); 
  res.setEncoding('utf8'); 
  res.on('data', function (chunk) { 
   console.log('BODY: ' + chunk); 
  }); 
 }); 
 //console.log(content);
 req.write(content); 
 req.end(); 
}

The above is all the interfaces of the cloud chip. In the actual use process, you can choose the corresponding interface according to your own needs. Specifically, you can see how to use the cloud chip API to send SMS verification code, which tells how to use single SMS API, group SMS API and batch SMS API, which is very practical.

In addition, the most important thing is that the service of cloud film is not bad, the arrival rate of SMS is relatively high, and some people reply in time when something goes wrong, which is one of the best SaaS manufacturers in China.


Related articles: