DEMO: Template/Post - 创建彩信模板
var request = require('request');
var crypto = require('crypto');
var appid = "input your appid";
var appkey = "input your appkey";
var api = "https://api-v4.mysubmail.com/mms/template";
//生成加密签名
function BuildSignature(params){
var sorted = [];
for(var key in params) {
sorted[sorted.length] = key;
}
sorted.sort();
var tempDict = {};
for(var i = 0; i < sorted.length; i++) {
tempDict[sorted[i]] = params[sorted[i]];
}
var signStr = "";
for(var key in tempDict) {
signStr += key + '=' + tempDict[key] + '&';
}
signStr = signStr.substring(0, signStr.length-1);
signStr = appid + appkey + signStr + appid + appkey;
var md5sum = crypto.createHash('md5');
md5sum.update(signStr);
return md5sum.digest('hex');
}
//模板参数示例
var template_content = [
{
text : "这是彩信的第一页",
image : {
data : "iVBORw0KGgoAAAANSUhEUgAAAA4AAAAYCAYAAADKx8xXAAABYWlDQ1BrQ0dDb2xvclNwYWNlRGlzcGxheVAzAAAokWNgYFJJLCjIYWFgYMjNKykKcndSiIiMUmB/yMAOhLwMYgwKicnFBY4BAT5AJQwwGhV8u8bACKIv64LMOiU1tUm1XsDXYqbw1YuvRJsw1aMArpTU4mQg/QeIU5MLikoYGBhTgGzl8pICELsDyBYpAjoKyJ4DYqdD2BtA7CQI+whYTUiQM5B9A8hWSM5IBJrB+API1klCEk9HYkPtBQFul8zigpzESoUAYwKuJQOUpFaUgGjn/ILKosz0jBIFR2AopSp45iXr6SgYGRiaMzCAwhyi+nMgOCwZxc4gxJrvMzDY7v////9uhJjXfgaGjUCdXDsRYhoWDAyC3AwMJ3YWJBYlgoWYgZgpLY2B4dNyBgbeSAYG4QtAPdHFacZGYHlGHicGBtZ7//9/VmNgYJ/MwPB3wv//vxf9//93MVDzHQaGA3kAFSFl7jXH0fsAAABsZVhJZk1NACoAAAAIAAQBGgAFAAAAAQAAAD4BGwAFAAAAAQAAAEYBKAADAAAAAQACAACHaQAEAAAAAQAAAE4AAAAAAAAAkAAAAAEAAACQAAAAAQACoAIABAAAAAEAAAAOoAMABAAAAAEAAAAYAAAAADh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YgZgpLY2B4dNyBgbeSAYG4QtAPdHFacZGYHlGHicGBtZ7//9/VmNgYJ/MwPB3wv//vxf9//93MVDzHQaGA3kAFSFl7jXH0fsAAABsZVhJZk1NACoAAAAIAAQBGgAFAAAAAQAAAD4BGwAFAAAAAQAAAEYBKAADAAAAAQACAACHaQAEAAAAAQAAAE4AAAAAAAAAkAAAAAEAAACQAAAAAQACoAIABAAAAAEAAAAOoAMABAAAAAEAAAAYAAAAADh6st4AAAAJcEhZcwAAFiUAABYlAUlSJPAAAAGVSURBVDgRvZQ7zwFREIbfXetSKYhoRCGUGoVGo1aTaDUSxdYqP8DvkEhEwT8QEpUSlU6iUEhEiEvEMpPs5JwQm0/xnWLznpl5zuWd3TWSyaSDH4b5A8OI0e12nVQq9cbfbjdst1vMZjP0ej04jn4wYzQaObFY7A1UA5fLBbZtY7PZSFgDx+MxJ3w+HyKRCDKZDPx+P8cOhwOq1aqAlqiXaLfb6hSmaWI4HMKyLITDYV5otVpxzVdzHo8H5vO5LJbL5UR/Banq1S4pXi6XorWjBoNBTtC9CCiXy4hGoxw7Ho9YLBafwcFgIAlV7Pd7tFotNQTPo1I13ZWMUofWDndVcjGRSCCfzyObzXI9vQD1el16qYGlUkldlHWlUkGtVmNNDjebTdb6/m8Y0O/3cb/fOZNOp6XCE6TK8/nMQCgU+hsYCAQEcIXnjoVCAW5/T6eTy0F7AYrFIifI1Xg8zo66rlJiOp1ynh6aqxL9INbrNRqNhnyX2o5qPfXter1it9uh0+lgMpmoaRj//s/xdFU7nzL5GXwCoj+HhCbGKa0AAAAASUVORK5CYII=",
type : "image/png"
}
}
];
//无加密示例
request.post({
url: api,
formData: {
appid:appid,
signature:appkey,
mms_content : JSON.stringify(template_content),
mms_title : "title",
mms_signature : "【SUBMAIL】",
mms_subject : "彩信模板项目"
}
}, function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error(err);
}
console.log(body);
});
//加密示例
request({
uri: "https://api-v4.mysubmail.com/service/timestamp",
method: 'GET'
}, function(error, response, body) {
var result = JSON.parse(body);
var requestParams = {}
requestParams['timestamp'] = result["timestamp"];
requestParams['sign_type'] = "md5";
requestParams['appid'] = appid;
requestParams['mms_content'] = JSON.stringify(template_content);
requestParams['mms_title'] = "title";
requestParams['mms_signature'] = "【SUBMAIL】";
requestParams['mms_subject'] = "彩信模板项目";
requestParams['signature'] = BuildSignature(requestParams);
request.post({
url: api,
formData: requestParams
}, function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error(err);
}
console.log(body);
});
});