-
Notifications
You must be signed in to change notification settings - Fork 7
/
router-builder.js
executable file
·96 lines (84 loc) · 3.48 KB
/
router-builder.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
import co from 'co';
import { resolve } from 'path';
import logger from 'logger';
import createFBMessengerClient, { messengerTunneling } from 'nbp-adapter-fb-messenger';
import createWitClient, { witTunneling } from 'nbp-adapter-wit';
import createMemcachedClient, { memcachedTunneling } from 'nbp-adapter-memcached';
import createGoogleNaturalLanguageClient, { googleLanguageTunneling } from 'nbp-adapter-google-natural-language';
import createGoogleDatastoreClient, { googleDatastoreTunneling } from 'nbp-adapter-google-datastore';
import locales from 'nbp-locales';
import createRules from 'nbp-rules';
import normaliser from 'nbp-normaliser-fb-messenger';
import skillsCluster from './clusters/core';
import vocabulary from './vocabulary';
const PLATFORM = 'your-platform';
const FB_MESSENGER_ACCESS_TOKEN = process.env.FACEBOOK_PAGE_TOKEN;
const APP_WIT_TOKEN = process.env.APP_WIT_TOKEN;
const APP_WIT_VERSION = process.env.APP_WIT_VERSION;
const MEMCACHED_ADDRESS = process.env.MEMCACHE_PORT_11211_TCP_ADDR;
const MEMCACHED_PORT = process.env.MEMCACHE_PORT_11211_TCP_PORT;
const GOOGLE_PROJECT_ID = process.env.GOOGLE_PROJECT_ID;
const GOOGLE_PROJECT_KEYS_FILENAME = process.env.GOOGLE_PROJECT_KEYS || resolve(__dirname, '../../../../../keys/google.json');
// This session will be passed to all skills
const initSession = bot => ({
bot,
rules: createRules({
// Decide if we should be silent (should we talk to user or no)
silent: false,
// Method used to specify vocabulary for the current
// Please, look at skill <locales> in <core> cluster
getLocales: locales(vocabulary)
})
});
// Here we will build a session object, where we will store all tunneled services and functions
export default function(router, route) {
// POST used to recieve requests from IM
router.post(route, [
(req, res, next) => {
// initialize an object inside request;
// here we will collect all references to services and data, related to current request
req.bot = {
platform: PLATFORM
};
next();
},
normaliser,
googleDatastoreTunneling(createGoogleDatastoreClient({
platform: PLATFORM,
projectId: GOOGLE_PROJECT_ID,
keyFilename: GOOGLE_PROJECT_KEYS_FILENAME,
logger
})),
memcachedTunneling(createMemcachedClient({
platform: PLATFORM,
address: MEMCACHED_ADDRESS,
port: MEMCACHED_PORT,
logger
})),
witTunneling(createWitClient({
token: APP_WIT_TOKEN,
version: APP_WIT_VERSION,
logger
})),
googleLanguageTunneling(createGoogleNaturalLanguageClient({
projectId: GOOGLE_PROJECT_ID,
keyFilename: GOOGLE_PROJECT_KEYS_FILENAME,
logger
})),
messengerTunneling(createFBMessengerClient({
accessToken: FB_MESSENGER_ACCESS_TOKEN,
logger
})),
(req, res) => {
// Say to messenger, that we've got it's request
res.status(200).send('ok');
// For each request we've got from client (for example, several messages),
// start initial skills cluster
req.bot.normalized.forEach(bot => co(skillsCluster(initSession(bot))));
}
]);
// GET used to setup Facebook Messenger webhooks
router.get(route, (req, res) => {
res.send(req.query['hub.challenge']);
});
}