Created
May 16, 2016 17:39
-
-
Save rondagdag/3b8bf62e0e90e158740ebd6cdd0f8dc5 to your computer and use it in GitHub Desktop.
Yoda speak for amazon echo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Developed by Ron Dagdag | |
Email: [email protected] | |
*/ | |
'use strict'; | |
let https = require('https'); | |
//var request = require('request'); | |
// Route the incoming request based on type (LaunchRequest, IntentRequest, | |
// etc.) The JSON body of the request is provided in the event parameter. | |
exports.handler = function (event, context) { | |
try { | |
console.log("event.session.application.applicationId=" + event.session.application.applicationId); | |
/** | |
* Uncomment this if statement and populate with your skill's application ID to | |
* prevent someone else from configuring a skill that sends requests to this function. | |
*/ | |
if (event.session.application.applicationId !== "amzn1.echo-sdk-ams.app.11111111111111") { | |
context.fail("Invalid Application ID"); | |
} | |
if (event.session.new) { | |
onSessionStarted({requestId: event.request.requestId}, event.session); | |
} | |
if (event.request.type === "LaunchRequest") { | |
onLaunch(event.request, | |
event.session, | |
function callback(sessionAttributes, speechletResponse) { | |
context.succeed(buildResponse(sessionAttributes, speechletResponse)); | |
}); | |
} else if (event.request.type === "IntentRequest") { | |
onIntent(event.request, | |
event.session, | |
function callback(sessionAttributes, speechletResponse) { | |
context.succeed(buildResponse(sessionAttributes, speechletResponse)); | |
}); | |
} else if (event.request.type === "SessionEndedRequest") { | |
onSessionEnded(event.request, event.session); | |
context.succeed(); | |
} | |
} catch (e) { | |
context.fail("Exception: " + e); | |
} | |
}; | |
/** | |
* Called when the session starts. | |
*/ | |
function onSessionStarted(sessionStartedRequest, session) { | |
console.log("onSessionStarted requestId=" + sessionStartedRequest.requestId + | |
", sessionId=" + session.sessionId); | |
} | |
/** | |
* Called when the user launches the skill without specifying what they want. | |
*/ | |
function onLaunch(launchRequest, session, callback) { | |
console.log("onLaunch requestId=" + launchRequest.requestId + | |
", sessionId=" + session.sessionId); | |
// Dispatch to your skill's launch. | |
getWelcomeResponse(session, callback); | |
} | |
/** | |
* Called when the user specifies an intent for this skill. | |
*/ | |
function onIntent(intentRequest, session, callback) { | |
console.log("onIntent requestId=" + intentRequest.requestId + | |
", sessionId=" + session.sessionId); | |
var intent = intentRequest.intent, | |
intentName = intentRequest.intent.name; | |
// Dispatch to your skill's intent handlers | |
if ("GetYodaSpeak" === intentName) { | |
YodaSpeak(intent, session, callback); | |
} else if ("AMAZON.HelpIntent" === intentName) { | |
getHelpResponse(session, callback); | |
} else if ("AMAZON.StopIntent" === intentName || "AMAZON.CancelIntent" === intentName) { | |
handleSessionEndRequest(callback); | |
} else { | |
throw "Invalid intent"; | |
} | |
} | |
/** | |
* Called when the user ends the session. | |
* Is not called when the skill returns shouldEndSession=true. | |
*/ | |
function onSessionEnded(sessionEndedRequest, session) { | |
console.log("onSessionEnded requestId=" + sessionEndedRequest.requestId + | |
", sessionId=" + session.sessionId); | |
// Add cleanup logic here | |
} | |
// --------------- Functions that control the skill's behavior ----------------------- | |
function getWelcomeResponse(session, callback) { | |
// If we wanted to initialize the session to have some attributes we could add those here. | |
var sessionAttributes = {}; | |
var cardTitle = "Welcome"; | |
var speechOutput = "You can say - Translate You will learn how to speak like me someday. Oh wait"; | |
// If the user either does not reply to the welcome message or says something that is not | |
// understood, they will be prompted again with this text. | |
var repromptText = "You can say - Translate You will learn how to speak like me someday. Oh wait"; | |
var shouldEndSession = false; | |
callback(sessionAttributes, | |
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)); | |
} | |
function getHelpResponse(session, callback) { | |
// If we wanted to initialize the session to have some attributes we could add those here. | |
var sessionAttributes = {}; | |
var cardTitle = "Help"; | |
var speechOutput = "I will do the Yoda Speak after saying translate. You can say - Translate You will learn how to speak like me someday. Oh wait. "; | |
// If the user either does not reply to the welcome message or says something that is not | |
// understood, they will be prompted again with this text. | |
var repromptText = "I will do the Yoda Speak after saying translate. You can say - Translate You will learn how to speak like me someday. Oh wait. "; | |
var shouldEndSession = false; | |
callback(sessionAttributes, | |
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)); | |
} | |
function handleSessionEndRequest(callback) { | |
var cardTitle = "Good Bye"; | |
var speechOutput = "Thank you."; | |
// Setting this to true ends the session and exits the skill. | |
var shouldEndSession = true; | |
callback({}, buildSpeechletResponse(cardTitle, speechOutput, null, shouldEndSession)); | |
} | |
function YodaSpeak(intent, session, callback) { | |
var cardTitle = "Yoda Speak"; | |
var repromptText = ""; | |
var sessionAttributes = {}; | |
var shouldEndSession = true; | |
var speechOutput = ""; | |
var translatewords = null; | |
if (intent.slots) { | |
var locationSlot = intent.slots.SlotName; | |
if (locationSlot.value){ | |
translatewords = locationSlot.value; | |
//sessionAttributes = createZipCodeAttributes(zipcode); | |
} | |
} | |
if (translatewords) { | |
getYodaSpeak(translatewords, function(err, data){ | |
//list all the names of state representative | |
if (!err) { | |
console.log(data.toString()) | |
if (data.toString().indexOf("Application Error") <= -1) | |
{ | |
speechOutput = data.toString() | |
callback(sessionAttributes, | |
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)); | |
} else | |
{ | |
speechOutput = "There was an error translating, please try again" | |
callback(sessionAttributes, | |
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)); | |
} | |
} else { | |
speechOutput = "There was an error translating, please try again" | |
callback(sessionAttributes, | |
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)); | |
} | |
}); | |
} else | |
{ | |
getWelcomeResponse(session, callback) | |
} | |
} | |
function getYodaSpeak(translatewords, callback) { | |
var options = { | |
"host": 'yoda.p.mashape.com', | |
"path": '/yoda?sentence=' + encodeURI(translatewords), | |
"headers" : { | |
"X-Mashape-Key" : "11111111111111111111111111111111111111" | |
} | |
}; | |
//options.agent = new https.Agent(options); | |
console.log(options); | |
var req = https.get(options, function(res) { | |
//console.log(res) | |
res.on('data', function(data) { | |
callback(null, data); | |
}); | |
}); | |
req.on('error', function(err) { | |
callback(err); | |
}) | |
return req; | |
} | |
// --------------- Helpers that build all of the responses ----------------------- | |
function buildSpeechletResponse(title, output, repromptText, shouldEndSession) { | |
return { | |
outputSpeech: { | |
type: "PlainText", | |
text: output | |
}, | |
card: { | |
type: "Simple", | |
title: title, | |
content: output | |
}, | |
reprompt: { | |
outputSpeech: { | |
type: "PlainText", | |
text: repromptText | |
} | |
}, | |
shouldEndSession: shouldEndSession | |
}; | |
} | |
function buildResponse(sessionAttributes, speechletResponse) { | |
return { | |
version: "1.0", | |
sessionAttributes: sessionAttributes, | |
response: speechletResponse | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment