gpt4 book ai didi

aws-lambda - 无法在 AWS Lambda 中初始化 Dialogflow Fulfillment WebhookClient

转载 作者:行者123 更新时间:2023-12-02 02:53:53 24 4
gpt4 key购买 nike

根据DialogFLow Fulfillment docs ,WebhookClient 构造函数需要 Express HTTP 请求和响应对象。但是,在 Lambda 函数中,我只收到 event(请求)。如何创建 Express 请求和响应对象?

到目前为止我已经试过了:

const {WebhookClient} = require('dialogflow-fulfillment');

exports.dialogflowFulfillment = async (event) => {
let response = {};
const agent = new WebhookClient({ event, response });

function sayNiceThings(agent) {
agent.add(`Nice to meet you!`);
}

let intentMap = new Map();
intentMap.set('Say Nice Things', sayNiceThings);
agent.handleRequest(intentMap);
};

最佳答案

创建一个 NodeJS Express 应用

安装用于添加 AWS Lambda 网桥的 serverless-http 包

安装 dialogflow-fulfillment 和 actions-on-google npm 包。

  npm init -f
npm install --save express serverless-http
npm install dialogflow-fulfillment
npm install actions-on-google

创建 index.js 文件:

index.js:
=========
const serverless = require('serverless-http');
const bodyParser = require('body-parser');
const express = require('express');

const app = express();
app.use(bodyParser.json({ strict: false }));

const {WebhookClient, Card, Suggestion} = require('dialogflow-fulfillment');
const request = require('request');



app.get('/', function (req, res) {

res.send('Hello World !!!\n');
console.log("Testing express lambda\n");

})



app.post('/', function (req, res) {


const agent = new WebhookClient({request: req, response: res});

function test_handler(agent) {
agent.add(`Welcome to my agent on AWS Lambda!`);
}

// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('test-intent', test_handler);

agent.handleRequest(intentMap);


})


module.exports.handler = serverless(app);

在serverless.yml中添加配置:

serverless.yml
================

service: example-express-lambda

provider:
name: aws
runtime: nodejs8.10
stage: dev
region: ap-southeast-1

functions:
app:
handler: index.handler
events:
- http: ANY /
- http: 'ANY {proxy+}'

然后部署 lambda 函数。在 Dialogflow fulfillment webhook url 中添加端点 url。

引用: https://serverless.com/blog/serverless-express-rest-api/

关于aws-lambda - 无法在 AWS Lambda 中初始化 Dialogflow Fulfillment WebhookClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50530883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com