concept request in category alexa

This is an excerpt from Manning's book Voice Applications for Alexa and Google Assistant.
A slot is like a matching group inside a regular expression. The Alexa platform looks at the values the skill developer has provided as those that should show up in this space. Sports fan Molly’s phrase doesn’t match up with “yesterday” or “last week” or “the other day,” but there it is: “last night.” We’ve got a match. Alexa has all the information it needs from what Molly said: the intent (
ResultsIntent) and the slot value (last night). It passes these, and information about the session, to the code that the skill developer has written to handle requests.
Listing 6.2 Creates
LaunchRequestHandler(./lambda/custom/index.js)const Alexa = require("ask-sdk"); const AWS = require("aws-sdk"); const LaunchRequestHandler = { canHandle(handlerInput) { return handlerInput.requestEnvelope.request.type === "LaunchRequest"; #1 }, handle(handlerInput) { const speech = "The name info skill needs you to just ask for a first name."; #2 const reprompt = "Try saying, 'give me facts about the name Dustin.'"; #2 return handlerInput.responseBuilder .speak(speech) .reprompt(reprompt) .getResponse(); } }; const skillId = "<YOUR SKILL ID>"; exports.handler = Alexa.SkillBuilders.standard() .addRequestHandlers( LaunchRequestHandler #3 ) .withSkillId(skillId) #4 .withTableName("name_info") .withAutoCreateTable(true) .withDynamoDbClient( new AWS.DynamoDB({ apiVersion: "latest", region: "us-east-1" }) ) .lambda(); #1 Matches the launch request type #2 Provides guidance on skill abilities #3 Registers the handler #4 Verifies that the request matches the skill ID you can get from the Developer Console
The threshold that determines whether a request is recent enough will vary based on the skill. A gaming skill with quick rounds will have a lower threshold than a baseball-scores skill, where games last for hours.
Listing 6.6 A function for determining value recency
function recentlyRequested(data, recentKey) { const decay = 10; const decayedAgo = new Date(Date.now() - 1000 * 60 * decay); if ( #1 data[recentKey] && #1 data[recentKey].lastRequested && #1 decayedAgo < new Date(data[recentKey].lastRequested) #1 ) { #1 return data[recentKey]; #1 } #1 }; #1 Checks to see if the user last requested the attribute before the threshold for “forgetting”The
recentlyRequestedfunction looks at the data we’ve stored about the user to see if the user requested the attribute, and, if so, whether that request hasn’t yet gone stale. In this case, that time comes when 10 minutes have passed. This amount of time is a static limit, but you could have a dynamic threshold. If a user asks for a bus, you could set the request to go stale not long after the next bus arrives. As you’ve seen, the goal is consistency and intuitiveness. You can’t rely on a countdown clock to show the user when the request is going stale, so choose a threshold that a user can understand without direction.
Any actions that are shared among the handlers could be done in the parts of the flow that are common to all handlers. These commonalities are when the request comes in and when the response heads out.
Figure 6.3 Flow of data inside the Name Info skill flow for
SpellingIntent, with request and response interceptors![]()
Interceptors, seen in figure 6.3, change the information flow to either take in the request before it hits the handler, or take in the response before it’s sent back to Alexa. By introducing these extra steps, the skill can prepare data or call methods that need to be shared across handlers. On the back side, the skill can perform any closing tasks or clean up before the invocation ends. For example, the request interceptor could introduce logging or prepare localization. The response interceptor could be used to centralize the code to save persistent data.