forked from node-webot/webot-douban-event
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
93 lines (75 loc) · 2.29 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
93
process.on('uncaughtException', function (err) {
error('Caught exception: ' + err);
if ('trace' in err) {
err.trace();
}
if ('stack' in err) {
console.log(err.stack);
}
});
var express = require('express');
var debug = require('debug');
var log = debug('weixin');
var error = debug('weixin:error');
var webot = require('weixin-robot');
var douban = require('./lib/douban');
var fanjian = require('./lib/fanjian');
var memcached = require('./lib/memcached');
var messages = require('./data/messages');
var conf = require('./conf');
function event_list_mapping(item, i) {
return {
title: item.title,
picurl: item.image_lmobile || '',
url: item.adapt_url && item.adapt_url.replace('adapt', 'partner') || '',
description: item.owner && douban.eventDesc(item),
};
}
webot.codeReplies = messages;
webot.config.beforeSend = function(err, info, next) {
if (err == 404 && info.param.start) {
info.reply = messages['NO_MORE'];
} else if (err || !info.reply) {
//res.statusCode = (typeof err === 'number' ? err : 500);
info.reply = info.reply || messages[String(err)] || messages['503'];
}
if (Array.isArray(info.reply)) {
info.reply = info.reply.map(event_list_mapping);
if (info.has_more) {
info.reply.push({
title: '回复 more 查看更多...',
picUrl: '',
url: 'http://www.douban.com/location/',
});
}
}
if (!info.is_zht) return next();
fanjian.zhs2zht(info.reply, function(ret) {
info.reply = ret || info.reply;
next();
});
};
webot.use(function ensure_zhs(info, next) {
if (!info.text) return next();
fanjian(info.text, function(ret) {
if (ret !== info.text) info.is_zht = true;
info.text = ret;
next();
});
});
// load rules
require('./rules')(webot);
var app = express();
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.cookieParser());
app.use(express.session({ secret: conf.salt, store: new memcached.MemObj('wx_session') }));
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');