Voice User Interface || Lambda Function || Connect VUI to Code || Testing || Customization || Intents and Slots || Smart Recommendations || Publication
When the user says "go outside", the GoOutIntent
intent is called and the code in the GoOutIntent handler block is executed.
This makes an API call over the Internet to the Yahoo Weather service, which returns the weather and current time in your city.
You can enhance the GoOutIntent
handler code (around line 185) to make a relevant activity suggestion to the user.
For example, add logic to decide, based on current time and weather conditions, whether to:
- Go out to a local beach or park
- Recommend a movie theatre or mall
- Attend a scheduled public event happening soon
- Staying home to watch a movie on Amazon Prime
- etc..
-
Test ‘go outside’ skill in simulator first. You should get a standard response giving you the time and weather conditions in your city.
-
Go to your lambda function
-
Scroll through your code to one of the last handlers, called
GoOutIntent
-
Now, paste this code into your GoOutIntent:
var AMPM = localTime.substr(-2);
console.log(AMPM);
var hour = parseInt(localTime.split(':').shift());
if(AMPM == "PM" && hour < 12) { hour = hour + 12; }
console.log(hour);
var suggestion = 'Read a book.';
console.log(suggestion);
if(hour < 7 ) {suggestion = 'Sleep.'; }
if(hour >= 7 && hour < 12) {suggestion = 'Ask me for a breakfast recommendation.'; }
if(hour >= 12 && hour < 14) {suggestion = 'Ask me for a lunch recommendation.'; }
if(hour >= 17 && hour < 20) {suggestion = 'Ask me for a dinner recommendation.'; }
if(hour >= 22) {suggestion = 'Go to bed.'; }
if(hour >= 20 && hour < 22) {
if(['Rain', 'Shower', 'Thunderstorms'].indexOf(currentCondition) > -1) {
suggestion = 'Stay home and watch a movie on Amazon Prime since it is wet outside.';
} else {
suggestion = 'Check out what is playing at the Cineplex movie theater on 123 Main St.';
}
}
if (['Sunny'].indexOf(currentCondition) > -1 -1 && currentTemp > 75 && hour < 11) {suggestion = 'Plan a day at the beach, as it is sunny and warm today.'}
console.log(suggestion);
this.emit(':tell', 'It is ' + localTime
+ ' and the weather in ' + data.city
+ ' is '
+ currentTemp + ' and ' + currentCondition
+ '. I suggest you ' + suggestion);
-
What we have just done is create some logic that matches possible outcomes to a custom response for our users.
-
Now click Save, then click Test. Our test should pass.
-
Go to the developer portal.
-
Type 'go outside' into the Enter Utterance field found in the Service Simulator and press enter
-
Alexa should give us a customized recommendation according to your code.
What if we wanted to get info about local movie showtimes? Try adding another API call, maybe one that checks movie showtimes for a local theater, or something creative that you come up with yourself!
When you are ready, proceed to learn about design for Alexa.