0

我最近开始开发 alexa 技能(使用 SMAPI 和 ASK SDK)并在后端 Firebase 上使用 nodejs。我将我的代码部署在 Cloud Functions 上,并将我的函数 uri 放在技能清单文件中的端点上。启动技能时遇到问题。

   `const AlexaASK = require('ask-sdk-core');    
   const LaunchRequestHandler = {
  canHandle(handlerInput) {
    return AlexaASK.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
  },
  handle(handlerInput) {
    const speakOutput = "launch request message";
    const repromptOutput = "reprompt message";
    return handlerInput.responseBuilder
      .speak(speakOutput)
      .reprompt(repromptOutput)
      .getResponse();
  },
};
const ExitHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === `IntentRequest` && (
              request.intent.name === 'AMAZON.StopIntent' ||
              request.intent.name === 'AMAZON.PauseIntent' ||
              request.intent.name === 'AMAZON.CancelIntent'
           );
  },
  handle(handlerInput) {
    return handlerInput.responseBuilder
      .speak(exitSkillMessage)
      .getResponse();
  },
};
const SessionEndedRequestHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';
  },
  handle(handlerInput) {
    //console.log(`Session ended with reason: ${JSON.stringify(handlerInput.requestEnvelope)}`);
    return handlerInput.responseBuilder.getResponse();
  },
};
const ErrorHandler = {
  canHandle() {
    return true;
  },
  handle(handlerInput) {
    return handlerInput.responseBuilder
      .speak("error")
      .reprompt("error hanlder")
      .getResponse();
  },
};

exports.alexaskill = functions.https.onRequest((req, response) => {
  const skillBuilder = AlexaASK.SkillBuilders.custom()
    .addRequestHandlers(
      LaunchRequestHandler,
      ExitHandler,
      SessionEndedRequestHandler
    )
    .addErrorHandlers(ErrorHandler)
    .create();
  response.send()
});`

4

2 回答 2

0

这段代码解决了我的问题。

`let skill;
app.post('/', function(req, res) {
  
    skill = AlexaASK.SkillBuilders.custom()
      .addRequestHandlers(
          LaunchRequestHandler,
          ExitHandler,
          SessionEndedRequestHandler
      )
      .create();
  
  skill.invoke(req.body)
    .then(function(responseBody) {
      return res.json(responseBody);
    })
    .catch(function(error) {
      console.log(error);
      res.status(500).send('Error during the request');
    });
});

exports.alexa = functions.https.onRequest(app);`

于 2020-07-25T17:21:48.090 回答
0

这是代码的快速存根

const functions = require('firebase-functions');
const AlexaASK = require('ask-sdk-core');
const { SkillRequestSignatureVerifier, TimestampVerifier } = require('ask-sdk-express-adapter');

...your ASK code..

let skillBuilder;

exports.alexaskill = functions.https.onRequest((req, response) => {
    if (!skillBuilder) {
        skillBuilder = AlexaASK.SkillBuilders.custom()
            .addRequestHandlers(
                LaunchRequestHandler,
                ExitHandler,
                SessionEndedRequestHandler
            )
            .addErrorHandlers(ErrorHandler)
            .create();
    } // only create the skill builder on the first invocation; otherwise it's static

   // validate that the signatures match
    try {
        const textBody = req.rawBody.toString()
        await new SkillRequestSignatureVerifier().verify(textBody, req.headers);
        await new TimestampVerifier().verify(textBody);
    } catch (err) {
        // server return err message
        response.send(403, JSON.stringify(err))
    }

    // invoke the skill 
    const responseASK = skill.invoke(req.body);
    response.send(responseASK.response); // this may just be responseASK

});
于 2020-07-24T14:52:00.220 回答