Home > Web Front-end > JS Tutorial > NodeJS implements Alibaba Big Fish SMS notification sending_node.js

NodeJS implements Alibaba Big Fish SMS notification sending_node.js

WBOY
Release: 2016-05-16 15:19:32
Original
1366 people have browsed it

1. Description

Alibaba Dayu provides verification codes, SMS notifications, voice and other services. It feels very convenient after use. It is worthy of being an Alibaba product.

Recently I want to develop a function of NodeJS sending SMS notifications. Alidayu-node is not used here (I didn’t know this existed when I started doing it. It would be simple if I use Alidayu-node.). I create a signature myself and implement it. SMS sent.

The official document explains the steps for generating a signature, but the third step:

3. Connect the string. Connect the parameter name and parameter value, and add secret

at the beginning and end.

There is an error (it is wrong to add secret at the beginning and end)

2. Code implementation

Generate sign

  /**
 * 根据淘宝官网提供的教程
 * http://open.taobao.com/doc2/detail.htm?articleId=101617&docType=1&treeId=1
 */
var md5 = require("blueimp-md5");

// 淘宝应用App信息(换成自己的)
var config = {
  AppKey: '233002**',
  AppSecret: '3403636b338e100399**'
};
exports.config=config;

var dySign = function (obj) {
  // 生成时间戳
  var time = new Date();
  var timestamp = time.getFullYear() + "-" +
    ("0" + (time.getMonth() + 1)).slice(-2) + "-" +
    ("0" + time.getDate()).slice(-2) + ' ' +
    ("0" + time.getHours()).slice(-2)  + ":" +
    ("0" + time.getMinutes()).slice(-2) + ":" +
    ("0" + time.getSeconds()).slice(-2);
  obj.timestamp = timestamp;

  // 程序key
  obj.app_key = config.AppKey;
  
  // 参数数组
  var arr = [];
  // 循环添加参数项
  for(var p in obj){
    arr.push(p + obj[p]);
  }
  // 2、按首字母升序排列
  arr.sort();
  // 3、连接字符串
  var msg = arr.join('');
  // console.log(msg);

  // 生成签名 sign hmac
  var sign = md5(msg, config.AppSecret);

  // 返回
  return sign.toUpperCase();
}

module.exports.dySign = dySign;

Copy after login

Concatenate strings and send requests

  var signGenerate = require('./create_dysign');
var http = require('http');
var qs = require('querystring');

var alidayuUrl = 'http://gw.api.taobao.com/router/rest';
// 短信发送的参数对象
var obj = {
  format: 'json',
  method: 'alibaba.aliqin.fc.sms.num.send',
  v: '2.0',
  timestamp: '2016-1-16 02:33:30',
  partner_id: 'top-sdk-nodejs-20160116',
  rec_num: '15110****', //手机号多个以逗号间隔
  sign_method: 'hmac',
  sms_type: 'normal',
  sms_param: '{"code":"giscafer","product":"alidayu短信测试"}',
  sms_free_sign_name: '身份验证',
  sms_template_code: 'SMS_4725038'
}
//生成签名并拼接请求参数链接
var sign = signGenerate.dySign(obj);
console.log('签名:', sign);

obj.sign = sign;
obj.app_key = signGenerate.config.AppKey;
var arr = [];
for (var p in obj) {
  arr.push(p + '=' + obj[p]);
}
var msg = arr.join('&')
var sendurl = alidayuUrl + '?' + msg;
console.log('完成请求地址:' + sendurl);

/**
 * 短信发送请求测试
 */
var params = qs.stringify(obj);
var options = {
  hostname: 'gw.api.taobao.com',
  port: 80,
  path: '/router/rest?' + params,
  method: 'GET'
};
var req = http.request(options, function (res) {
  console.log('STATUS: ' + res.statusCode);
  // console.log('HEADERS: ' + JSON.stringify(res.headers));
  // console.log(req.path);
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('RESULT: ' + chunk);
  });
});

req.on('error', function (e) {
  console.log('ERROR: ' + e.message);
});

// write data to request body
req.write("执行完毕!");

req.end();

Copy after login

Request result

Copy code The code is as follows:
{"alibaba_aliqin_fc_sms_num_send_response":{"result":{"err_code": "0","model":"100545278271^1100912293896","success":true},"request_id":"15q8um2g8w8ef"}}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template