Reputation: 53
Ok, this needs elaboration: First of all, when I ask for the intent "DeparmentLocation", in developer.amazon.com it works fine. When I use the real device it calls 'unhandledIntent'. Second of all, it does work when it is re-prompted, like so: "Hey Alexa, ask invocation name." and not say anything until it replies with the unhandled message. Then I could say the utterance normally and it would respond. Third and lastly, it is working with the "facilityLocation" intent. I'm not sure what's going on or what is different. Here is the code. Thanks!
var doc = require('dynamodb-doc');
var db = new doc.DynamoDB();
var Alexa = require('alexa-sdk');
exports.handler = function(event, context) {
var alexa = Alexa.handler(event, context);
var handlers = {
'sessionStartedRequest' : function(){
this.emit(':talk', "welcome.")
},
'AMAZON.StopIntent': function() {
this.emit(':tell', "Goodbye!");
},
'AMAZON.CancelIntent': function() {
this.emit(':tell', "Goodbye!");
},
'Unhandled': function() {
this.emit(':ask', "I'm sorry. I didn't get that. Could you repeat it?", "Ask me a question.");
},
'SessionEndedRequest': function() {
this.emit(":tell", "Goodbye!");
},
'DepartmentLocationIntent': function () {
var name = "";
if (this.event.request.intent.slots.department.value) {
name = this.event.request.intent.slots.department.value.toLowerCase().trim();
}
var key = {
'name': name
};
var tableName = "Department";
var params = {
TableName: tableName,
Key: key,
ProjectionExpression: 'loc'
};
db.getItem(params, function (err, data) {
//alexa.emit(":tell", name);
if (err) {
console.log(err);
//alexa.emit(':tell', "Sorry! I did not catch that!");
alexa.emit(':tell', err);
} else {
var loc = JSON.stringify(data.Item.loc);
alexa.emit(':tell', "The " + name + " department is in " + loc);
}
}, context.done);
},
'FacilityLocationIntent': function() {
var name = "";
if (this.event.request.intent.slots.facility.value) {
name = this.event.request.intent.slots.facility.value.toLowerCase().trim();
}
var key = {
'name': name
};
var tableName = "Facility";
var params = {
TableName: tableName,
Key: key,
ProjectionExpression: 'loc'
};
db.getItem(params, function(err, data) {
if (err) {
console.log(err);
alexa.emit(':tell', "Sorry! This facility does not exist!");
} else {
var response = JSON.stringify(data.Item.loc);
alexa.emit(':tell', name + " is located in " + response);
}
}, context.done);
}
};
alexa.registerHandlers(handlers);
alexa.execute();
};
`
Upvotes: 1
Views: 156
Reputation: 545
bdeir
I don't know if you are still seeking a solution but I think your problem is the nesting of your code. try and keep stuff separated out of the ‘exports.handler’ as it is cleaner and easier to use as said by AWS lambda guidelines. So try this:
exports.handler = function(event, context, callback){
var alexa = Alexa.handler(event, context, callback);
alexa.registerHandlers(handlers);
alexa.execute();
};
And then underneath the different handlers
var handlers = {
'sessionStartedRequest' : function(){
this.emit(':talk', "welcome.")
},
'AMAZON.StopIntent': function() {
this.emit(':tell', "Goodbye!");
},
'AMAZON.CancelIntent': function() {
this.emit(':tell', "Goodbye!");
},
'Unhandled': function() {
this.emit(':ask', "I'm sorry. I didn't get that. Could you repeat it?", "Ask me a question.");
},
'SessionEndedRequest': function() {
this.emit(":tell", "Goodbye!");
},
'DepartmentLocationIntent': function () {
var name = "";
if (this.event.request.intent.slots.department.value) {
name = this.event.request.intent.slots.department.value.toLowerCase().trim();
}
var key = {
'name': name
};
var tableName = "Department";
var params = {
TableName: tableName,
Key: key,
ProjectionExpression: 'loc'
};
db.getItem(params, function (err, data) {
//alexa.emit(":tell", name);
if (err) {
console.log(err);
//alexa.emit(':tell', "Sorry! I did not catch that!");
alexa.emit(':tell', err);
} else {
var loc = JSON.stringify(data.Item.loc);
alexa.emit(':tell', "The " + name + " department is in " + loc);
}
}, context.done);
},
'FacilityLocationIntent': function() {
var name = "";
if (this.event.request.intent.slots.facility.value) {
name = this.event.request.intent.slots.facility.value.toLowerCase().trim();
}
var key = {
'name': name
};
var tableName = "Facility";
var params = {
TableName: tableName,
Key: key,
ProjectionExpression: 'loc'
};
db.getItem(params, function(err, data) {
if (err) {
console.log(err);
alexa.emit(':tell', "Sorry! This facility does not exist!");
} else {
var response = JSON.stringify(data.Item.loc);
alexa.emit(':tell', name + " is located in " + response);
}
}, context.done);
}
};
Hope this solves your problem.
Upvotes: 1