-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwechat.js
79 lines (72 loc) · 2.27 KB
/
wechat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var WechatAPI = require('wechat-api');
var fs = require("fs");
let templateId = 'pe4G6xS4ZYjUViGetMR75KW10CHpLVDFSebKc-n74tw';
let appId = 'wx3de2742296b5b267';
let appSecret = 'a351088ffbcf0500b7390a9a792888c2';
let myOpenId = 'o0dZUv0gdwqOa2osmkONzaAyuXiE';
var api = new WechatAPI(appId, appSecret, function (callback) {
// 传入一个获取全局token的方法
fs.readFile('access_token.txt', 'utf8', function (err, txt) {
//文件不存在创建
if (err && err.code == 'ENOENT') {return callback(null, null);}
else if (err) {return callback(err)}
callback(null, JSON.parse(txt));
});
}, function (token, callback) {
// 请将token存储到全局,跨进程、跨机器级别的全局,比如写到数据库、redis等
// 这样才能在cluster模式及多机情况下使用,以下为写入到文件的示例
fs.writeFile('access_token.txt', JSON.stringify(token), callback);
});
// URL置空,则在发送后,点击模板消息会进入一个空白页面(ios), 或无法点击(android)
// var url = 'http://weixin.qq.com/download';
exports.pushWechat = function(openid, result) {
var data = {
"first": {
"value":"服务器瓦特了",
"color":"#173177"
},
"performance":{
"value":"请赶紧处理一下",
"color":"#173177"
},
"time": {
"value":"已经发生"+result,
"color":"#173177"
},
"remark":{
"value":"请联系一下资源中心说明下情况",
"color":"#173177"
}
};
api.sendTemplate(openid, templateId, null, data, function (err, data, res) {
if (err) console.log(err);
//console.log(data);
//console.log(res);
});
}
exports.pushSMS = function(sender, content) {
if (!sender) return;
var data = {
"first": {
"value":"短信来了",
"color":"#173177"
},
"performance":{
"value":sender.substring(0, sender.length-12).trim(),
"color":"#173177"
},
"time": {
"value":sender.substring(sender.length-11),
"color":"#173177"
},
"remark":{
"value":content,
"color":"#173177"
}
};
api.sendTemplate(myOpenId, templateId, null, data, function (err, data, res) {
if (err) console.log(err);
//console.log(data);
//console.log(res);
});
}