Search code examples
node.jsamazon-web-servicesaws-lambdaalexa-skills-kitamazon-lex

What is the simplest way is to deploy an existing AWS Lex and Lambda as an Alexa Skills Kit (ASK)?


I currently have a Lex bot that requires a Lambda function on my AWS account. I want to turn this existing bot into an Alexa Skill but can't migrate it easily.

What I've done so far:

  • Publish a version of the Lex Bot and export as an ASK
  • Import the JSON file into the Alexa Developer Console JSON Editor
  • Followed this tutorial to give both my Alexa Skill and Lambda function access to each other

I've checked in the CloudWatch Logs and the Alexa Skill seems to be calling my function properly, however, it won't fulfill the intent. When it reaches the end of the dialog for an intent, the Amazon Skill only says:

You just triggered [IntentName]

I understand that the JSON format is different for Alexa and Lex and the return formats are as follows:

// Alexa return
return handlerInput.responseBuilder
        .speak(speakOutput)
        .reprompt(speakOutput)
        .getResponse();

// AWS Lex return
return {
  sessionAttributes: attributes,
  dialogAction: {
    type: 'Close',
    fulfillmentState: fulfillStatus,
    message: {
      contentType: "PlainText",
      content: messageContent,
    }
  }
};

Is there any simple way to convert my existing Lambda function to work with an Alexa Skill without needing to rewrite the entire thing?


Solution

  • The answer to my question is: no. There is no way way to use my existing Lex-specific Lambda function with Alexa since Lex and Alexa use different API's.

    The closest thing I found to using one Lambda for both would be to create a middleware to determine what client type is calling the Lambda, then, handle the Lex API calls and the Alexa API calls separately. This code comes from the AWS QnABot GitHub.

    In my opinion, this seems to go against the idea of single responsibility. Since they're both using different code anyway, it seems like creating 2 separate Lambdas would be better. Please let me know your thoughts on this.

    It's unfortunate that Lex code isn't able to translate to Alexa simply. Hopefully the AWS team creates a simple solution for this in the future.

    This is specifically for Lex V1 though. V2 seems to more easily translate into an Alexa Skill.