Alexa JSON 请求与 Intent Schema 不匹配

Alexa JSON Request doesn't match Intent Schema

我正在使用 Express 托管应用程序服务器(版本:"body-parser": 1.14.2", "express": "^4.13.3", "parse-http-header": "^1.0. 0", "x509": "^0.2.3") 节点 v5.4.1 NPM v3.3.12

我已经成功测试了 Alexa 和本地应用程序服务器之间的 SSL 连接,但是,当我解析来自 Alexa 的 Request 时,它与我的 不匹配Intent Schema 或在 Service Simulator - Test.

下生成的 Service Request

代码:

const fs = require('fs'); //File System, for loading certifications and keys
var http = require('http');
var https = require('https'); //Https server module
var bodyParser = require('body-parser');
var parseHttpHeader = require('parse-http-header');

/*****************   Credentials  **********************/
const privateKey  = fs.readFileSync('keys/private-key.pem'); //Private Key for validation (server uses only)
const certificate = fs.readFileSync('certs/certificate.pem'); //Certificate, to provide to connecting host.
const options = {
  key: privateKey,
  cert: certificate
};

/******** Instantiate the Express server *************/
var express = require('express');
var app = express(); //Instantiate express server

//Express configuration
app.set('title', 'mySkillJS');
app.set('json spaces', 2);

//Route all requests from app
app.all('/*', function(request, response){
  //Handle request
  var bucket = request;
  console.log('\n \n \n &&&&&& \n \n \n');
  var joptions = {
    type: 'application/json'
  }
  var bodyParsed = app.use(bodyParser.json(joptions));
  console.log(bodyParsed);
  var headParsed = parseHttpHeader(bucket.headers['content-type'])[0];
  console.log(headParsed);
});

//Ceate Server with SSL credentials
var httpsServer = https.createServer(options, app).listen(8000);//createServer(options, fx(req, res))

意图架构:

{
  "intents": [
    {
      "intent": "HelloWorldIntent"
    },
    {
      "intent": "AMAZON.HelpIntent"
    }
  ]
}

生成的服务请求:

{
  "session": {
    "sessionId": "SessionId.efb0235c-8201-4877-832e-58671b42c9b5",
    "application": {
      "applicationId": "amzn1.echo-sdk-ams.app.9eb766d1-f6a6-4ac7-9801-ce1478782b98"
    },
    "user": {
      "userId": "amzn1.echo-sdk-account.AHUTLGA6FMHPERGIUSQTTXG2RLOABOBTGSGN4LXBEWYK2XDLDRXU2"
    },
    "new": true
  },
  "request": {
    "type": "IntentRequest",
    "requestId": "EdwRequestId.734c7e4a-ec2c-4bdf-b2bc-37757796e670",
    "timestamp": "2016-01-22T04:10:18Z",
    "intent": {
      "name": "HelloWorldIntent",
      "slots": {}
    }
  }
}

我的应用程序服务器处理来自 Alexa 的请求:

 &&&&&& 



{ [Function]
  domain: undefined,
  _events: { mount: [Function: onmount] },
  _maxListeners: undefined,
  setMaxListeners: [Function: setMaxListeners],
  getMaxListeners: [Function: getMaxListeners],
  emit: [Function: emit],
  addListener: [Function: addListener],
  on: [Function: addListener],
  once: [Function: once],
  removeListener: [Function: removeListener],
  removeAllListeners: [Function: removeAllListeners],
  listeners: [Function: listeners],
  listenerCount: [Function: listenerCount],
  init: [Function: init],
  defaultConfiguration: [Function: defaultConfiguration],
  lazyrouter: [Function: lazyrouter],
  handle: [Function: handle],
  use: [Function: use],
  route: [Function: route],
  engine: [Function: engine],
  param: [Function: param],
  set: [Function: set],
  path: [Function: path],
  enabled: [Function: enabled],
  disabled: [Function: disabled],
  enable: [Function: enable],
  disable: [Function: disable],
  acl: [Function],
  bind: [Function],
  checkout: [Function],
  connect: [Function],
  copy: [Function],
  delete: [Function],
  get: [Function],
  head: [Function],
  link: [Function],
  lock: [Function],
  'm-search': [Function],
  merge: [Function],
  mkactivity: [Function],
  mkcalendar: [Function],
  mkcol: [Function],
  move: [Function],
  notify: [Function],
  options: [Function],
  patch: [Function],
  post: [Function],
  propfind: [Function],
  proppatch: [Function],
  purge: [Function],
  put: [Function],
  rebind: [Function],
  report: [Function],
  search: [Function],
  subscribe: [Function],
  trace: [Function],
  unbind: [Function],
  unlink: [Function],
  unlock: [Function],
  unsubscribe: [Function],
  all: [Function: all],
  del: [Function],
  render: [Function: render],
  listen: [Function: listen],
  request: IncomingMessage { app: [Circular] },
  response: ServerResponse { app: [Circular] },
  cache: {},
  engines: {},
  settings: 
   { 'x-powered-by': true,
     etag: 'weak',
     'etag fn': [Function: wetag],
     env: 'development',
     'query parser': 'extended',
     'query parser fn': [Function: parseExtendedQueryString],
     'subdomain offset': 2,
     'trust proxy': false,
     'trust proxy fn': [Function: trustNone],
     view: [Function: View],
     views: '/home/dev/Alexa/alexa-js-mod/samples/helloWorld/src/views',
     'jsonp callback name': 'callback',
     title: 'mySkillJS',
     'json spaces': 2 },
  _eventsCount: 1,
  locals: 
   { settings: 
      { 'x-powered-by': true,
        etag: 'weak',
        'etag fn': [Function: wetag],
        env: 'development',
        'query parser': 'extended',
        'query parser fn': [Function: parseExtendedQueryString],
        'subdomain offset': 2,
        'trust proxy': false,
        'trust proxy fn': [Function: trustNone],
        view: [Function: View],
        views: '/home/dev/Alexa/alexa-js-mod/samples/helloWorld/src/views',
        'jsonp callback name': 'callback',
        title: 'mySkillJS',
        'json spaces': 2 } },
  mountpath: '/',
  _router: 
   { [Function: router]
     params: {},
     _params: [],
     caseSensitive: false,
     mergeParams: undefined,
     strict: false,
     stack: [ [Object], [Object], [Object], [Object] ] } }
application/json

我不确定我做错了什么。我认为 Request 会包含类似于我的 Intent Schema 或生成的 Service Request 的内容。 为什么我没有在 JSON Request 中看到 Intent Schema 或我生成的 Service Request我从 Alexa 收到?

所以出现这个问题是因为对express模块​​没有经验

我认为 bodyParsed 会保存从以下返回的值:app.use(bodyParser.json());

当你使用 app.use(bodyParser.json()); 它实际上将它存储在 Request 的正文 属性 中。

所以:

app.use(bodyParser.json(joptions));//causes request.body to populate with the JSON data
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded requires multer
app.all('/*', function(request, response){
  //Handle request
  var bucket = request;
  console.log('\n \n \n &&&&&& \n \n \n');
  var joptions = {
    type: 'application/json'
  }
  console.log(request.body));
  var headParsed = parseHttpHeader(bucket.headers['content-type'])[0];
  console.log(headParsed);
});

不确定这是否会强制将所有请求解析为 application/json。也许在 app.all() 内移动 bodyparser 会导致它仅在使用该路由时解析?