Skip to content

Commit

Permalink
Merge pull request #354 from JemyCheung/sms
Browse files Browse the repository at this point in the history
Sms
  • Loading branch information
bachue authored Dec 3, 2019
2 parents 5c3f9c4 + 3266c94 commit ff0130e
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 1 deletion.
99 changes: 99 additions & 0 deletions examples/sms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const qiniu = require('../index.js');
const proc = require('process');
const should = require('should');
const assert = require('assert');

// eslint-disable-next-line no-undef
before(function(done) {
if (!process.env.QINIU_ACCESS_KEY || !process.env.QINIU_SECRET_KEY) {
console.log('should run command `source test-env.sh` first\n');
process.exit(0);
}
done();
});

// message
describe('test Message', function() {
var accessKey = proc.env.QINIU_ACCESS_KEY;
var secretKey = proc.env.QINIU_SECRET_KEY;
var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
// eslint-disable-next-line no-undef
describe('test sendMessage', function() {
// eslint-disable-next-line no-undef
it('test sendMessage', function(done) {
var num = new Array("17321129884","18120582893");
var reqBody = {
"template_id": "1199572412090290176",
"mobiles": num,
"parameters": {
"prize": "3333",
"name": "sendMessage",
"time": "1238"
}
};
qiniu.sms.message.sendMessage(reqBody, mac, function(respErr, respBody,
respInfo) {
should.not.exist(respErr);
assert.strictEqual(respInfo.statusCode, 200);
done();
});
});
});
describe('test sendSingleMessage', function() {
it('test sendSingleMessage', function(done) {
var reqBody = {
"template_id": "1199572412090290176",
"mobile": "17321129884",
"parameters": {
"prize": "3333",
"name": "sendSingleMessage",
"time": "1238"
}
};
qiniu.sms.message.sendSingleMessage(reqBody, mac, function(respErr, respBody,
respInfo) {
should.not.exist(respErr);
assert.strictEqual(respInfo.statusCode, 200);
done();
});
});
});
describe('test sendOverseaMessage', function() {
it('test sendOverseaMessage', function(done) {
var reqBody = {
"template_id": "1199572412090290176",
"mobile": "17321129884",
"parameters": {
"prize": "3333",
"name": "1111",
"time": "1238"
}
};
qiniu.sms.message.sendOverseaMessage(reqBody, mac, function(respErr, respBody,
respInfo) {
should.not.exist(respErr);
assert.strictEqual(respInfo.statusCode, 200);
done();
});
});
});
describe('test sendFulltextMessage', function() {
it('test sendFulltextMessage', function(done) {
var num = new Array("17321129884","18120582893");
var reqBody = {
"mobiles": num,
"content": "【七牛云-测试】您的验证码为1121,该验证码5分钟内有效",
"template_type": "verification"
};
qiniu.sms.message.sendFulltextMessage(reqBody, mac, function(respErr, respBody,
respInfo) {
if(respErr!=null){
console.log(respErr);
}
should.not.exist(respErr);
assert.strictEqual(respInfo.statusCode, 200);
done();
});
});
});
});
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ module.exports = {
zone: require('./qiniu/zone.js'),
app: require('./qiniu/rtc/app.js'),
room: require('./qiniu/rtc/room.js'),
Credentials: require('./qiniu/rtc/credentials.js')
Credentials: require('./qiniu/rtc/credentials.js'),
sms: {
message: require('./qiniu/sms/message.js'),
}
};
58 changes: 58 additions & 0 deletions qiniu/sms/message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const util = require('../util');
const urllib = require('urllib');
exports.sendMessage = function (reqBody,mac,callbackFunc){
reqBody = JSON.stringify(reqBody);
var args = {
requestURI:"https://sms.qiniuapi.com/v1/message",
reqBody:reqBody,
mac:mac,
}
post(args,callbackFunc);
}

exports.sendSingleMessage = function (reqBody,mac,callbackFunc){
reqBody = JSON.stringify(reqBody);
var args = {
requestURI:"https://sms.qiniuapi.com/v1/message/single",
reqBody:reqBody,
mac:mac,
}
post(args,callbackFunc);
}

exports.sendOverseaMessage = function (reqBody,mac,callbackFunc){
reqBody = JSON.stringify(reqBody);
var args = {
requestURI:"https://sms.qiniuapi.com/v1/message/oversea",
reqBody:reqBody,
mac:mac,
}
post(args,callbackFunc);
}

exports.sendFulltextMessage = function (reqBody,mac,callbackFunc){
reqBody = JSON.stringify(reqBody);
var args = {
requestURI:"https://sms.qiniuapi.com/v1/message/fulltext",
reqBody:reqBody,
mac:mac,
}
post(args,callbackFunc);
}

function post(args,callbackFunc){
var contentType = 'application/json';
var accessToken = util.generateAccessTokenV2(args.mac, args.requestURI, 'POST', contentType, args.reqBody);
var headers = {
'Authorization': accessToken,
'Content-Type': contentType,
}

var data = {
method: 'POST',
headers: headers,
data: args.reqBody,
}

urllib.request(args.requestURI, data, callbackFunc);
}

0 comments on commit ff0130e

Please sign in to comment.