首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google Assistant的事实应用程序

Google Assistant的事实应用程序
EN

Stack Overflow用户
提问于 2017-07-14 04:58:59
回答 1查看 247关注 0票数 1

我正在摆弄我的谷歌主页,并创建了一个可以读取树懒信息的应用程序。我已经使用API.AI创建了代理,我将我的函数托管在Firebase上,并通过webhook将其连接到API.AI。你要求谷歌告诉你一个关于树懒的事实,它会回答你是想听一个“有趣的事实”还是“科学的事实”。你的回答决定了谷歌将为你读出什么样的事实。

在API.AI上测试时,我得到了默认的失败响应,但当我看一眼JSON时,它显然是在解析事实类别。我的javascript代码是基于他们的教程正在使用的Google的示例“关于Google的事实”应用程序。下面是来自API.AI测试的JSON以及我的tellFact()函数。

如果JSON清楚地显示它正在解析一个正确的类别,为什么我会遇到失败条款?

JSON

{ "id": "2b920a5b-0d17-4c5a-9ac1-18071f078464", "timestamp": "2017-07-13T20:43:33.307Z", "lang": "en", "result": { "source": "agent", "resolvedQuery": "tell me something scientific about sloths", "action": "tell.fact", "actionIncomplete": false, "parameters": { "fact-category": "science" }, "contexts": [ { "name": "_actions_on_google_", "parameters": { "fact-category.original": "scientific", "fact-category": "science" }, "lifespan": 100 } ], "metadata": { "intentId": "ca4fa7f1-aceb-4867-b7c3-cf16d1ce4d79", "webhookUsed": "true", "webhookForSlotFillingUsed": "false", "webhookResponseTime": 195, "intentName": "tell_fact" }, "fulfillment": { "speech": "Sorry, I didn't understand. I can tell you fun facts or science facts about sloths. Which one do you want to hear about?", "messages": [ { "type": 0, "speech": "Sorry, I didn't understand. I can tell you fun facts or science facts about sloths. Which one do you want to hear about?" } ], ...

index.js

代码语言:javascript
复制
const App = require('actions-on-google').ApiAiApp;
const functions = require('firebase-functions');
const TELL_FACT = 'tell.fact';

// API.AI parameter names
const CATEGORY_ARGUMENT = 'category';
const FACT_TYPE = {
  FUN: 'fun',
  SCIENCE: 'science'
};
const FUN_FACTS = new Set([...]);
const SCIENCE_FACTS = new Set([...]);

...

exports.factsAboutSloths = functions.https.onRequest((request, response) => {
  const app = new App({ request, response });
  console.log('Request headers: ' + JSON.stringify(request.headers));
  console.log('Request body: ' + JSON.stringify(request.body));

  // Greet the user and direct them to next turn
  function unhandledDeepLinks(app) {
    if (app.hasSurfaceCapability(app.SurfaceCapabilities.SCREEN_OUTPUT)) {
      app.ask(app.buildRichResponse()
        .addSimpleResponse(`Welcome to Facts about Sloths! I'd really rather not talk about ${app.getRawInput()}.` +
        `Wouldn't you rather talk about Sloths? I can tell you a fun fact or a science fact about sloths.` +
        `Which do you want to hear about?`).addSuggestions(['Fun', 'Science']));
    } else {
      app.ask(`Welcome to Facts about Sloths! I'd really rather not talk about ${app.getRawInput()}.` +
      `Wouldn't you rather talk about Sloths? I can tell you a fun fact or a science fact about sloths.` +
      `Which do you want to hear about?`, NO_INPUTS);
    }
  }

  // Say a fact
  function tellFact(app) {
    let funFacts = app.data.funFacts ? new Set(app.data.funFacts) : FUN_FACTS;
    let scienceFacts = app.data.scienceFacts ? new Set(app.data.scienceFacts) : SCIENCE_FACTS;

    if (funFacts.size === 0 && scienceFacts.size === 0) {
      app.tell('Actually it looks like you heard it all. Thanks for listening!');

      return;
    }

    let factCategory = app.getArgument(CATEGORY_ARGUMENT);

    if (factCategory === FACT_TYPE.FUN) {
      let fact = getRandomFact(funFacts);
      if (fact === null) {
        if (app.hasSurfaceCapability(app.SurfaceCapabilities.SCREEN_OUTPUT)) {
          let suggestions = ['Science'];

          app.ask(app.buildRichResponse()
            .addSimpleResponse(noFactsLeft(app, factCategory, FACT_TYPE.SCIENCE))
            .addSuggestions(suggestions));
        } else {
          app.ask(noFactsLeft(app, factCategory, FACT_TYPE.SCIENCE), NO_INPUTS);
        }
        return;
      }

      let factPrefix = 'Sure, here\'s a fun fact. ';
      app.data.funFacts = Array.from(funFacts);

      if (app.hasSurfaceCapability(app.SurfaceCapabilities.SCREEN_OUTPUT)) {
        let image = getRandomImage(SLOTH_IMAGES);
        app.ask(app.buildRichResponse()
          .addSimpleResponse(factPrefix)
          .addBasicCard(app.buildBasicCard(fact)
            .addButton(LINK_OUT_TEXT, WIKI_LINK)
            .setImage(image[0], image[1]))
          .addSimpleResponse(NEXT_FACT_DIRECTIVE)
          .addSuggestions(CONFIRMATION_SUGGESTIONS));
      } else {
        app.ask(factPrefix + fact + NEXT_FACT_DIRECTIVE, NO_INPUTS);
      }
      return;

    } else if (factCategory === FACT_TYPE.SCIENCE) {
      let fact = getRandomFact(scienceFacts);

      if (fact === null) {
        if (app.hasSurfaceCapability(app.SurfaceCapabilities.SCREEN_OUTPUT)) {
          let suggestions = ['Fun'];

          app.ask(app.buildRichResponse()
            .addSimpleResponse(noFactsLeft(app, factCategory, FACT_TYPE.FUN))
            .addSuggestions(suggestions));
        } else {
          app.ask(noFactsLeft(app, factCategory, FACT_TYPE.FUN), NO_INPUTS);
        }
        return;
      }

      let factPrefix = 'Okay, here\'s a science fact. ';
      app.data.scienceFacts = Array.from(scienceFacts);
      if (app.hasSurfaceCapability(app.SurfaceCapabilities.SCREEN_OUTPUT)) {
        let image = getRandomImage(SLOTH_IMAGES);
        app.ask(app.buildRichResponse()
          .addSimpleResponse(factPrefix)
          .addBasicCard(app.buildBasicCard(fact)
            .setImage(image[0], image[1])
            .addButton(LINK_OUT_TEXT, WIKI_LINK))
          .addSimpleResponse(NEXT_FACT_DIRECTIVE)
          .addSuggestions(CONFIRMATION_SUGGESTIONS));
      } else {
        app.ask(factPrefix + fact + NEXT_FACT_DIRECTIVE, NO_INPUTS);
      }
      return;

    } else {
      // Conversation repair is handled in API.AI, but this is a safeguard
      if (app.hasSurfaceCapability(app.SurfaceCapabilities.SCREEN_OUTPUT)) {
        app.ask(app.buildRichResponse()
          .addSimpleResponse(`Sorry, I didn't understand. ` +
          `I can tell you fun facts or science facts about sloths. ` +
          `Which one do you want to hear about?`)
          .addSuggestions(['Fun', 'Science']));
      } else {
        app.ask(`Sorry, I didn't understand. ` +
        `I can tell you fun facts or science facts about sloths. ` +
        `Which one do you want to hear about?`, NO_INPUTS);
      }
    }
  }

  // Say they've heard it all about this category
  function noFactsLeft(app, currentCategory, redirectCategory) {
    let parameters = {};

    parameters[CATEGORY_ARGUMENT] = redirectCategory;
    // Replace the outgoing facts context with different parameters
    app.setContext(FACTS_CONTEXT, DEFAULT_LIFESPAN, parameters);
    let response = `Looks like you've heard all there is to know about the ${currentCategory} facts of sloths. ` +
    `I could tell you about its ${redirectCategory} instead. So what would you like to hear about?`;

    return response;
  }

  let actionMap = new Map();
  actionMap.set(UNRECOGNIZED_DEEP_LINK, unhandledDeepLinks);
  actionMap.set(TELL_FACT, tellFact);

  app.handleRequest(actionMap);
});

tell.fact意图屏幕截图

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-14 12:15:03

啊哈!一个很容易被忽视的偷偷摸摸的问题。

在API.AI中,您已经将参数命名为“事实类别”。

您正在使用以下命令请求该参数

代码语言:javascript
复制
 let factCategory = app.getArgument(CATEGORY_ARGUMENT);

并将CATEGORY_ARGUMENT定义为

代码语言:javascript
复制
 const CATEGORY_ARGUMENT = 'category';

因此,它陷入了错误条件,因为您将获得名为"category“的参数,但该参数尚未设置,因此factCategory在任何情况下都是未定义的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45090785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档