尝试运行新的AWS /无服务器/ Dialogflow项目时遇到问题。我确信这是一件很简单的事,我只是没有看到。
使用以下内容创建初始项目:serverless create --template aws-nodejs-typescript
将 handler.js 移至 src / &已更新 serverless.yml
actions-on-google
跟随actions-on-google示例并更新 src / handler.js
import { dialogflow, Image } from 'actions-on-google';
const app = dialogflow({debug: true});
app.intent("test.intent", (conv) => {
conv.ask("Hi, how is it going?");
conv.ask("Here is a picture of a cat!");
conv.ask(new Image({
url: "https://developers.google.com/web/fundamentals/accessibility/semantics-builtin/imgs/160204193356-01-cat-500.jpg",
alt: "A fluffy cat!"
}));
});
exports.fulfillment = app;
还更新了 tsconfig.json 以匹配另一个Typescript项目
{
"compilerOptions": {
"sourceMap": true,
"target": "es6",
"allowJs": true,
"module": "commonjs"
},
"exclude": [
"node_modules"
],
"include": [
"./src/**/*"
]
}
为了彻底,我的 serverless.yml 。 (我手动创建了API网关,因为无服务器创建了lambda代理,我没有查看其他配置。)
service:
name: test-lambda
# Add the serverless-webpack plugin
plugins:
- serverless-webpack
provider:
name: aws
runtime: nodejs6.10
functions:
fulfillment:
handler: src/handler.fulfillment
# events:
# - http:
# method: get
# path: hello
项目成功编译和部署,但是当调用lambda时,我一直在
(node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot convert undefined or null to object
P.S。示例源选择使用猫!
答案 0 :(得分:0)
最后有一段时间来解决这个问题,并遇到了这个git issue。
基本上,dialogflow
实例需要由lambda封装。
exports.fulfillment = function(event, context, callback) {
app.handler(event, {})
.then((res) => {
if (res.status != 200) {
callback(null, {
"fulfillmentText": `I got status code: ${res.status}`
});
} else {
callback(null, res.body);
}
}).catch((e) => {
callback(null, {
"fulfillmentText": `There was an error\n${e}`
});
});
};