我正在使用DialogFlow + Firebase修补Google上的操作。我们的想法是为默认AoG所不具备的物联网设备进行自定义构建。
就广播和电视而言,目前有两种意图:
1)频道:它接收3个参数:device_name(自定义实体),device_action(自定义实体)和值。
例如:请将无线电频道更改为22.3
或者将电视频道改为22
2)卷:它包含3个参数:device_name(自定义实体),device_action(自定义实体)和值。
例如:请将收音机音量更改为99 或者将电视的音量改为22
问题是代理似乎无法区分两者。
执行错误了吗?
//编辑2018年5月9日:
index.js
// Copyright 2016, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
const util = require('util');
const functions = require('firebase-functions');
const {
dialogflow,
Suggestions,
BasicCard,
Button,
SimpleResponse,
} = require('actions-on-google');
const {values, concat, random, randomPop} = require('./util');
const responses = require('./responses');
/** Dialogflow Contexts {@link https://dialogflow.com/docs/contexts} */
const AppContexts = {
FACT: 'choose_fact-followup',
CATS: 'choose_cats-followup',
XXX: 'choose_xxx-followup',
};
/** Dialogflow Context Lifespans {@link https://dialogflow.com/docs/contexts#lifespan} */
const Lifespans = {
DEFAULT: 5,
};
const app = dialogflow({
debug: true,
init: () => ({
data: {
// Convert array of facts to map
facts: responses.categories.reduce((o, c) => {
o[c.category] = c.facts.slice();
return o;
}, {}),
cats: responses.cats.facts.slice(), // copy cat facts
},
}),
});
/**
* Greet the user and direct them to next turn
* @param {DialogflowConversation} conv DialogflowConversation instance
* @return {void}
*/
app.intent('Unrecognized Deep Link Fallback', (conv) => {
const response = util.format(responses.general.unhandled, conv.query);
const suggestions = responses.categories.map((c) => c.suggestion);
conv.ask(response, new Suggestions(suggestions));
});
// redirect to the intent handler for tell_fact
app.intent('choose_fact', 'tell_fact');
// Say a fact
app.intent('tell_fact', (conv, {category}) => {
const {facts, cats, xxx} = conv.data;
if (values(facts).every((c) => !c.length)) {
// If every fact category facts stored in conv.data is empty,
// close the conversation
return conv.close(responses.general.heardItAll);
}
const categoryResponse =
responses.categories.find((c) => c.category === category);
const fact = randomPop(facts[categoryResponse.category]);
if (!fact) {
const otherCategory =
responses.categories.find((other) => other !== categoryResponse);
const redirect = otherCategory.category;
const parameters = {
category: redirect,
};
// Add facts context to outgoing context list
conv.contexts.set(AppContexts.FACT, Lifespans.DEFAULT, parameters);
const response = [
util.format(responses.transitions.content.heardItAll, category, redirect),
];
// If cat facts not loaded or there still are cat facts left
if (cats.length) {
response.push(responses.transitions.content.alsoCats);
}
response.push(responses.general.wantWhat);
conv.ask(concat(...response));
conv.ask(new Suggestions(otherCategory.suggestion));
if (cats.length) {
conv.ask(new Suggestions(responses.cats.suggestion));
}
return;
}
const {factPrefix} = categoryResponse;
// conv.ask can be called multiple times to have the library construct
// a single response itself the response will get sent at the end of
// the function or if the function returns a promise, after the promise
// is resolved.
conv.ask(new SimpleResponse({
speech: concat(factPrefix, fact),
text: factPrefix,
}));
conv.ask(responses.general.nextFact);
conv.ask(new BasicCard({
title: fact,
image: random(responses.content.images),
buttons: new Button({
title: responses.general.linkOut,
url: responses.content.link,
}),
}));
console.log('hiwwxxxxxww thi is aaron');
conv.ask(responses.general.suggestions.confirmation);
});
// Redirect to the intent handler for tell_cat_fact
app.intent('choose_cats', 'tell_cat_fact');
// Say a cat fact
app.intent('tell_cat_fact', (conv) => {
const {cats} = conv.data;
console.log('this is cats data' + {cats});
const fact = randomPop(cats);
if (!fact) {
conv.contexts.delete(AppContexts.FACT);
conv.contexts.delete(AppContexts.CATS);
conv.ask(responses.transitions.cats.heardItAll);
return conv.ask(responses.general.suggestions.confirmation);
}
const {factPrefix, audio} = responses.cats;
// conv.ask can be called multiple times to have the library construct
// a single response itself. The response will get sent at the end of
// the function or if the function returns a promise, after the promise
// is resolved.
const sound = util.format(audio, random(responses.cats.sounds));
conv.ask(new SimpleResponse({
// <speak></speak> is needed here since factPrefix is a SSML string
// and contains audio.
speech: `<speak>${concat(factPrefix, sound, fact)}</speak>`,
text: factPrefix,
}));
conv.ask(responses.general.nextFact);
conv.ask(new BasicCard({
title: fact,
image: random(responses.cats.images),
buttons: new Button({
title: responses.general.linkOut,
url: responses.cats.link,
}),
}));
console.log('hiwwxxxxxww thi is aaron');
conv.ask(responses.general.suggestions.confirmation);
});
//say a tv channel
app.intent('volume', (conv, {device_name,device_action, value}) => {
var no_device_name = device_name;
var no_value = value;
var no_device_action = device_action;
var this_device_value = util.inspect(value, false, null);
var this_device_name = util.inspect(device_name, false, null);
var this_device_action = util.inspect(device_action, false, null);
console.log(this_device_action[0]);
if (no_device_name[0] == 'channel'){
console.log('inside tv but CHANNEL');
}
else{
console.log('VOLUME');
}
console.log('THIS IS VOL');
console.log(no_device_action[0]);
conv.ask(`Alright, ${device_name} VOLUM is now ${value}! `);
console.log('inside volume ' + value[0]);
console.log('inside volume' + device_name[0]);
console.log('inside volume' + device_action[0]);
console.log('hiwwxxxxxww thi is aaron');
});
//say a tv channel
app.intent('channel', (conv, {device_channel_name, device_channel_action, channel_value}) => {
console.log('THIS IS CHANNEL');
conv.ask(`Alright, ${device_channel_name} CHANNEL is now ${channel_value}! `);
console.log('inside CHANNEL ' + channel_value[0]);
console.log('inside CHANNEL' + device_channel_name[0]);
console.log('inside CHANNEL' + device_channel_action[0]);
console.log('hiwwxxxxxww thi is aaron');
});
app.intent('no_input', (conv) => {
const repromptCount = parseInt(conv.arguments.get('REPROMPT_COUNT'));
if (repromptCount === 0) {
conv.ask(`What was that?`);
} else if (repromptCount === 1) {
conv.ask(`Sorry I didn't catch that. Could you repeat yourself?`);
} else if (conv.arguments.get('IS_FINAL_REPROMPT')) {
conv.close(`Okay let's try this again later.`);
}
});
// The entry point to handle a http request
exports.factsAboutGoogle = functions.https.onRequest(app);
&#13;
答案 0 :(得分:0)
如果您有一个示例短语,其中该短语的一部分设置为表示参数,则该短语的该部分可以采用任何值,该参数被定义为。 &#34;已解决的价值&#34;列只会在您的示例短语中显示已解决的内容。
所以在你的&#34;频道&#34;意图,短语&#34;电视频道到3&#34;也可以匹配&#34;无线电音量到4&#34;自从&#34; tv&#34;和&#34;无线电&#34;是device_name
实体和&#34;频道&#34;和&#34;音量&#34;都是device_action
实体。
您有几个解决方案:
您可以将它们变成一个接受所有短语的Intent,并在您的webhook中检查device_action
的值,看看您应该做什么。
您可以将它们保持为单独的Intent并完全删除device_action
参数。你不需要它们。只需使用各种示例短语中的同义词,学习系统就哪些同义词与哪个Intent一起工作进行训练。
如果您仍然担心同义词,或者它仍然有意义,请将它们设为不同的实体类型。
例如,您可能希望拥有&#34;频道&#34;和&#34;预设&#34;作为同一实体类型下的不同实体。您关心的是差异,因为它会改变您对待号码的方式,但您仍在更改频道。 &#34;卷&#34;将是一个不同的实体,因为它代表了完全不同的东西。
在您的评论中,您询问了设备的个性化别名。你没有询问不止一种具有不同名称的设备类型(所以你想要处理&#34; tv1&#34;和#34; tv2&#34;使用相同的Intent,但是能够检测到使用上面的选项(3)可以最好地处理这两种方法,但方式略有不同。
如果你想要多个&#34; tv&#34;,每个都是相同的实体类型(例如,&#34;设备电视&#34;),但每一个都是不同的实体价值。它可能看起来像这样:
&#34;但等等!&#34;我听到你说&#34;如果用户有三台电视怎么办?还是九点?我必须设置所有这些吗?如果他们想把一台卧室电视叫做“卧室电视”怎么办呢?另一个是电视&#39;?&#34;
这表明你会将电视及其别名的数量保存在某种数据库中(无论如何你都需要这样做 - 你可能需要将他们的电视映射到一些独特的设备ID以实际打开它们),以及当用户与您的座席进行通话时,请使用User Entity进行更新。
用户实体具有仅为该用户设置的用户特定名称和别名。当用户第一次与您的代理商交谈时,您将使用Dialogflow API设置它们。如果您使用的是V1,则会使用/userEntities端点。如果您使用的是V2,则会使用projects.agent.sessions.entityTypes资源。