您可以使用 fulfillment 定义 Action 的对话, 它是以网络钩子形式部署的代码,其中包含您的 Dialogflow 代理的 对话逻辑。当用户执行这些操作时,执行方式会告知 Action 该执行什么操作 请求。
对于 Interactive Canvas,您的执行方式还会传达
Web 应用添加到 Google 助理。你可以使用HtmlResponse让 Google 助理执行下列操作:
呈现您的 Web 应用HtmlResponse 还可以为 data 提供更新,
您的 Web 应用自定义逻辑用于对您的 Web 应用进行更改。
本页介绍了如何在执行方式中使用 HtmlResponse,以及常规操作
使用此响应类型的准则。
HTML 响应
要将有关你的 Web 应用的信息转发给 Google 助理,你必须添加
HtmlResponse
。HtmlResponse 可以包含网址
Web 应用的数据以及用于更新 Web 应用的数据。发送
HtmlResponse 时,会执行以下步骤:
- 所匹配的 intent 的执行方式会向设备发送
HtmlResponse。 - 设备使用
HtmlResponse中的网址加载 Web 应用。 dataJSON 载荷通过回调传递给 Web 应用。您的对话型 Action 会发送新的
HtmlResponse来发送更新或 加载新状态。
执行方式示例
以下内容摘录自示例
执行方式代码展示了如何实现 HtmlResponse:
const functions = require('firebase-functions');
const {dialogflow, HtmlResponse} = require('actions-on-google');
const app = dialogflow({debug: true});
app.intent('welcome', (conv) => {
conv.ask('Welcome! Do you want me to change color or pause spinning?');
conv.ask(new HtmlResponse({
url: 'https://your-web-app.com',
}));
});
// map of human speakable colors to color values
const tints = {
red: 0xFF0000,
green: 0x00FF00,
blue: 0x0000FF,
};
app.intent('color', (conv, {color}) => {
if (color in tints) {
conv.ask(`Ok, I changed my color to ${color}. What else?`);
conv.ask(new HtmlResponse({
data: {
tint: tints[color],
},
}));
return;
}
conv.ask(`Sorry, I don't know that color. What else?`);
conv.ask(new HtmlResponse({
data: {
query: conv.query,
},
}));
});
app.intent('start', (conv) => {
conv.ask(`Ok, I'm spinning. What else?`);
conv.ask(new HtmlResponse({
data: {
spin: true,
},
}));
});
app.intent('pause', (conv) => {
conv.ask(`Ok, I paused spinning. What else?`);
conv.ask(new HtmlResponse({
data: {
spin: false,
},
}));
});
app.intent('restart game', (conv) => {
conv.ask(new HtmlResponse({
data: {
command: 'RESTART_GAME',
},
}));
});
exports.conversation = functions.https.onRequest(app);
welcome intent
在上面的代码段中,welcome intent 的 fulfillment 会发送一个
将 HtmlResponse 替换为 Web 应用的网址。Google 助理会收到这项内容
并在该地址加载 HTML 和 JavaScript。
...
app.intent('welcome', (conv) => {
conv.ask('Welcome! Do you want me to change color or pause spinning?');
conv.ask(new HtmlResponse({
url: 'https://your-web-app.com',
}));
});
...
其他 intent
其他 intent 的执行方式中的 HtmlResponse 会传递变量值
(在示例中为 tint 或 spin)附加到 Web 应用。Web 的自定义逻辑
应用使用这些值来更新元素(动画、颜色等):
...
app.intent('start', (conv) => {
conv.ask(`Ok, I'm spinning. What else?`);
conv.ask(new HtmlResponse({
data: {
spin: true,
},
}));
});
...
准则和限制
请谨记 HtmlResponse 的以下准则和限制
构建 fulfillment:
- 执行方式中的每个 intent 都必须包含一个
HtmlResponse。如果 intent 不包含HtmlResponse,您的 Web 应用将关闭。 - 您只需要在发送到的第一个 intent 中添加您的 Web 应用网址
用户(这通常是
Welcomeintent)。 - “
HtmlResponse”的大小不得超过 50kb。