我刚刚开始使用Dialogflow为我的Google Home构建一些简单的应用程序,而我在创建一个只会返回带有句子的随机名称的应用程序时遇到了问题。
例如:我说"给我们一个挑战":我希望应用程序返回类似$ random_name的内容应该做10次俯卧撑。
这可能实现吗?
谢谢!
答案 0 :(得分:0)
如上面的评论,您需要使用您的履行来确定名称并将其回复给用户。简单地说,您可以使用类似下面的代码从固定名称的数组中返回名称:
"use strict";
process.env.DEBUG = "actions-on-google:*";
const App = require("actions-on-google").DialogflowApp;
const request = require("request");
const nameListFromConst = [
"name1", "name2", "name3", "name4", "name5",
"name6", "name7", "name8", "name9", "name10"
];
exports.foobar = (req, res) => {
const app = new App({request: req, response: res});
const inputWelcome = app => {
const index = Math.floor(Math.random() * 10);
const name = nameListFromConst[index];
app.ask(name);
};
const actionMap = new Map();
actionMap.set("input.welcome", inputWelcome);
app.handleRequest(actionMap);
};
但是,您似乎想要从注册到Dialogflow代理中的实体中确定名称。如果为true,则可以在履行代码中动态检索具有Dialogflow API的实体,如下所示:
exports.foobar = (req, res) => {
const app = new App({request: req, response: res});
const inputWelcome = app => {
const options = {
url: "https://api.dialogflow.com/v1/entities/{YOUR_ENTITY_ID}",
method: "GET",
headers: {
"Authorization": "Bearer {YOUR_DEVELOPER_ACCESS_TOKEN}",
"Content-type": "application/json; charset=UTF-8"
},
json: true
};
request(options, (error, response, body) => {
if (error) {
console.log(error);
app.ask("Error occurred.");
} else {
const nameListFromEntity = body.entries.map(x => {
return x.value;
});
const index = Math.floor(Math.random() * 10);
const name = nameListFromEntity[index];
app.ask(name);
}
});
};
const actionMap = new Map();
actionMap.set("input.welcome", inputWelcome);
app.handleRequest(actionMap);
};