Alexa-skills-kit-sdk-for-nodejs: Provide sample code for an express based Skill using the v2 SDK and Signature verification

Created on 12 Nov 2019  路  5Comments  路  Source: alexa/alexa-skills-kit-sdk-for-nodejs

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[ ] Feature request
[x] Documentation issue or request
[ ] Other... Please describe:

Could you please create an example Alexa skill that's built as an express app, which shows how to pass request / context to handlers and how to verify the signature, instead of a lambda-only example? I'm dependent upon using Express to power my Skill (handling incoming https requests). So, it'd be helpful to see a best practices for building a skill this way.

Expected Behavior


Current Behavior




Possible Solution


skill = Alexa.SkillBuilders.custom().addRequestHandlers(
      ...
    )
    .addErrorHandlers(ErrorHandler)
    .create();

   try {
    await new expressAdapter.SkillRequestSignatureVerifier().verify(textBody, requestHeaders);
    await new expressAdapter.TimestampVerifier().verify(textBody);
  } catch (err) {
    // server return err message
  }

  skill.invoke(req.body)
    .then(function(responseBody) {
      res.json(responseBody);
    })
    .catch(function(error) {
      console.log(error);
      res.status(500).send('Error during the request');
    });

What does it mean: "This code snippet assumes you have already consumed the request body as text and headers"??

Steps to Reproduce (for bugs)



Context



I'm trying to integrate a new skill with V2 SDK in my Express app executed on a node server.

Your Environment

  • ASK SDK for Node.js used: ^2.7.0
  • ASK SDK Express Adapter for Node.js used: ^2.0.0
  • Operating System and version: macOS 10.15.1 Catalina

Node.js and NPM Info

  • Node.js version used for development: 10.16.3
  • NPM version used for development: 6.9.0
2.x.x webservice support

Most helpful comment

Hi @sjames1958gm ,

Thanks for reaching out. And yes you're right, as you can see in our code base, the express adapter will send error response if other middleware is registered before the handler. Due to your report, I think only a error response might not be enough. I will reach out to the doc team to update our doc to avoid similar issues in the future.

Thanks,
Shen

All 5 comments

Hey @lucidokr , thanks for raising the issue. We have a backlog item for this request and will provide you with a code sample soon.

One issue I had trouble with the adaptor is that my boilerplate express app had app.use(bodyParser.json());
which caused a error in the first of the three request handlers pushed onto the array in the adaptor. A note to not add this middleware before adding the request handlers should be included.

Hi @lucidokr

Thanks for being patience and using our new ExpressAdapter package ! Your request is reasonable and we will work on sample skills in near future.

To answer your question: if you are using express and try to build an express app, you can ignore this part as it shows how to use SignatureVerifier and TimeStampVerifier without express. For user who don't use express, they cannot directly use our adapter class and need to figure out a way to parse the request body and request Header themselves. Therefore, for your case you just need to follow this tab. Please be aware don't use any other middleware before adding the request handlers just as @sjames1958gm mentioned.

Let me know if you met any problems while using our SDK !

Thanks,
Shen

Hi @sjames1958gm ,

Thanks for reaching out. And yes you're right, as you can see in our code base, the express adapter will send error response if other middleware is registered before the handler. Due to your report, I think only a error response might not be enough. I will reach out to the doc team to update our doc to avoid similar issues in the future.

Thanks,
Shen

Hey there,
I just created a little repo with an example. Hope this helps!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rmtuckerphx picture rmtuckerphx  路  3Comments

rmtuckerphx picture rmtuckerphx  路  5Comments

z4o4z picture z4o4z  路  3Comments

slesem picture slesem  路  4Comments

ancashoria picture ancashoria  路  6Comments