-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
355 lines (315 loc) · 10.7 KB
/
app.js
File metadata and controls
355 lines (315 loc) · 10.7 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
const restify = require('restify')
const builder = require('botbuilder')
const botbuilder_azure = require('botbuilder-azure')
const inMemoryStorage = new builder.MemoryBotStorage()
const utils = require('./utils')
const api = require('./api')
//--- START CONNECTIONS
var server = restify.createServer()
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s now listening to %s', server.name, server.url)
})
var connector = new builder.ChatConnector({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword,
openIdMetadata: process.env.BotOpenIdMetadata
})
server.post('/api/messages', connector.listen())
//--- INIT BOT
var bot = new builder.UniversalBot(connector, function (session, args) {
// Default Handling: When nothing else matches
if (hasMessageAttachment(session)) {
session.replaceDialog('DocumentUploadDialog')
} else {
session.send(
'\uD83E\uDD14' /* thinking emoji */
+ ' I don\'t understand what you mean, sorry.\nPlease try rephrasing your question.'
)
console.log('[?] - ' + session.message.text)
session.endConversation()
}
})
bot.set('storage', inMemoryStorage)
const hasMessageAttachment = (session) => (
session.message.attachments && session.message.attachments.length > 0
)
// extract LUIS settings from environment variables
var luisAppId = process.env.LuisAppId
var luisAPIKey = process.env.LuisAPIKey
var luisAPIHostName = process.env.LuisAPIHostName || 'westus.api.cognitive.microsoft.com'
const LuisModelUrl =
'https://' + luisAPIHostName + '/luis/v2.0/apps/' + luisAppId
+ '?subscription-key=' + luisAPIKey
// add LUIS recognizer to the bot for reacting to intents
var recognizer = new builder.LuisRecognizer(LuisModelUrl)
.onEnabled(function (session, callback) {
if (hasMessageAttachment(session)) {
// -> do not send to LUIS
callback(null, false)
} else {
callback(null, true)
}
})
bot.recognizer(recognizer)
//--- INTENT SPECIFIC DIALOG HANDLING
bot.dialog('HelpDialog',
(session) => {
session.send(
'You can ask me whatever you need about your documents and I will surely help you.'
)
}
).triggerAction({
matches: 'Help'
})
bot.dialog('CancelDialog',
(session) => {
var responses = [
'okay, aborted.',
'All right, I will drop that.',
'ok, see you later.',
'ok, anything else I can do?',
'okay, anything else?'
]
session.send(utils.choose(responses))
session.endConversation()
}
).triggerAction({
matches: 'Cancel'
})
bot.dialog('GreetingDialog',
(session) => {
var responses = [
'Hey there! How may I help you?',
'Hi, let\'s get started, shall we?',
'Hey, what do you want to do?',
'Hello. How may I assist you?',
'Hi. What can I do for you?'
]
session.send('\uD83D\uDC4B ' /* waving hand emoji */ + utils.choose(responses))
session.endConversation()
}
).triggerAction({
matches: 'Greeting'
})
bot.dialog('ThanksDialog',
(session) => {
var responses = [
'You\'re welcome.',
'No problem!',
'Nice. Can I help you with something else?'
]
session.send(utils.choose(responses))
session.endConversation()
}
).triggerAction({
matches: 'Thanks'
})
bot.dialog('DocumentUploadDialog',
(session) => {
if (hasMessageAttachment(session)) {
session.send('Thank you for `%s`', session.message.attachments[0].contentUrl)
} else {
session.send(
'You can drop me a file at any time with the attachment-button in the lower left corner.'
)
}
session.endConversation()
}
).triggerAction({
matches: 'Document.Upload'
})
var documentSpecifiers = {
'by Keyword': {
'handler': 'FetchDocumentByKeywordDialog'
},
'by Entity': {
'handler': 'FetchDocumentByEntityDialog'
},
'by Place': {
'handler': 'FetchDocumentByPlaceDialog'
},
'by Type': {
'handler': 'FetchDocumentByTypeDialog'
}
}
bot.dialog('FetchDocumentsDialog', [
function (session, args, next) {
var responses = [
'okay, lets get the documents.',
'ok, I will fetch them for you.',
'sure, I will get them right away.',
'okay, lets fetch them quickly.',
'ok, documents are coming right up.'
]
session.send(utils.choose(responses))
builder.Prompts.choice(session,
'How do you want to specify the documents?', documentSpecifiers,
{ listStyle: builder.ListStyle.button }
)
},
function (session, results) {
var handlerDialog = documentSpecifiers[results.response.entity].handler
session.replaceDialog(handlerDialog)
}
]).triggerAction({
matches: 'Documents.Fetch'
})
bot.dialog('FetchDocumentByKeywordDialog', [
function (session, args, next) {
let keywordFromIntent = null
if (args && args.intent) {
keywordFromIntent = builder.EntityRecognizer.findEntity(args && args.intent.entities,
'Document.Keyword'
)
}
session.dialogData.keyword = keywordFromIntent ? keywordFromIntent.entity : null
if (!session.dialogData.keyword) {
builder.Prompts.text(session, 'For which Keyword should I search documents?')
} else {
next()
}
},
function (session, result) {
if (result.response) {
session.dialogData.keyword = result.response
}
api.getDocumentsByKeyword(session, session.dialogData.keyword)
session.endDialog()
}
]).triggerAction({
matches: 'Documents.Fetch.ByKeyword'
})
bot.dialog('FetchDocumentByEntityDialog', [
function (session, args, next) {
let entityFromIntent = null
if (args && args.intent) {
entityFromIntent = builder.EntityRecognizer.findEntity(args && args.intent.entities,
'Document.Entity'
)
}
session.dialogData.entity = entityFromIntent ? entityFromIntent.entity : null
if (!session.dialogData.entity) {
builder.Prompts.text(session, 'For which Entity should I search documents?')
} else {
next()
}
},
function (session, result) {
if (result.response) {
session.dialogData.entity = result.response
}
api.getDocumentsByEntity(session, session.dialogData.entity)
session.endDialog()
}
]).triggerAction({
matches: 'Documents.Fetch.ByEntity'
})
bot.dialog('FetchDocumentByPlaceDialog', [
function (session, args, next) {
let placeFromIntent = null
if (args && args.intent) {
placeFromIntent = builder.EntityRecognizer.findEntity(args && args.intent.entities,
'Document.Place'
)
}
session.dialogData.place = placeFromIntent ? placeFromIntent.entity : null
if (!session.dialogData.place) {
builder.Prompts.text(session, 'For which Place should I search documents?')
} else {
next()
}
},
function (session, result) {
if (result.response) {
session.dialogData.place = result.response
}
api.getDocumentsByPlace(session, session.dialogData.place)
session.endDialog()
}
]).triggerAction({
matches: 'Documents.Fetch.ByPlace'
})
bot.dialog('FetchDocumentByTypeDialog', [
function (session, args, next) {
let typeFromIntent = null
if (args && args.intent) {
typeFromIntent = builder.EntityRecognizer.findEntity(args && args.intent.entities,
'Document.Type'
)
}
session.dialogData.type = typeFromIntent ? typeFromIntent.entity : null
if (!session.dialogData.type) {
builder.Prompts.text(session, 'Which document types should I search?')
} else {
next()
}
},
function (session, result) {
if (result.response) {
session.dialogData.type = result.response
}
api.getDocumentsByDoctype(session, session.dialogData.type)
session.endDialog()
}
]).triggerAction({
matches: 'Documents.Fetch.ByType'
})
//--- "INTERNAL" CALLBACKS FOR CARD BUTTONS
bot.dialog('InternalResultsDialog', [
function (session, args, next) {
if (args.length == 0) {
session.send('\uD83D\uDE41 ' /* frowning emoji */ + 'Sorry, no results available.')
session.endDialog()
} else {
let msg = new builder.Message(session)
let attachments = []
msg.attachmentLayout(builder.AttachmentLayout.carousel)
for (var i = 0; i < args.length; i++) {
let element = args[i]
let linkElements = element.link.split('/')
let title = linkElements[linkElements.length - 1]
let date = element.date
let pagesText = element.pages + ' page' + (element.pages > 1 ? 's' : '')
let sender = element.author
attachments.push(new builder.HeroCard(session)
.title(title)
.subtitle(
'added on ' + date +
' \u25AA ' + /* small black square */
pagesText
)
.text('sent by ' + sender)
.buttons([
builder.CardAction.postBack(session, '\u25AAF' + element.id, 'Show File'),
builder.CardAction.postBack(session, '\u25AAS' + element.id, 'Show Summary')
])
)
}
msg.attachments(attachments)
session.send(msg).endDialog()
}
}
])
bot.dialog('InternalDownloadDialog', [
function (session, args, next) {
let id = args.intent.matched[0].substr(2)
session.send('Opening file in the display...')
api.sendPdfDisplay(id)
session.endConversation()
}
]).triggerAction({ matches: /(\u25AAF)(.)*/i })
bot.dialog('InternalSummaryForwardDialog', [
function (session, args, next) {
let id = args.intent.matched[0].substr(2)
api.getAndSendSummary(session, id)
}
]).triggerAction({ matches: /(\u25AAS)(.)*/i })
bot.dialog('InternalSummaryDialog', [
function (session, text, next) {
var msg = new builder.Message(session)
.text('Here is the short version of it: *%s*', text)
.textFormat('markdown')
session.send(msg)
session.endConversation()
}
])