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() |
Could you please elaborate step 2? I tried implementing but I am getting the same error.
I mean, if using step 3, then u do not need to use Async await simultaneously. The following both works:
- medthod1: promis.then() (as the same as step 3, I said above):
handle(handlerInput) {
let response = { apiResponse:{ _" the arg"_}};
var db = firebase.database();
var ref = db.ref( _" the data location on firebase"_);
const result = new Promise((resolve) => {
ref.set(
{ _" the data you want to set"_) },
() => {
db.goOffline();
resolve(response)}
);
});
return result;
}
- medthod2: async await:
async handle(handlerInput) {
let response = { apiResponse:{ _" the arg"_}};
var db = firebase.database();
var ref = db.ref( _" the data location on firebase"_);
const result = await ref.set({ _" the data you want to set"_));
db.goOffline();
return response;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you please elaborate step 2? I tried implementing but I am getting the same error.