Skip to content

Instantly share code, notes, and snippets.

@manudevcode
Created November 20, 2019 18:40
Show Gist options
  • Save manudevcode/00cfc61278253e8267091674de6c01be to your computer and use it in GitHub Desktop.
Save manudevcode/00cfc61278253e8267091674de6c01be to your computer and use it in GitHub Desktop.
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()
@yashsm01
Copy link

a use this code to make interaction with firebase and Alexa and it gave output in firebase Realtime database but i also show error in JSON Output 1 (ex:- There was a problem with the requested skill's response(voice)). how can i solve this problem?

@yashsm01
Copy link

Screenshot (199)

@yashsm01
Copy link

yashsm01 commented Jul 16, 2020

as you show in last one i did not get any out put in form of utterance.

@amalkhaled225
Copy link

@yashsm01 did you find a solution for this problem
?

@yashsm01
Copy link

yashsm01 commented Nov 5, 2020

@amalkhaled225 no friend, tell me if you find any solution for this.

@yashsm01 did you find a solution for this problem
?

@LuWang0211
Copy link

The problem could cause by using "firebase admit", which has storage power and hard to force to stop running. So Alexa Service may show an error- "time out". If so, the following way works for me.
step1: import "firebase instead of "firebase-admin", code modify to as following:

           const admin = require('firebase');
           admin.initializeApp({serviceAccount)
                       })

step2: no need "async" before handle;
step2: using "goOffline()":
const result = new Promise((resolve) => { ref.set({ _" the data you want to set"_) }, () => {db.goOffline(); resolve(_" the result you want to get"_)}); });
Hope it will be helpful.

@Shivanijain21
Copy link

Shivanijain21 commented Nov 21, 2020

Could you please elaborate step 2? I tried implementing but I am getting the same error.

@LuWang0211
Copy link

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