从 Express 请求创建 Actions-on-google 对象 [英] Create actions-on-google object from Express request

查看:10
本文介绍了从 Express 请求创建 Actions-on-google 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档此处 显示了此代码示例用于为 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆