When building your voice experience, think about continued engagement with your customers. Make sure your skill will delight users on their tenth visit just as much as it did on their first.
One way to ensure that users will want to keep interacting with your Alexa skill is to provide dynamic content. When you first get started building Alexa skills, it is perfectly fine to hardcode your skill’s data directly into the skill. But one thing you're likely to discover over time is that once the users exhaust your finite data set, engagement will start to drop off. (Imagine a fact skill that repeats the same 20 facts, or a trivia skill that asks the same few questions over and over again.)
One way to ensure that engagement stays high is to set up your skill so that it pulls its data from an external source, like a website or a database. As an added benefit, you'll be able to update the content you deliver to the user without having to update your skill's code since the data is loaded from outside and consumed by the skill at runtime.
Say, for example, you were building a skill that told the user the current price of a crypto currency. If you were to hardcode that value, the price that your skill would return to the user would most certainly be outdated by the time they enabled your skill and invoked it. This would likely hurt your skill's engagement.
To ensure that you're always providing the most up-to-date information, you can teach your skill to look up the information when the user requests it using a RESTful web service.
RESTful web services are based on Representational State Transfer, REST, which is a protocol for creating networked applications and much of the web relies on them. In fact your browser relies on REST to GET the webpage that you entered into your browser from the server it is stored on. (For a brief introduction, see the REST entry on Wikipedia.)
Here’s how a RESTful web service can boost your skill. To hook into any webservice from your Alexa skill, you can use the following helper function:
CONST https = require('https');
CONST HOST = 'replacewithyourownhost.com';
CONST PATH = '/change/this/path/to/whatever/your/webservice/path/is';
// https is a default part of Node.JS. Read the developer doc: https://nodejs.org/api/https.html
function httpsGet(callback) {
// GET is a web service request that is fully defined by a URL string
// Try GET in your browser:
// Update these options with the details of the web service you would like to call
var options = {
host: HOST,
path: PATH,
method: 'GET',
};
var req = https.request(options, res => {
res.setEncoding('utf8');
var returnData = "";
res.on('data', chunk => {
returnData = returnData + chunk;
});
res.on('end', () => {
var jsonData = JSON.parse(returnData);
// this will execute whatever function the caller defined,
// and pass the data we received from the webservice call
// to it. In your call back you'll need handle the data
// and make Alexa speak.
callback(jsonData);
});
});
req.end();
}
From your skill's intent, you would then call httpsGet. You will want to change prices.USD.selling to match the data that comes back from the webservice that you use.
'LookUpPriceIntent': function() {
httpGet((prices) => {
this.response.speak(`The current price is ${prices.USD.selling}`);
this.emit(':responseReady');
});
}
Since httpsGet makes use of https.request which is asynchronous, you can't immediatly call this.speak and this.emit. Instead, you have to pass those two calls in a callback which is invoked after the data is retrieved from the server. The code below WILL NOT WORK.
'LookUpPriceIntent' : function() {
httpGet((prices) => {
this.response.speak(`The current price is ${prices.USD.selling}`);
}
// this will return before the next tick and prevent httpsGet from
// ever executing and will return an empty speech response from the skill
this.emit(':responseReady');
}
Our example deals with looking up crypto currency prices, but you can use this same code sample to pull any dynamic content you’d like. Imagine, for example, building a skill that pulls and shares inspirational quotes from a website. Or you could also pull in the weather based on the device location settings. You could even lookup the scores of your user's favorite sports team. Odds are they have a favorite team, so you could save it and look it up for them automatically especially if that's what they use your skill for.
In addition to any web services, you can also store your skill’s data anywhere in the cloud including Amazon S3 and Wordpress.
If you want to consume data from a web service in your Alexa skills, take a look at the cookbook entryfor external web service calls. If you are interested in storing your data in S3, please see the cookbook entry on reading from S3.