-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathops.js
More file actions
140 lines (132 loc) · 5.4 KB
/
Copy pathops.js
File metadata and controls
140 lines (132 loc) · 5.4 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
const { getStatus } = require('./systemstatus');
const launchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
handle(handlerInput) {
const speechText = 'hi, you can ask, whats the current status or what is the status of a service or Whats the status of all services, for example, what is the status of unified <say-as interpret-as="interjection">a p i</say-as>. Which one would you like to try?';
return handlerInput.responseBuilder
.speak(speechText)
.reprompt('Do you want to know status of a service, just say, What is the status of unified a p i')
.getResponse();
}
};
// System Status
const systemStatusIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'systemstatus';
},
async handle(handlerInput) {
let speechText = "<emphasis level=\"strong\">its all good!</emphasis>" +
"<say-as interpret-as=\"interjection\">woo hoo</say-as>!" ;
const response = await getStatus();
if (response === 'unhealthy') {
speechText = "<emphasis level=\"strong\">Huston! we have a problem</emphasis>, to find out which service has a problem, just say 'Whats the status of all services?'";
}
return handlerInput.responseBuilder
.speak(speechText)
.getResponse();
}
};
// Specific Service Status by Slot
const serviceStatusIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'servicestatus';
},
handle(handlerInput) {
console.log('service status - handle');
const responseBuilder = handlerInput.responseBuilder;
const filledSlots = handlerInput.requestEnvelope.request.intent.slots;
const slotValues = getSlotValues(filledSlots);
console.log(`slot:${JSON.stringify(slotValues)}`);
let speechText = "Sorry, I can't find the service";
if (slotValues.servicename.isValidated) {
// TODO add code to check for specfic status of a service
speechText = `The system status of ${slotValues.servicename.resolved} is <emphasis level="strong">healthy</emphasis>`;
}
return handlerInput.responseBuilder
.speak(speechText)
.getResponse();
}
}
// All Services
const allServicesStatusIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'allstatus';
},
async handle(handlerInput) {
console.log('all services - handle');
const response = await getStatus();
if (response === 'unhealthy') {
const speechText = "<emphasis level=\"strong\">Huston! we have a problem</emphasis> standby!'";
return handlerInput.responseBuilder
.speak(speechText)
.getResponse();
}
const checks = response.healthcheck.checks;
let statusText = '';
for(var i=0;i< checks.length;i++) {
//statusText+= `${checks[i].keys}`;
const data = checks[i];
const keys = Object.keys(data);
for (const key of keys) {
console.log(`key:${key} -- ${data[key].message}`);
if (data[key].message !== undefined) {
statusText+= `${key} is ${data[key].message} <break strength="strong"/> `;
}
}
}
console.log(`txt:${statusText}`);
return handlerInput.responseBuilder
.speak(statusText)
.getResponse();
}
}
// Helper Function
function getSlotValues(filledSlots) {
const slotValues = {};
console.log(`The filled slots: ${JSON.stringify(filledSlots)}`);
Object.keys(filledSlots).forEach((item) => {
const name = filledSlots[item].name;
if (filledSlots[item] &&
filledSlots[item].resolutions &&
filledSlots[item].resolutions.resolutionsPerAuthority[0] &&
filledSlots[item].resolutions.resolutionsPerAuthority[0].status &&
filledSlots[item].resolutions.resolutionsPerAuthority[0].status.code) {
switch (filledSlots[item].resolutions.resolutionsPerAuthority[0].status.code) {
case 'ER_SUCCESS_MATCH':
slotValues[name] = {
synonym: filledSlots[item].value,
resolved: filledSlots[item].resolutions.resolutionsPerAuthority[0].values[0].value.name,
isValidated: true,
};
break;
case 'ER_SUCCESS_NO_MATCH':
slotValues[name] = {
synonym: filledSlots[item].value,
resolved: filledSlots[item].value,
isValidated: false,
};
break;
default:
break;
}
} else {
slotValues[name] = {
synonym: filledSlots[item].value,
resolved: filledSlots[item].value,
isValidated: false,
};
}
}, this);
return slotValues;
}
module.exports.Ops = {
launchRequestHandler,
systemStatusIntentHandler,
serviceStatusIntentHandler,
allServicesStatusIntentHandler
}