-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
121 lines (106 loc) · 4.11 KB
/
server.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require('newrelic');
const express = require('express');
const path = require('path');
const Bot = require('messenger-bot');
const Cache = require('./components/cache');
const Actions = require('./components/actions');
const Utterance = require('./components/utterance');
const CafeRandomActions = require('./components/actions/cafe-random');
const WithinCountryActions = require('./components/actions/within-country');
const WithinProximityActions = require('./components/actions/within-proximity');
const OtherActions = require('./components/actions/other');
const Analytics = require('./components/analytics');
const TelegramConfig = require('./config/telegram');
const Notify = require('./components/notify');
const Utility = require('./components/actions/utility');
const {
handleRandomRepeat
} = require('./components/actions/cafe-random-repeat');
const app = express();
/** CONFIG */
process.env.PORT = process.env.PORT || 8080;
const config = {
development: {
token: process.env.TOKEN,
verify: process.env.VERIFY,
app_secret: process.env.APP_SECRET
},
production: {
token: process.env.facebook_token,
verify: process.env.verification_token,
app_secret: process.env.facebook_app_secret
},
};
const bot = new Bot(config[process.env.NODE_ENV]);
const postbackHandlers = {
[Actions.CAFE_RANDOM]: CafeRandomActions.handle,
[Actions.WITHIN_COUNTRY_RANDOM]: WithinCountryActions.handleRandom,
[Actions.WITHIN_COUNTRY_RANDOM_REPEAT]: handleRandomRepeat,
[Actions.WITHIN_PROXIMITY_RANDOM_REPEAT]: handleRandomRepeat,
[Actions.WITHIN_PROXIMITY_RANDOM]: WithinProximityActions.handleRandom,
[Actions.WITHIN_200M_RANDOM]: WithinProximityActions.handle200mRandom,
[Actions.WITHIN_500M_RANDOM]: WithinProximityActions.handle500mRandom,
[Actions.WITHIN_2KM_RANDOM]: WithinProximityActions.handle2kmRandom,
[Actions.WITHIN_NEVERMIND]: WithinProximityActions.handleNevermind
};
bot.on('postback', (payload, reply) => {
console.log('|--- bot.on("postback") ---|');
const senderFacebookId = payload.sender.id;
const action = payload.postback.payload;
if (action === Actions.WITHIN_PROXIMITY_RANDOM || action === Actions.WITHIN_COUNTRY_RANDOM) {
Cache.setLastAction(senderFacebookId, action);
}
bot.getProfile(senderFacebookId, (err, profile) => {
Object.assign(profile, { sender: payload.sender });
const label = payload.postback.payload;
const handler = postbackHandlers[action];
handler ? handler(reply, profile) : OtherActions.handleUnknown();
Analytics.sendEvent(action, label, senderFacebookId);
});
});
bot.on('message', (payload, reply) => {
console.log('|--- bot.on("message") ---|');
const senderFacebookId = payload.sender.id;
const isText = (typeof payload.message.text !== 'undefined')
const isAttachments = (typeof payload.message.attachments !== 'undefined');
bot.getProfile(senderFacebookId, (err, profile) => {
Object.assign(profile, { sender: payload.sender });
Cache.setUser(senderFacebookId, profile.first_name);
if(isText) {
Utterance.handleText(bot, reply, payload.sender.id);
} else if(isAttachments) {
if(err) {
console.error(err);
Utility
} else {
const { attachments } = payload.message;
attachments.forEach(attachment => {
if(attachment.type === 'location') {
const { coordinates } = attachment.payload;
Utterance.handleLocation(reply, profile, coordinates, () => {
reply('It\'s coming soon!');
});
}
});
}
}
});
});
bot.on('error', (err) => {
console.log('|--- bot.on("error") ---|');
console.log(err.message);
});
app.use("/fbbot", bot.middleware());
app.use("/_coverage", express.static(path.resolve('./coverage/lcov-report')));
if (process.env.NODE_ENV === "development") {
app.use("/", require("./config/details.js"));
}
(TelegramConfig.notificationBotToken && TelegramConfig.chatId) && (() => {
const message = `Good day, commanders. KopiBoy has just started in \`${process.env.NODE_ENV}\` environment.`;
Notify('telegram', message, {
chatId: TelegramConfig.chatId,
token: TelegramConfig.notificationBotToken
});
})();
app.listen(process.env.PORT);
console.log(`Kopiboy bot server running at port ${process.env.PORT}.`);