-
Notifications
You must be signed in to change notification settings - Fork 35
/
app.js
92 lines (70 loc) · 2.15 KB
/
app.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
87
88
89
90
91
92
var express = require('express');
var debug = require('debug');
var log = debug('weixin');
var error = debug('weixin:error');
var webot = require('weixin-robot');
var User = require('./model/user');
var douban = require('./lib/douban');
var fanjian = require('./lib/fanjian');
var RedisStore = require('connect-redis')(express);
var messages = require('./data/messages');
var conf = require('./conf');
webot.codeReplies = messages;
var app = express();
app._conf = conf;
app.use(express.static(__dirname + '/static'));
app.engine('jade', require('jade').__express);
app.set('view engine', 'jade');
app.set('views', __dirname + '/templates');
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: conf.salt, store: new RedisStore(conf.redis) }));
// load rules
require('./rules')(webot);
var ONE_HOUR = 3600000;
webot.beforeReply(function ensure_zhs(info, next) {
// add alias
info.from = info.uid;
// find user
User.getOrCreate(info.uid, function(err, user) {
if (err) {
info.ended = true;
return next(err);
}
info.user = user;
// waiter will expire
if (user.mtime < new Date() - ONE_HOUR) {
delete info.session.waiter;
}
if (!info.text) return next();
fanjian(info.text, function(ret) {
if (ret !== info.text) info.is_zht = true;
info.text = ret;
next();
});
});
});
webot.afterReply(function reply_output(info, next) {
if (info.err == 404 && info.param.start) {
info.reply = messages['NO_MORE'];
} else if (info.err || !info.reply) {
//res.statusCode = (typeof err === 'number' ? err : 500);
info.reply = info.reply || messages[String(info.err)] || messages['503'];
}
if (typeof info.reply === 'string') {
info.reply = info.reply.trim();
}
if (!info.is_zht) return next();
fanjian.zhs2zht(info.reply, function(ret) {
info.reply = ret || info.reply;
next();
});
});
require('./serve')(app, webot);
webot.watch(app, conf.weixin);
var port = conf.port || 3000;
var hostname = conf.hostname || '127.0.0.1';
app.listen(port, hostname, function() {
log('listening on ', hostname, port);
});
app.enable('trust proxy');