Can someone maybe assist a noob here. The docs mention that the handlers can be in separate files and imported later. Can someone please show me the correct Nodey way of doing this. Im actually more of a Java dev, so im not sure what the correct syntax for this is... Callbacks or no callbacks, how to define the imports and exports its all a bit bewildering to me.
Defining all my handlers under a
const handlers = {
"LaunchRequest": function() {
this.emit('tell:':'Whazzzzuuuup')
},
"脰therIntent": function(){
this.emit(); //Lots of stuff in this function I want to have it in its own file
}
}
Makes the file really big. How to I split them out
Thanks!
@3uPh0riC ,
You can do
const handler1 = {
"LaunchRequest": function() {
this.emit('tell:':'Whazzzzuuuup')
},
"脰therIntent": function(){
this.emit(); //Lots of stuff in this function I want to have it in its own file
}
}
in one file, and
const handler2 = {
"脰therIntent": function() {
this.emit('tell:':'Whazzzzuuuup')
},
"脰therIntent": function(){
this.emit(); //Lots of stuff in this function I want to have it in its own file
}
}
in other file, and expose the handlers in the form of an array so that the Alexa.registerHandlers can be receive them.
const handlers = [handler1, handler2 .....]
In the index.js file, do
alexa.registerHandlers(handlers);
Yeah that doesnt work. Or im sure it would, if I understood how to import/export those constants. I tried various things and theres always an error along the lines of 脕rgument 1 is not an object
@3uPh0riC,
You have to make sure you are properly importing the handlers. Let's assume everything is inside a directory called "src" with another directory called "handlers" inside.
Assume I have two handlers in the directory "handlers"
The following file is called "newSession.js" which would have the path /src/handlers/newSession.js
const newSession = {
"LaunchRequest": function() {
this.response.speak("Welcome to my app");
this.emit(":responseReady");
}
// Insert whatever intents you would like in this handler
}
module.exports = newSession;
Assume this file is called "handler2.js" so it would have the path /src/handlers/handler2.js
const handler2 = {
"AMAZON.HelpIntent": function() {
this.response.speak("Do XYZ to get to point A");
this.emit(":responseReady");
}
// Insert whatever intents you would like in this handler
}
module.exports = handler2;
Now, to properly import your handlers, you need to go to your index.js file and require them and add them to your active handlers.
const Alexa = require("alexa-sdk");
// handlers
const newSession = require("./handlers/newSession");
const handler2 = require("./handlers/handler2");
exports.handler = function (event, context, callback) {
let alexa = Alexa.handler(event, context);
alexa.registerHandlers(
newSession,
handler2
);
alexa.execute();
};
If you are still confused on how to import modules in node with require, I recommend reading the docs here: https://nodejs.org/api/modules.html
Hopefully this cleared things up for you. Good luck!
@chasebish Thanks, that helped. I think i was importing it correctly but I wasnt adding them to the registerHandlers correctly. If you put them in a list first and add the list it doesnt work, but your example seems good. Thanks
@ajayarjun-bka - no, node_modules only needs to be in their once
Ya got it. I made a mistake in the directory structure. Thanks
How is this working in v2 ?
Most helpful comment
How is this working in v2 ?