-
Notifications
You must be signed in to change notification settings - Fork 2
/
getUserName.js
75 lines (57 loc) · 1.62 KB
/
getUserName.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
/**
* dialog unit: getUserName
*
* the dialog prompt the user asking his name, reply with an hello and end.
*
* askName: is the (output state) entry point of the dialog
*
* getName: is the input state that elaborate th user sentence.
* when the user reply a valid name, the dialog end.
*
*/
// get naif dialog manager DSL statements
const { say, ask, next, end, match, slots, take } = require('../../')
// define states of current dialog unit
const getUserName = { askName, getName }
// defines tags
//const askNameState = 'getUserName.askName'
const getNameState = 'getUserName.getName'
/**
* askName
* output state handler, dialog entry point (start)
*/
function askName() {
say( 'Hello!' )
ask( 'What\'s your name?' )
next(getNameState)
}
/**
* getName
* input state handler
*
* @param {requestDataObject} request
*/
function getName(request) {
// create a regexp rule to get a first name (e.g. Anna Lisa, Giorgio La Malfa)
const firstNameReg = '(?<firstName>[A-Z][a-z]{1,}(\\s[A-Z][a-z]{1,})*)'
const introNameReg = '^(my name is |name me |I\'m )?'
const FIRSTNAME_REGEX = new RegExp ( introNameReg + firstNameReg )
switch ( take(request) ) {
// pattern 1
case match( /hi|hello|good morning|good afternoon/i ):
say( 'Hi there!' )
ask( 'What\'s your name?' )
break
// pattern 2
case match( FIRSTNAME_REGEX ):
say( `Nice to meet you, ${slots().firstName}!` )
end()
break
// fallback pattern
default:
say( 'I do not understand.' )
ask( 'What is your name?' )
break
}
}
module.exports = getUserName