从 Express 请求创建 Actions-on-google 对象 [英] Create actions-on-google object from Express request
问题描述
文档此处 显示了此代码示例用于为 Express 创建 Actions SDK 对象:
The documentation here shows this code example for creating an Actions SDK object for Express:
const express = require('express');
const bodyParser = require('body-parser');
const expressApp = express().use(bodyParser.json());
expressApp.post('/fulfillment', app);
expressApp.listen(3000);
但我需要做这样的事情:
But I need to do something like this:
const express = require('express');
const bodyParser = require('body-parser');
function func (req, res) {
let headers = req.headers;
let body = req.body;
console.log(headers);
console.log(body);
const app = actionssdk({
// init: () => body
// init: () => req
});
app.intent('actions.intent.MAIN', (conv) => {
conv.ask("Welcome to the app");
})
return app;
}
const expressApp = express().use(bodyParser.json());
expressApp.post('/fulfillment', func);
expressApp.listen(3000);
这显然不是正确的语法,它不会向 Google Action 返回任何响应.
This obviously isn't the correct syntax and it doesn't return any response to the Google Action.
我想做这样的事情的原因是用动态clientId
初始化actionssdk
对象.我可以从 req.headers
对象中存在的授权标头中提取 projectId
.从 projectId
我可以动态获取 clientId
并通过传递 clientId
The reason I want to do something like this is to initialize the actionssdk
object with dynamic clientId
. I can extract the projectId
from the authorization header present in req.headers
object. From the projectId
I can dynamically fetch the clientId
and initialize the actionssdk
by passing the clientId
正确的语法是什么?
推荐答案
app
变量可以像这样使用 express 请求和响应对象调用,因为它是一个函数:
The app
variable can be called with express request and response objects like so, as it is a function:
const express = require('express');
const bodyParser = require('body-parser');
const expressApp = express().use(bodyParser.json());
expressApp.post('/fulfillment', async (req, res) => {
const clientId = // Some logic to get client ID
const app = actionssdk({ clientId })
// Define intent handlers here
return app(req, res)
});
expressApp.listen(3000);
这篇关于从 Express 请求创建 Actions-on-google 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!