This repository has been archived by the owner on Jan 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
114 lines (108 loc) · 4.28 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const builder = require('botbuilder')
let lib = new builder.Library('FormBuilder')
lib.dialog('/', [
(session, args) => {
if (args !== null && typeof args !== 'undefined') {
if (!args.questions) {
return session.error('Missing questions array')
} else {
session.dialogData.questions = args.questions
}
session.dialogData.entities = args.entities ? args.entities : false
session.dialogData.index = args.index ? args.index : 0
session.dialogData.form = args.form ? args.form : {}
session.dialogData.reprompt = args.reprompt ? args.reprompt : false
} else {
return session.error('Arguments were not supplied or were undefined')
}
let index = session.dialogData.index
let questions = session.dialogData.questions
// Check if entities exists
if (session.dialogData.entities) {
// If the entities exists and it possesses the property for this question, send a confirm prompt
if (session.dialogData.entities.hasOwnProperty(questions[index].field)) {
var prompt = questions[session.dialogData.index].prompt
// Replace placeholder text with user data
prompt = prompt.replace('{' + questions[index].field + '}', session.dialogData.entities[questions[index].field])
builder.Prompts.confirm(session, prompt)
} else {
if (!session.dialogData.reprompt) {
builder.Prompts.text(session, questions[index].question)
} else {
// If reset boolean is true, send reprompt text
delete session.dialogData.reprompt
builder.Prompts.text(session, questions[index].repromptText)
}
}
} else {
if (!session.dialogData.reprompt) {
builder.Prompts.text(session, questions[index].question)
} else {
// If reset boolean is true, send reprompt text
delete session.dialogData.reprompt
builder.Prompts.text(session, questions[index].repromptText)
}
}
},
(session, results) => {
let questions = session.dialogData.questions
let currentQuestion = questions[session.dialogData.index]
if (typeof results.response !== 'string') {
if (!results.response) {
var field = questions[session.dialogData.index].field
delete session.dialogData.entities[field]
} else if (currentQuestion.hasOwnProperty('validation')) {
let validation = new RegExp(currentQuestion.validation)
if (validation.test(session.dialogData.entities[currentQuestion.field])) {
field = questions[session.dialogData.index++].field
session.dialogData.form[field] = session.dialogData.entities[field]
} else {
field = questions[session.dialogData.index].field
session.dialogData.reprompt = true
delete session.dialogData.entities[field]
}
} else {
field = questions[session.dialogData.index++].field
session.dialogData.form[field] = session.dialogData.entities[field]
}
} else {
if (currentQuestion.hasOwnProperty('validation')) {
let validation = new RegExp(currentQuestion.validation)
if (validation.test(results.response)) {
field = questions[session.dialogData.index++].field
session.dialogData.form[field] = results.response
} else {
field = questions[session.dialogData.index].field
session.dialogData.reprompt = true
}
} else {
field = questions[session.dialogData.index++].field
session.dialogData.form[field] = results.response
}
}
// Check for end of form
if (session.dialogData.index >= questions.length) {
// Return to original dialog
session.endDialogWithResult({formData: session.dialogData.form})
} else {
// Next field
session.replaceDialog('/', session.dialogData)
}
}
])
module.exports.createLibrary = () => {
return lib.clone()
}
module.exports.entityCheck = (entities, requirements, entityThreshold, callback) => {
var result = {}
for (var entity in entities) {
if (entities[entity].score >= entityThreshold) {
for (var requirement in requirements) {
if (entities[entity].type === requirements[requirement]) {
result[requirements[requirement]] = entities[entity].entity
}
}
}
}
callback(result)
}