-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
242 lines (207 loc) · 8.49 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
var restify = require('restify');
var builder = require('botbuilder');
var axios = require('axios');
const appId = process.env.MicrosoftAppId || 'c718e84f-a303-4787-850d-e53f1928a804';
const appPassword = process.env.MicrosoftAppPassword || 'tdfHHOXL5{(%*zsnqUQ7011';
//const appId = 'b26ee976-6431-4667-8f78-e970db44fe07';
//const appPassword = 'srmON226%*ecilWRYHX86||';
const dianaNlpUrl = process.env.DIANA_NLP_SERVICE_URL || 'https://lit-inlet-30311.herokuapp.com/api/v0.1/diana';
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
appId,
appPassword
});
// Listen for messages from users
server.post('/api/messages', connector.listen());
// Receive messages from the user and respond by echoing each message back (prefixed with 'You said:')
var bot = new builder.UniversalBot(connector, function (session) {
// console.log("session >>??",session);
console.log(`Calling NLP Serice : ${dianaNlpUrl}?query=${session.message.text}`);
axios.post(`${dianaNlpUrl}?query=${session.message.text}`)
.then(function (response) {
const body = response.data.body;
const msgResponse = prepareResponse(body);
session.send(msgResponse);
//session.send(testBtn());
}).catch(function (error) {
console.log("ERROR", error);
session.send("Something went wrong...come back later !!");
});
});
function testBtn(){
return {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "message",
"text":"helllllo",
//"speak":speechText,
"attachments": [{
"contentType": "application/vnd.microsoft.card.adaptive",
"content": {
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": 2,
"items": [
{
"type": "TextBlock",
"text": "Tell us about yourself",
"weight": "bolder",
"size": "medium"
},
{
"type": "TextBlock",
"text": "We just need a few more details to get you booked for the trip of a lifetime!",
"isSubtle": true,
"wrap": true
},
{
"type": "TextBlock",
"text": "Don't worry, we'll never share or sell your information.",
"isSubtle": true,
"wrap": true,
"size": "small"
},
{
"type": "TextBlock",
"text": "Your name",
"wrap": true
},
{
"type": "Input.Text",
"id": "myName",
"placeholder": "Last, First"
},
{
"type": "TextBlock",
"text": "Your email",
"wrap": true
},
{
"type": "Input.Text",
"id": "myEmail",
"placeholder": "youremail@example.com",
"style": "email"
},
{
"type": "TextBlock",
"text": "Phone Number"
},
{
"type": "Input.Text",
"id": "myTel",
"placeholder": "xxx.xxx.xxxx",
"style": "tel"
}
]
},
{
"type": "Column",
"width": 1,
"items": [
{
"type": "Image",
"url": "https://upload.wikimedia.org/wikipedia/commons/b/b2/Diver_Silhouette%2C_Great_Barrier_Reef.jpg",
"size": "auto"
}
]
}
]
}
],
"actions": [
{
"type": "Action.Submit",
"title": "Submit"
},
{
"type": "Action.Submit",
"title": "Hello",
"data":{
"type":"string"
}
}
]
}
}]
};
}
function prepareResponse(standardResponse) {
switch (standardResponse.type) {
case 'SIMPLE':
return {
"type": "message",
"text": standardResponse.displayText[getRandomInt(standardResponse.displayText.length)]
};
case 'RICH':
// var speechText = '';
// for(var i=0;i<standardResponse.speechText.length;i++){
// speechText = speechText+ '\n'+standardResponse.speechText[i];
// }
var richResponse = {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "message",
//"text": standardResponse.displayText[0],
//"speak":speechText,
"attachments": [{
"contentType": "application/vnd.microsoft.card.adaptive",
"content": {
"type": "AdaptiveCard",
"version": "1.0",
"body": [],
"actions": []
}
}]
};
if (standardResponse.displayText && standardResponse.displayText.length > 0) {
for (var index = 0; index < standardResponse.displayText.length; index++) {
richResponse.attachments[0].content.body.push({
"type": "TextBlock",
"text": `${standardResponse.displayText[index]}`,
"wrap": "true",
"spacing": "large",
"separator":"true"
});
}
}
if (standardResponse.buttons && standardResponse.buttons.length > 0) {
for (var index = 0; index < standardResponse.buttons.length; index++) {
richResponse.attachments[0].content.actions.push({
"type": "Action.Submit",
"title": `${standardResponse.buttons[index].name}`,
"data": `${standardResponse.buttons[index].value}`
});
}
}
if (standardResponse.images && standardResponse.images.length > 0) {
for (var index = 0; index < standardResponse.images.length; index++) {
richResponse.attachments[0].content.body.push({
"type": "Image",
"url": `${standardResponse.images[index].url}`,
"size": "big",
"spacing": "large",
"selectAction": {
"type": "Action.OpenUrl",
"title": `Screenshot${index}`,
"url": `${standardResponse.images[index].url}`
}
});
}
}
return richResponse;
default:
return "Sorry something went wrong";
}
}
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}