Created
November 20, 2019 18:40
-
-
Save manudevcode/00cfc61278253e8267091674de6c01be to your computer and use it in GitHub Desktop.
This file contains 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
const Alexa = require('ask-sdk') | |
const admin = require('firebase-admin') | |
var serviceAccount = require('./testing-iot-19f5e-firebase-adminsdk-oj631-6d9d70e282.json') | |
admin.initializeApp({ | |
credential: admin.credential.cert(serviceAccount), | |
databaseURL: 'https://testing-iot-19f5e.firebaseio.com' | |
}) | |
const GetNewFactHandler = { | |
canHandle (handlerInput) { | |
const request = handlerInput.requestEnvelope.request | |
return request.type === 'LaunchRequest' || | |
(request.type === 'IntentRequest' && request.intent.name === 'Traductor') | |
}, | |
async handle (handlerInput) { | |
var db = admin.database() | |
var ref = db.ref('bombas') | |
// Guardado en db | |
await ref.child('bomba0').set({ | |
on: true | |
}).then(() => { | |
}) | |
const factArr = data | |
const factIndex = Math.floor(Math.random() * factArr.length) | |
const randomFact = factArr[factIndex] | |
const speechOutput = GET_FACT_MESSAGE + randomFact | |
return handlerInput.responseBuilder | |
.speak(speechOutput) | |
.withSimpleCard(SKILL_NAME, randomFact) | |
.getResponse() | |
} | |
} | |
const HelpHandler = { | |
canHandle (handlerInput) { | |
const request = handlerInput.requestEnvelope.request | |
return request.type === 'IntentRequest' && | |
request.intent.name === 'AMAZON.HelpIntent' | |
}, | |
handle (handlerInput) { | |
return handlerInput.responseBuilder | |
.speak(HELP_MESSAGE) | |
.reprompt(HELP_REPROMPT) | |
.getResponse() | |
} | |
} | |
const ExitHandler = { | |
canHandle (handlerInput) { | |
const request = handlerInput.requestEnvelope.request | |
return request.type === 'IntentRequest' && | |
(request.intent.name === 'AMAZON.CancelIntent' || request.intent.name === 'AMAZON.StopIntent') | |
}, | |
handle (handlerInput) { | |
return handlerInput.responseBuilder | |
.speak(STOP_MESSAGE) | |
.getResponse() | |
} | |
} | |
const SessionEndedRequestHandler = { | |
canHandle (handlerInput) { | |
const request = handlerInput.requestEnvelope.request | |
return request.type === 'SessionEndedRequest' | |
}, | |
handle (handlerInput) { | |
console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`) | |
return handlerInput.responseBuilder.getResponse() | |
} | |
} | |
const ErrorHandler = { | |
canHandle () { | |
return true | |
}, | |
handle (handlerInput, error) { | |
console.log(`Error handled: ${error.message}`) | |
return handlerInput.responseBuilder | |
.speak('Lo siento ocurrió un error.') | |
.reprompt('Lo siento, ha ocurrido un error.') | |
.getResponse() | |
} | |
} | |
const SKILL_NAME = 'Robot Traductor' | |
const GET_FACT_MESSAGE = 'Ahí va: <break time="1s"/> ' | |
const HELP_MESSAGE = 'Hola puedes pedimre traducir algo a lenguaje robot.' | |
const HELP_REPROMPT = '¿Cómo puedo ayudarte?' | |
const STOP_MESSAGE = 'Adiós!' | |
const data = [ | |
'<audio src="https://robot-traductor.s3-us-west-2.amazonaws.com/texto_1.mp3"/> <break time="1s"/> ¿Que tal?' | |
] | |
const skillBuilder = Alexa.SkillBuilders.standard() | |
exports.handler = skillBuilder | |
.addRequestHandlers( | |
GetNewFactHandler, | |
HelpHandler, | |
ExitHandler, | |
SessionEndedRequestHandler | |
) | |
.addErrorHandlers(ErrorHandler) | |
.lambda() |
LuWang0211
commented
Nov 21, 2020
- medthod1: promis.then() (as the same as step 3, I said above):
- medthod2: async await:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment