-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
188 lines (135 loc) · 6.65 KB
/
server.js
File metadata and controls
188 lines (135 loc) · 6.65 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// TODO: encrypt app to run HTTPS locally, check:
// https://github.com/botmasterai/botmaster-socket.io/issues/2#issuecomment-303785233
// https://www.sitepoint.com/how-to-use-ssltls-with-node-js/
// Load enviroment variables from file ".env"
const dotenv = require('dotenv').config();
/////////////////////////////////////////////////////
/// Express server for Frontend app - START
/////////////////////////////////////////////////////
// TODO: Refractor the frontend app in a separate file/folder
const express = require('express');
const port = process.env.PORT || 3000;
const host = process.env.HOST || "0.0.0.0";
const app = express();
// Routing for ./public/index.html
app.use(express.static(__dirname + '/public'));
const server = app.listen(port, host, () => {
console.log('Server listening at http://%s:%d', host, port);
});
/////////////////////////////////////////////////////
/// Express server for Frontend app - END
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/// Botmaster Backend
/////////////////////////////////////////////////////
const Botmaster = require('botmaster');
// Adding a new Botmaster
const botmaster = new Botmaster({
server
});
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/// Bot platforms - START
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/// Bot platform: SocketIo
/////////////////////////////////////////////////////
// Using this socket.io bot class for the sake of the example
const SocketioBot = require('botmaster-socket.io');
// Adding a SocketIo to botmaster
botmaster.addBot(new SocketioBot({
id: 'SOCKETIO_BOT_ID', //TOTO: change id
// server: botmaster.server, // this is required for socket.io. You can set it to another node server object if you wish to. But in this example, we will use the one created by botmaster under the hood
server: server
}));
/////////////////////////////////////////////////////
/// Bot platform: Facebook Messenger
/////////////////////////////////////////////////////
const MessengerBot = require('botmaster-messenger');
const messengerSettings = {
credentials: {
verifyToken: process.env.FACEBOOK_VERIFY_TOKEN,
pageToken: process.env.FACEBOOK_PAGE_TOKEN,
fbAppSecret: process.env.FACEBOOK_APP_SECRET,
},
webhookEndpoint: process.env.FACEBOOK_WEBHOOK_ENDPOINT
};
const messengerBot = new MessengerBot(messengerSettings);
botmaster.addBot(messengerBot);
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/// Bot platforms - END
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/// Bot middleware - START
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
const incomingMiddleware = require('./middleware/incoming');
const outgoingMiddleware = require('./middleware/outgoing');
const wrappedMiddleware = require('./middleware/wrapped');
/////////////////////////////////////////////////////
/// Bot middleware incoming: DB - store all incoming messages (from user to bot)
/////////////////////////////////////////////////////
// TODO
// botmaster.use(incomingMiddleware.db.storeMessage);
/////////////////////////////////////////////////////
/// Bot middleware incoming: translate incoming message
/////////////////////////////////////////////////////
if (process.env.WATSON_LANGUAGE_TRANSLATOR_ENABLED == 'true') {
botmaster.use(incomingMiddleware.translate.translateMessage);
}
/////////////////////////////////////////////////////
/// Bot middleware incoming: IBM Watson Conversation Middleware and SessionWare
/////////////////////////////////////////////////////
// might want to use this in conjunction with your own store in production
// as SessionWare uses the non-production ready MemoryStore by default
const SessionWare = require('botmaster-session-ware');
const WatsonConversationWare = require('botmaster-watson-conversation-ware');
// Setting Watson Conversation/Assistant credentials
const watsonConversationWareOptions = {
settings: {
username: process.env.WATSON_ASSISTANT_USERNAME,
password: process.env.WATSON_ASSISTANT_PASSWORD,
version: process.env.WATSON_ASSISTANT_VERSION, // new Watson Assistant v2 is available, but to use it first we need to update the "botmaster-watson-conversation-ware" library
version_date: process.env.WATSON_ASSISTANT_VERSION_DATE, // '2017-05-26' = version of when the demo repo was created
},
workspaceId: process.env.WATSON_ASSISTANT_WORKSPACE_ID // Currently poiting to the skill/workspace "Customer Care"
}
// Declaring middleware
const watsonConversationWare = WatsonConversationWare(watsonConversationWareOptions);
botmaster.use(watsonConversationWare);
// This will make our context persist throughout different messages from the same user
const sessionWare = new SessionWare();
botmaster.useWrapped(sessionWare.incoming, sessionWare.outgoing);
/////////////////////////////////////////////////////
/// Bot middleware incoming: Reply user (watsonUpdate)
/////////////////////////////////////////////////////
botmaster.use(incomingMiddleware.reply.replyToUser);
/////////////////////////////////////////////////////
/// Bot middleware outgoing: translate outgoing message
/////////////////////////////////////////////////////
if (process.env.WATSON_LANGUAGE_TRANSLATOR_ENABLED == 'true') {
botmaster.use(outgoingMiddleware.translate.translateMessage);
}
/////////////////////////////////////////////////////
/// Bot middleware outgoing: DB - store all outgoing messages (from bot to user)
/////////////////////////////////////////////////////
// TODO
/////////////////////////////////////////////////////
/// Bot middleware outgoing: show is typing meesage
/////////////////////////////////////////////////////
// botmaster.use(outgoingMiddleware.botIsTyping.sendTypingMessage); // Error: Bots of type socket.io can't send messages with typing_on sender action at SocketioBot.sendIsTypingMessageTo
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/// Bot middleware - END
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/// Botmaster error log
/////////////////////////////////////////////////////
botmaster.on('error', (bot, err) => {
console.log(err.stack);
});