alexa / alexa-skills-kit-sdk-for-nodejs

The Alexa Skills Kit SDK for Node.js helps you get a skill up and running quickly, letting you focus on skill logic instead of boilerplate code.
Apache License 2.0
3.12k stars 736 forks source link

Can we use the sdk without lambda? #200

Closed ancashoria closed 6 years ago

ancashoria commented 7 years ago

Hi, I'm trying to use the sdk outside lambda but I get this error: Cannot read property 'locale' of undefined

Here's my code:

router.post('/alexa_webhook', (req, res) => {
  const { context, request, session } = req.body
  const alexa = Alexa.handler(request, context)
  alexa.appId = APP_ID
  alexa.registerHandlers(handlers)
  alexa.execute()
})

The code that works on lambda is almost the same except for the objects passed to Alexa.handler()

Here it is:

exports.handler = function (event, context) {
    const alexa = Alexa.handler(event, context);
    alexa.APP_ID = APP_ID;
    alexa.registerHandlers(handlers);
    alexa.execute();
}

Am I doing something wrong, or it's not possible to use this sdk outside of lambda?

dman777 commented 6 years ago

I am having this issue with "locale": "en-US" in my request. I checked my linux server and that locale is selected and available. Any ideas?

dman777 commented 6 years ago

Same question here....looking at the source code. It looks like it only deals with lamba in which the request is mutated and not one to one of a request of a normal node server(ie: it doesn't not look for locale on body.request.locale for value).

Anyone know????

ghost commented 6 years ago

Hi Ancashoria, A normal lambda event will have a json body similar to the following:

{
  "version": "1.0",
  "session": {
    "new": true,
    "sessionId": "amzn1.echo-api.session.[unique-value-here]",
    "application": {
      "applicationId": "amzn1.ask.skill.[unique-value-here]"
    },
    "attributes": {
      "key": "string value"
    },
    "user": {
      "userId": "amzn1.ask.account.[unique-value-here]",
      "accessToken": "Atza|AAAAAAAA...",
      "permissions": {
        "consentToken": "ZZZZZZZ..."
      }
    }
  },
  "context": {
    "System": {
      "device": {
        "supportedInterfaces": {
          "AudioPlayer": {}
        }
      },
      "application": {
        "applicationId": "amzn1.ask.skill.[unique-value-here]"
      },
      "user": {
        "userId": "amzn1.ask.account.[unique-value-here]",
        "accessToken": "Atza|AAAAAAAA...",
        "permissions": {
          "consentToken": "ZZZZZZZ..."
        }
      },
      "apiEndpoint": "https://api.amazonalexa.com"
    },
    "AudioPlayer": {
      "playerActivity": "PLAYING",
      "token": "audioplayer-token",
      "offsetInMilliseconds": 0
    }
  },
  "request": {}
}

Inside the sdk, there's a HandleLambdaEvent() method in which the sdk is trying to retrieve locale using this.locale = this._event.request.locale;. SDK is written under assumption that event object will have a similar structure as above.

It seems that in your code, only request is passed into Alexa.handler(). which will cause this.locale = this._event.request.locale; to fail.

Hope this helps!

feedm3 commented 6 years ago

The question was already answered in another issue: https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs/issues/72

carterw commented 6 years ago

I disagree, it was not actually answered.

ciberado commented 6 years ago

Maybe this is what you are looking for? https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs/issues/263#issuecomment-355988626

It works very nicely in conjuction with the is-lambda package (https://www.npmjs.com/package/is-lambda).