This repository was archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
180 lines (149 loc) · 5.93 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
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
'use strict';
const optimizelySDK = require('@optimizely/optimizely-sdk'),
defaultLogger = require('@optimizely/optimizely-sdk/lib/plugins/logger'),
Alexa = require('ask-sdk'),
rp = require('request-promise');
// Setup undefined Optimizlely object
// We'll define this later, and will be temporarily cached by Amazon
let optimizelyClientInstance;
// Function to get datafile and initialize Optimizely client
let initOptimizely = (projectID) => {
// Options to get the datafile via CDN
let options = {
uri: `https://cdn.optimizely.com/json/${projectID}.json`,
json: true
}
console.log('Initializing Optimizely Client');
// Options to get the datafile via REST api
// let options = {
// uri: `https://www.optimizelyapis.com/experiment/v1/projects/${projectID}/json`,
// headers: {
// 'Authorization': 'Bearer ' + process.env.OPTIMIZELY_TOKEN
// },
// json: true
// };
// Returns promise to make GET request to Optimizely API
return rp(options)
.then( (datafile) => {
console.log('Here\s the datafile: ', datafile);
optimizelyClientInstance = optimizelySDK.createInstance(
{
datafile: datafile, // Datafile returned from request
logger: defaultLogger.createLogger({ logLevel: 1 }) // Default logger provided by SDK
}
)
})
.catch( (err) => {
console.log('There was an error: ', err);
})
};
// Here are the responses we'll give when a user either accepts or denies our confirmation
let confirmationResponse = "Great! I'll send you more information",
deniedResponse = "Ok, maybe next time";
const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
handle(handlerInput) {
let speechOutput = "You've kicked off the Optimizely Full Stack demo. Say a phrase that's mapped to one of my intents to get a variation of a test.";
return handlerInput.responseBuilder
.speak(speechOutput)
.reprompt(speechOutput)
.getResponse();
}
};
// Alexa intent where we're going to test our responses!
const OptimizelyDemoHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'OptimizelyDemo';
},
// Use async incase we need to get a new datafile - this is Node after all!
async handle(handlerInput) {
// Set the userId as the session ID from the Alexa Skill
// This will get a new user ID on each session - alternatively use deviceId
let userId = handlerInput.requestEnvelope.session.sessionId;
let speechText = 'Well, this is awkward I don\'t have anything to say';
let status = handlerInput.requestEnvelope.request.intent.confirmationStatus;
// Check to see the status of the intent
if( status === "NONE"){
// Check to see if Optimizely Client exists...
// If it doesn't we'll make a request to the REST api (most up to date), and init the client
if(!optimizelyClientInstance){
await initOptimizely(process.env.PROJECT_ID);
}
/***
OPTIMIZELY FEATURE FLAGS GO HERE
***/
let enabled = optimizelyClientInstance.isFeatureEnabled('personal_stylist', userId);
let response = optimizelyClientInstance.getFeatureVariableString('personal_stylist', 'response', userId);
// Send back response with feature variable if applicable
return handlerInput.responseBuilder
.speak(enabled ? response : speechText)
.addConfirmIntentDirective(handlerInput.requestEnvelope.request.intent)
.getResponse();
// How to respond if a user accepts my confirmation question
} else if ( status === "CONFIRMED" ){
// Send tracking event to Optimizely
optimizelyClientInstance.track('respondedYes', userId);
return handlerInput.responseBuilder
.speak(confirmationResponse)
.withShouldEndSession(true)
.getResponse();
// Check to see if the user rejected the prompt
} else if ( status === "DENIED" ) {
// Track event that the user rejected the recommendation
optimizelyClientInstance.track('respondedNo', userId);
return handlerInput.responseBuilder
.speak(deniedResponse)
.withShouldEndSession(true)
.getResponse();
}
}
}
const HelpIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const speechText = 'You can say hello to me!';
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withSimpleCard('Hello World', speechText)
.getResponse();
}
};
const CancelAndStopIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent'
|| handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
console.log('stop request USER ', userId);
const speechText = 'Goodbye!';
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Hello World', speechText)
.getResponse();
}
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
//any cleanup logic goes here
return handlerInput.responseBuilder.getResponse();
}
};
let skill;
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(LaunchRequestHandler,
OptimizelyDemoHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler)
.lambda();