Estoy tratando de ejecutar el ejemplo sillyNameMaker de acciones en google con api.ai, en mi computadora. Configuré un servidor nodejs con express y un túnel ngrok. Cuando intento enviar una solicitud con mi agente en api.ai, mi servidor recibe la solicitud POST, pero el cuerpo parece estar vacío. ¿Hay algo que no configuré correctamente?
Aquí está mi archivo index.js:
'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!') })Y el error que obtuve:
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)Estoy tratando de imprimir req.body pero no está definido... Gracias de antemano por su ayuda.
Tanto usted como el paquete de acciones en Google están asumiendo cómo está utilizando Express. De forma predeterminada, Express no completa el atributo req.body (consulte la referencia para req.body ). En su lugar, se basa en un middleware adicional, como el analizador corporal, para hacerlo.
Debería poder agregar un analizador de cuerpo a su proyecto con
npm install body-parser y luego úselo para analizar el cuerpo de la solicitud en JSON (que envía API.AI y usa acciones en Google) con algunas líneas adicionales justo después de definir la app para adjuntarla a Express:
var bodyParser = require('body-parser'); app.use(bodyParser.json());