-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
97 lines (82 loc) · 2.91 KB
/
index.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
const restify = require('restify'),
builder = require('botbuilder'),
mongoose = require('mongoose'),
chatbase = require('@google/chatbase'),
config = require('./config'),
dialogs = require('./dialogs'),
version = require('./package.json').version,
isChat = process.env.NODE_ENV === 'production' || false,
mongoUri = process.env.MONGO_URI || config.MONGO;
//=========================================================
// DB Setup
//=========================================================
mongoose.connect(mongoUri);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'mongo connection error:'));
db.once('open', function() {
console.log('Connected to mongo');
});
//=========================================================
// Bot Setup
//=========================================================
let bot;
if ( isChat ) {
// Setup Restify Server
const server = restify.createServer();
const port = (process.env.PORT || config.SERVER_PORT);
const ip = (process.env.IP || config.IP);
server.listen(port, ip, function() {
console.log('%s listening to %s', server.name, server.url);
});
const connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());
server.get('/health', function( req, res ) {
res.send();
});
} else {
const connector = new builder.ConsoleConnector().listen();
bot = new builder.UniversalBot(connector);
}
if (process.env.CHATBASE_KEY) {
chatbase.setApiKey(process.env.CHATBASE_KEY);
bot.use({
botbuilder: function (session, next) {
const event = session.message;
chatbase.newMessage()
.setAsTypeUser()
.setPlatform(event.address.channelId)
.setVersion(version)
.setTimestamp(new Date(event.timestamp).getTime().toString())
.setMessage(event.text)
.setUserId(event.address.user.id || event.user.id)
.send()
.catch(err => console.error(`Error sending data to Chatbase: `, err));
next();
},
send: function (event, next) {
chatbase.newMessage()
.setAsTypeAgent()
.setPlatform(event.address.channelId)
.setVersion(version)
.setMessage(event.text)
.setUserId(event.address.user.id)
.send()
.catch(err => console.error(`Error sending data to Chatbase: `, err));
next();
}
});
}
//=========================================================
// Bots Dialogs
//=========================================================
bot.dialog('/',[]);
bot.dialog('/profile', dialogs.profile);
bot.dialog('/recruiter', dialogs.recruiter);
bot.dialog('/offer', dialogs.offer);
bot.dialog('/firstRun', dialogs.firstRun);
// Install First Run middleware and dialog
bot.use(builder.Middleware.firstRun({ version: 1.0, dialogId: '*:/firstRun' }));