2017-04-21 5 views
1

내 컴퓨터에서 google actions-on-google에서 sillyNameMaker example을 실행하려고합니다. express와 ngrok 터널링을 사용하여 nodejs 서버를 설정합니다. api.ai에서 내 상담원에게 요청을 보내려고하면 내 서버가 POST 요청을 받지만 본문이 비어있는 것처럼 보입니다. 내가 제대로 설정하지 않은 것이 있습니까? ... 감사 내가 req.body를 인쇄하기 위해 노력하고있어actions-on-google api.ai가 nodejs와 함께 POST 요청시 본문을 보내지 않으며 표현형이 아닙니다.

TypeError: Cannot read property 'originalRequest' of undefined 
    at new ApiAiAssistant (/Users/clementjoudet/Desktop/Dev/google-home/node_modules/actions-on-google/api-ai-assistant.js:67:19) 
    at sillyNameMaker (/Users/clementjoudet/Desktop/Dev/google-home/main.js:8:21) 

하지만 정의되지 않은 :

'use strict'; 
var express = require('express') 
var app = express() 
const ApiAiAssistant = require('actions-on-google').ApiAiAssistant; 

function sillyNameMaker(req, res) { 
    const assistant = new ApiAiAssistant({request: req, response: res}); 

    // Create functions to handle requests here 
    const WELCOME_INTENT = 'input.welcome'; // the action name from the API.AI intent 
    const NUMBER_INTENT = 'input.number'; // the action name from the API.AI intent 
    const NUMBER_ARGUMENT = 'input.mynum'; // the action name from the API.AI intent 

    function welcomeIntent (assistant) { 
    assistant.ask('Welcome to action snippets! Say a number.'); 
    } 

    function numberIntent (assistant) { 
    let number = assistant.getArgument(NUMBER_ARGUMENT); 
    assistant.tell('You said ' + number); 
    } 

    let actionMap = new Map(); 
    actionMap.set(WELCOME_INTENT, welcomeIntent); 
    actionMap.set(NUMBER_INTENT, numberIntent); 
    assistant.handleRequest(actionMap); 

    function responseHandler (assistant) { 
    console.log("okok") 
    // intent contains the name of the intent you defined in the Actions area of API.AI 
    let intent = assistant.getIntent(); 
    switch (intent) { 
     case WELCOME_INTENT: 
     assistant.ask('Welcome! Say a number.'); 
     break; 

     case NUMBER_INTENT: 
     let number = assistant.getArgument(NUMBER_ARGUMENT); 
     assistant.tell('You said ' + number); 
     break; 
    } 
    } 
    // you can add the function name instead of an action map 
    assistant.handleRequest(responseHandler); 
} 


app.post('/google', function (req, res) { 
    console.log(req.body); 
    sillyNameMaker(req, res); 
}) 


app.get('/', function (req, res) { 
    res.send("Server is up and running.") 
}) 


app.listen(3000, function() { 
    console.log('Example app listening on port 3000!') 
}) 

그리고 내가 가진 오류 : 여기

내하는 index.js 파일입니다 도움을 청하십시오.

답변

8

귀하와 Google의 액션 패키지 모두 귀하가 Express를 어떻게 사용하고 있는지 가정합니다. 기본적으로 Express는 이 아니며은 req.body 특성을 채 웁니다 (reference for req.body 참조). 대신, 추가 미들웨어 (예 : body-parser)를 사용합니다.

당신은

npm install body-parser 

으로 프로젝트에 몸 파서를 추가 한 다음 일부 (API.AI가 보내는 및 구글 액션 -에 - 사용) JSON으로 요청 본문을 구문 분석하는 데 사용할 수 있어야합니다 을 정의한 직후에 추가 행을 추가하면 다음과 같이 나타납니다.


var bodyParser = require('body-parser'); 
app.use(bodyParser.json()); 
+0

대단히 감사합니다. – clemkoa