qnabot-on-aws
qnabot-on-aws copied to clipboard
Feature Request: Alexa custom LaunchRequest welcome message
Currently the Alexa welcome message is delivered in the first step of the fulfillment middleware /lambda/fulfillment/lib/middleware/1_parse.js
. Before the userInfo object has been processed is step 2 /lambda/fulfillment/lib/middleware/2_preprocess.js
.
Amazon's 'Situational Design' best practices encourages Alexa skills to respond differently, in context, between; first-time users, return users, logging in users with a name etc.
To do this the logic to construct the welcome message needs to happen after the userInfo object is instantiated. I've temporarily fixed this by removing the LaunchRequest handler from the alexa middleware, alexa.parse()
and moving to to a new method alexa.handleLaunchRequest()
. Then created a new step in the middleware lambda/fulfillment/lib/middleware/3.1_welcome.js
which checks for 'Alexa' and runs alexa.handleLaunchRequest()
Example of 3.1_welcome.js

module.exports = async function handleLaunchRequest(req,res) {
if (req._type === 'ALEXA') {
alexa.handleLaunchRequest(req, res);
return;
}
return {req, res}
}
Example of alexa.handleLaunchRequest()
exports.handleLaunchRequest=function(req,res){
var event = req._event
switch(_.get(event,"request.type")) {
case "LaunchRequest":
var interactionCount = _.get(req,'_userInfo.InteractionCount', 0)
var userGreeting = (interactionCount < 1) ? "New User" : "Return User";
var welcome_message = `Hello ${userGreeting}, Please ask a question.`;
throw new Respond({
version: '1.0',
response: {
outputSpeech: {
type: "PlainText",
text: welcome_message
},
card: {
type: "Simple",
title: "Welcome",
content: welcome_message
},
shouldEndSession: false
}
});
break;
}
}
As the actual code is quite specific to individual use-cases and because I don't want to rename all the middleware steps I haven't created a pull request. The ideal solution would probably be to have a custom 'Welcome' topic in the Content Designer that can be dynamic, similar to the greetings example at bottom of page here. Thoughts?
Actually, it can be easily changed to a dynamic message by editing
/lambda/fulfillment/lib/middleware/alexa.js
from
...
var welcome_message = _.get(req,'_settings.DEFAULT_ALEXA_LAUNCH_MESSAGE', 'Hello, Please ask a question');
switch(_.get(event,"request.type")){
case "LaunchRequest":
throw new Respond({
version:'1.0',
response:{
outputSpeech:{
type:"PlainText",
text: welcome_message
},
card: {
type: "Simple",
title: "Welcome",
content:welcome_message
},
shouldEndSession:false
}
});
break;
to
...
switch(_.get(event,"request.type")){
case "LaunchRequest":
// Set question to 'welcome' to get a custom welcome message
// Make sure the Content Designer has a Welcome Question defined
out.question="welcome"
break;
Then make sure there is a item in the ContentDesigner Knowledge Base with id:Welcome and question:welcome. followed by an answer like
{{!-- pick a random answer from list below --}}
{{randomPick
"Greetings"
"Hi there"
"Howdy"
"Hello"
"Whassup"
}}
{{!-- respond with users name if known --}}
{{#if UserInfo.GivenName}}
{{UserInfo.GivenName}}!
{{else}}
friendly human!
{{/if}}
{{!-- Conditional welcome back message --}}
{{#ifCond UserInfo.InteractionCount '<' 1}}
I have lots of information for you to discover about XXX.
{{/ifCond}}
Ask me a question, or let me know if you want to hear the different topics I have information for?