-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwechat.js
86 lines (72 loc) · 2.62 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
80
81
82
83
84
85
86
var querystring = require('querystring'),
cypher = require('./cypher'),
url = require('url'),
xml2js = require('xml2js'),
logging = require('./logging'),
agent = require('./xmlBuilder'),
handler = require('./messageHandler'),
parser = new xml2js.Parser(),
_TOKEN = "the token you set",
_CORPID = 'your corp id',
_ENCODINGAESKEY = "encoding aes key shall be seen when you set the call back mod",
_AGENTID = "agent id can be seen from the setting panel of your enterprise app",
query,
davonki = cypher.davonki(_TOKEN, _CORPID, _ENCODINGAESKEY, _AGENTID);
function process(req, res, next) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var param = req.url.split("?")[1];
query = param ? querystring.parse(param) : {};
if (req.method == "GET") {
onGet(req, res, next);
} else {
onPost(req, res, next);
}
}
function onGet(req, res, next) {
logging.log("[GET]:" + req.url);
var path = url.parse(req.url).pathname;
var sVerifyMsgSig = decodeURI(query.msg_signature);
var sVerifyTimeStamp = decodeURI(query.timestamp);
var sVerifyNonce = decodeURI(query.nonce);
var sVerifyEchoStr = decodeURI(query.echostr);
var verifyResult = davonki.verifyURL(sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, sVerifyEchoStr);
if (path == '/log') {
logging.getLog(function (data) {
res.end(data)
});
} else {
res.end(verifyResult);
}
}
function onPost(req, res, next) {
logging.log("[POST]:" + req.url);
var sea = "";
req.addListener("data", function (water) {
sea += water;
});
req.addListener("end", function () {
parser.parseString(sea, function (err, result) {
logging.log("[POST body]:" + result);
if (err) {
logging.log(err);
}
var xml = agent.formatMessage(result.xml);
var sVerifyMsgSig = decodeURI(query.msg_signature);
var sVerifyTimeStamp = decodeURI(query.timestamp);
var sVerifyNonce = decodeURI(query.nonce);
davonki.decryptMsg(sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, xml, function (json) {
var assistantHandler = handler[json.MsgType];
if (assistantHandler && (typeof assistantHandler == "function")) {
res.writeHead(200, {
'Content-Type': 'text/xml'
});
assistantHandler(json, agent.build(json, davonki, res));
}
});
});
});
}
exports.process = process;
/**
* Created by Robin on 10/18/2014.
*/