Skip to content

Instantly share code, notes, and snippets.

@adamcrampton
Created December 3, 2018 04:30
Show Gist options
  • Save adamcrampton/b6e00f2b120577380e4b4c966b62f3a7 to your computer and use it in GitHub Desktop.
Save adamcrampton/b6e00f2b120577380e4b4c966b62f3a7 to your computer and use it in GitHub Desktop.
Simple boilerplate for Lambda function using the Alexa SDK for Node JS
// Alexa Demo
// ==========
'use strict';
// Config Alexa SDK and required libraries.
const Alexa = require('alexa-sdk');
const request = require('request-promise');
// Define handlers.
const handlers = {
'LaunchRequest': function() {
this.emit(':ask', 'What would you like to do?', 'Please say that again?');
},
'SessionEndedRequest': function() {
console.log('session ended.');
this.emit(':saveState', true);
},
'Unhandled': function() {
this.emit(':ask', 'What would you like to do?', 'Please say that again?');
},
'demoIntent': function() {
// Define intent and slot(s).
let intent = this.event.request.intent;
let slotValue = getSlotValue(intent);
// Make emit method available within the scope of request.
var emitMethod = this.emit;
// Generate speech string (this will likely be in a function).
// Initialise speech array, which will eventually be converted to append to the speech string.
var speechArray = [];
var speechString = '';
// Alexa does not like ampersands.
speechString = speechString.replace('&', 'and');
// Output speech.
emitMethod(':tell', speechString);
}
}
// Do the Lambda/Alexa magic.
exports.handler = function(event, context, callback) {
const alexa = Alexa.handler(event, context, callback);
alexa.appId = process.env.APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};
// Uncomment to fire up local Node server
// const http = require('http');
// const hostname = '127.0.0.1';
// const port = 3000;
// const server = http.createServer((req, res) => {
// res.statusCode = 200;
// res.setHeader('Content-Type', 'text/plain');
// res.end('Test Page\n');
// });
// server.listen(port, hostname, () => {
// console.log(`Server running at http://${hostname}:${port}/`);
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment