forked from salesforce-misc/violet-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiabetes-stoplight.js
220 lines (191 loc) · 6.7 KB
/
diabetes-stoplight.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
'use strict';
var violet = require('violet/lib/violet.js').script();
var violetClientTx = require('violet/lib/violetClientTx.js')(violet);
var violetTime = require('violet/lib/violetTime.js')(violet);
var violetSFStore = require('../lib/violetStoreSF.js')(violet);
violetSFStore.store.propOfInterest = {
'diabetesLog': ['user', 'timeOfCheckin', 'bloodSugarLvl', 'feetWounds', 'missedDosages']
}
const yCall = 'Take action today. Call: 123-456-7890';
const yBloodSugarLo = 70;
const yBloodSugarHi = 130;
const rCall = 'Call your doctor now: 555-555-5555';
const rBloodSugarLo = 70;
const rBloodSugarHi = 130;
violet.addInputTypes({
"bloodSugarLvl": "NUMBER",
});
//common across multiple goals
violet.addPhraseEquivalents([
]);
violet.addTopLevelGoal('checkIn');
violet.respondTo({
expecting: ['Check in', 'Can I check in', 'I would like to check in'],
resolve: (response) => {
response.say('Sure.');
response.addGoal('checkIn');
}});
violet.defineGoal({
goal: 'checkIn',
prompt: ['Did you check your blood sugar level today?'],
respondTo: [{
expecting: ['Yes', 'I tested my blood sugar level'],
resolve: (response) => {
response.say('Great.');
response.addGoal('checkInDetails');
}}, {
expecting: ['No', 'I cannot test my blood sugar level'],
resolve: (response) => {
response.addGoal('whyCannotTestBloodSugar');
}}]
});
violet.respondTo({
expecting: ['Is everything running well'],
resolve: function *(response) {
}
});
violet.defineGoal({
goal: 'checkInDetails',
resolve: function *(response) {
if (!response.ensureGoalFilled('timeOfCheckin')
|| !response.ensureGoalFilled('bloodSugarLvl')
|| !response.ensureGoalFilled('feetWounds')
|| !response.ensureGoalFilled('missedDosages') ) {
return false; // dependent goals not met
}
response.say('Thanks for checking in - I am logging the data for you.');
if (response.get('bloodSugarLvl') < rBloodSugarLo) {
response.say('Your blood sugar level is very low.');
response.say(rCall);
} else if (response.get('bloodSugarLvl') < yBloodSugarLo) {
response.say('Your blood sugar level is low.');
response.say(yCall);
}
if (response.get('bloodSugarLvl') > rBloodSugarHi) {
response.say('Your blood sugar level is very high.');
response.say(rCall);
} else if (response.get('bloodSugarLvl') > yBloodSugarHi) {
response.say('Your blood sugar level is high.');
response.say(yCall);
}
// if (response.get('timeOfCheckin') == 'before-my-meal') {
// } else {
// // 2hrs-after-my-meal
// }
if (response.get('feetWounds') == true) {
var diabetesLog = yield response.load('diabetesLog', 'user', response.get('userId'), 'CreatedDate = LAST_N_DAYS:14')
//console.log('load-results', diabetesLog);
var sevenDaysAgo = new Date();
sevenDaysAgo.setDate(sevenDaysAgo.getDate()-7);
var yWarn = true;
var rWarn = true;
diabetesLog.forEach((logItem)=>{
// we have already filtered for the last 14 days
if (logItem.feetWounds == false) rWarn = false;
var itemDate = new Date(logItem.CreatedDate);
if (itemDate.getTime() > sevenDaysAgo.getTime() && logItem.feetWounds == false) yWarn = false;
});
if (rWarn) {
response.say('Your feet wounds are an indicator of a big problem.');
response.say(rCall);
} else if (yWarn){
response.say('Your feet wounds are an indicator of a problem.');
response.say(yCall);
}
}
if (response.get('missedDosages') == 'yes') {
response.say('Your missed dosage is a problem.');
response.say(yCall);
// TODO: implement rCall for dosages
}
response.store('diabetesLog', {
'user': response.get('userId'),
'timeOfCheckin': response.get('timeOfCheckin'),
'bloodSugarLvl': response.get('bloodSugarLvl'),
'feetWounds': response.get('feetWounds'),
'missedDosages': response.get('missedDosages')
});
}});
violet.defineGoal({
goal: 'timeOfCheckin',
prompt: 'Was this before a meal, or 2 hours after a meal?',
respondTo: [{
expecting: ['Before', 'Before my meal'],
resolve: (response) => {
response.set('timeOfCheckin', 'before-my-meal');
}}, {
expecting: ['After', '2 hours after my meal'],
resolve: (response) => {
response.set('timeOfCheckin', '2hrs-after-my-meal');
}}]
});
violet.defineGoal({
goal: 'bloodSugarLvl',
prompt: 'What was your blood sugar level?',
respondTo: [{
expecting: ['My blood sugar level is [[bloodSugarLvl]]', '[[bloodSugarLvl]]'],
resolve: (response) => {
response.set('bloodSugarLvl', response.get('bloodSugarLvl') );
}}]
});
violet.defineGoal({
goal: 'feetWounds',
prompt: 'Do you have any wounds on your feet?',
respondTo: [{
expecting: ['No'],
resolve: (response) => {
response.set('feetWounds', false );
}}, {
expecting: ['Yes'],
resolve: (response) => {
response.set('feetWounds', true );
}}]
});
violet.defineGoal({
goal: 'missedDosages',
prompt: 'Did you miss any doses of medicine?',
respondTo: [{
expecting: ['No'],
resolve: (response) => {
response.set('missedDosages', false );
}}, {
expecting: ['Yes'],
resolve: (response) => {
response.set('missedDosages', true );
}}]
});
violet.defineGoal({
goal: 'whyCannotTestBloodSugar',
prompt: 'Are you out of strips, not sure how to test, sweaty, shaky, lightheaded, or confused?',
respondTo: [{
expecting: ['{I am|} out of strips', '{I have|} no strips'],
resolve: (response) => {
response.set('cannotTestBloodSugarReason', 'out-of-strips'); response.say(yCall);
}}, {
expecting: '{I am not sure|not sure|} how to test',
resolve: (response) => {
response.set('cannotTestBloodSugarReason', 'not-sure-how-to-test'); response.say(yCall);
}}, {
expecting: '{I am|} sweaty',
resolve: (response) => {
response.set('cannotTestBloodSugarReason', 'sweaty'); response.say(rCall);
}}, {
expecting: '{I am|} shaky',
resolve: (response) => {
response.set('cannotTestBloodSugarReason', 'shaky'); response.say(rCall);
}}, {
expecting: '{I am|} sweaty and shaky',
resolve: (response) => {
response.set('cannotTestBloodSugarReason', 'sweaty-and-shaky'); response.say(rCall);
}}, {
expecting: '{I am|} lightheaded',
resolve: (response) => {
response.set('cannotTestBloodSugarReason', 'lightheaded'); response.say(rCall);
}}, {
expecting: '{I am|} confused',
resolve: (response) => {
response.set('cannotTestBloodSugarReason', 'confused'); response.say(rCall);
}}]
});
violetTime.repeat(48*60, ()=>{ violet.addGoal('checkIn'); });
module.exports = violet;