Created
August 25, 2018 18:55
-
-
Save anoop4real/db65d6fb544b6a482a9da3c195cd8f2f to your computer and use it in GitHub Desktop.
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
| /* eslint-disable func-names */ | |
| /* eslint quote-props: ["error", "consistent"]*/ | |
| /** | |
| * This sample demonstrates a simple skill built with the Amazon Alexa Skills | |
| * nodejs skill development kit. | |
| * This sample supports multiple lauguages. (en-US, en-GB, de-DE). | |
| * The Intent Schema, Custom Slots and Sample Utterances for this skill, as well | |
| * as testing instructions are located at https://github.com/alexa/skill-sample-nodejs-fact | |
| **/ | |
| 'use strict'; | |
| const Alexa = require('alexa-sdk'); | |
| const http = require('http'); | |
| //========================================================================================================================================= | |
| //TODO: The items below this comment need your attention. | |
| //========================================================================================================================================= | |
| //Replace with your app ID (OPTIONAL). You can find this value at the top of your skill's page on http://developer.amazon.com. | |
| //Make sure to enclose your value in quotes, like this: const APP_ID = 'amzn1.ask.skill.bb4045e6-b3e8-4133-b650-72923c5980f1'; | |
| const APP_ID = 'amzn1.ask.skill.09f4a640-8a03-4185-b2c3-ccdc05ef1a6d'; | |
| const SKILL_NAME = 'Funny Translations'; | |
| const GET_HERO_MESSAGE = "Here's your hero: "; | |
| const HELP_MESSAGE = 'You can say translate followed by a sentence to translate, or, you can say exit... What can I help you with?'; | |
| const HELP_REPROMPT = 'What can I help you with?'; | |
| const STOP_MESSAGE = 'Enjoy the day...Goodbye!'; | |
| const MORE_MESSAGE = 'Do you want more?' | |
| const PAUSE = '<break time="0.3s" />' | |
| const WHISPER = '<amazon:effect name="whispered"/>' | |
| const PLING = "<audio src='https://s3.amazonaws.com/ask-soundlibrary/cartoon/amzn_sfx_boing_long_1x_01.mp3'/>" | |
| const WELCOME_MESSAGE = "Hello" + PAUSE +", Welcome to funny translations, please choose the translations to use from the following list," + PAUSE + "YODA" + PAUSE + "PIRATE" + PAUSE + "MINION"; | |
| // +-----------------------------------------------+ | |
| // |Constants: Service types available | | |
| // | | | |
| // |- yoda | | |
| // |- pirate | | |
| // |- minion | | |
| // +-----------------------------------------------+ | |
| var serviceEndPoints = { | |
| yodaAPI:'yoda.json', | |
| pirateAPI:'pirate.json', | |
| minionAPI:'minion.json' | |
| }; | |
| var speechNames = { | |
| yoda:'yoda', | |
| pirate:'pirate', | |
| minion:'minion' | |
| }; | |
| var curreEndpoint = serviceEndPoints.minionAPI; | |
| var endpointSpeechName = speechNames.minion | |
| const baseEndpoint = 'http://api.funtranslations.com/translate/' | |
| //========================================================================================================================================= | |
| //Editing anything below this line might break your skill. | |
| //========================================================================================================================================= | |
| const handlers = { | |
| 'LaunchRequest': function () { | |
| this.emit('WelcomeTheUser'); | |
| }, | |
| 'WelcomeTheUser': function () { | |
| const speechOutput = WELCOME_MESSAGE; | |
| const more = "Please make a choice"; | |
| this.response.cardRenderer(SKILL_NAME, "Welcome User"); | |
| this.response.speak(speechOutput).listen(more); | |
| this.emit(':responseReady'); | |
| }, | |
| 'SetEndpoint': function () { | |
| // if (this.event.request.dialogState === 'STARTED') { | |
| // let updatedIntent = this.event.request.intent; | |
| // this.emit(':delegate', updatedIntent); | |
| // } | |
| var nameVal = this.event.request.intent.slots.endpoint.value; | |
| var lowerName = nameVal.toLowerCase() | |
| console.log("NAMEVAL" + lowerName); | |
| switch (lowerName) { | |
| case 'yoda': | |
| console.log("ENDPOINT SET" + "Yoda"); | |
| curreEndpoint = serviceEndPoints.yodaAPI; | |
| endpointSpeechName = speechNames.yoda | |
| break; | |
| case 'pirate': | |
| curreEndpoint = serviceEndPoints.pirateAPI; | |
| endpointSpeechName = speechNames.pirate | |
| console.log("ENDPOINT SET" + "Pirate"); | |
| break; | |
| case 'minion': | |
| curreEndpoint = serviceEndPoints.minionAPI; | |
| endpointSpeechName = speechNames.minion | |
| console.log("ENDPOINT SET" + "minion"); | |
| break; | |
| default: | |
| break; | |
| } | |
| const speechOutput = "OK, I will use your choice" + PAUSE + " Please say translate followed by a sentence to translate"; | |
| const more = "Please say translate followed by a sentence to translate"; | |
| this.response.cardRenderer(SKILL_NAME, "translate"); | |
| this.response.speak(speechOutput).listen(more); | |
| this.emit(':responseReady'); | |
| }, | |
| 'GetTranslation': function () { | |
| var sentenceVal = this.event.request.intent.slots.sentence.value; | |
| const query = '/translate/'+curreEndpoint + '?text=' + encodeURIComponent(`${sentenceVal}`); | |
| console.log("Calling GetTranslation") | |
| httpGet(query, (theResult, status) => { | |
| //console.log("sent : " + query); | |
| console.log("received : " + theResult); | |
| const theFact = theResult; | |
| const more = "Please say translate followed by a sentence to translate again"; | |
| const speechOutput = endpointSpeechName + "says" + PAUSE + theFact; | |
| this.response.cardRenderer(SKILL_NAME, speechOutput); | |
| if(status){ | |
| this.response.speak(speechOutput + PAUSE + PLING + more).listen(more); | |
| }else{ | |
| this.response.speak(speechOutput + PLING); | |
| } | |
| this.emit(':responseReady'); | |
| }); | |
| // var sentenceVal = this.event.request.intent.slots.sentence.value; | |
| // const query = encodeURIComponent(`?text=${sentenceVal}`); | |
| // console.log("QUERY" + query) | |
| // const url = baseEndpoint + curreEndpoint + query; | |
| // var speechOutput = ""; | |
| // console.log("CALLING httpGet") | |
| // httpGet(query, (theResult) => { | |
| // console.log("sent : " + query); | |
| // console.log("received : " + theResult); | |
| // const more = "Please say translate followed by a sentence to translate again"; | |
| // this.response.cardRenderer(SKILL_NAME, speechOutput); | |
| // this.response.speak(speechOutput).listen(more); | |
| // this.emit(':responseReady'); | |
| // //const obj = JSON.parse(theResult); | |
| // // speechOutput += obj.contents.translated; | |
| // }); | |
| // // request(url, function (error, response, body) { | |
| // // const obj = JSON.parse(body); | |
| // // speechOutput += obj.contents.translated; | |
| // // console.log(obj.contents.translated); | |
| // // console.log('error:', error); // Print the error if one occurred | |
| // // console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received | |
| // // console.log('body:', body); // Print the HTML for the Google homepage. | |
| // // }); | |
| }, | |
| 'AMAZON.HelpIntent': function () { | |
| const speechOutput = HELP_MESSAGE; | |
| const reprompt = HELP_REPROMPT; | |
| this.response.speak(speechOutput).listen(reprompt); | |
| this.emit(':responseReady'); | |
| }, | |
| 'AMAZON.CancelIntent': function () { | |
| this.response.speak(STOP_MESSAGE); | |
| this.emit(':responseReady'); | |
| }, | |
| 'AMAZON.StopIntent': function () { | |
| this.response.speak(STOP_MESSAGE); | |
| this.emit(':responseReady'); | |
| }, | |
| 'Unhandled': function() { | |
| this.emit('AMAZON.HelpIntent'); | |
| } | |
| }; | |
| exports.handler = function (event, context, callback) { | |
| const alexa = Alexa.handler(event, context, callback); | |
| alexa.APP_ID = APP_ID; | |
| alexa.registerHandlers(handlers); | |
| alexa.execute(); | |
| }; | |
| function httpGet(query, callback) { | |
| var options = { | |
| host: 'api.funtranslations.com', | |
| path: query, | |
| method: 'GET', | |
| }; | |
| console.log("SET OPTIONS"); | |
| var req = http.request(options, res => { | |
| res.setEncoding('utf8'); | |
| var responseString = ""; | |
| //accept incoming data asynchronously | |
| res.on('data', chunk => { | |
| responseString = responseString + chunk; | |
| }); | |
| //return the data when streaming is complete | |
| res.on('end', () => { | |
| console.log("RESPONSE STRING" + responseString); | |
| var obj = JSON.parse(responseString); | |
| if (obj.error){ | |
| callback("My sources are tired..ask after sometime", false); | |
| }else{ | |
| console.log("RESPONSE" + obj.contents.translated); | |
| callback(obj.contents.translated, true); | |
| } | |
| }); | |
| }); | |
| req.on('error', function(e) { | |
| console.log('problem with request: ' + e.message); | |
| }); | |
| req.end(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment