使用api.ai将contextOut数组附加到firebase webhook响应

时间:2017-07-24 11:24:22

标签: firebase api-ai

我尝试将contextOut数组添加到我的firebase webhook为api.ai as documented here提供的响应中。

我的简化firebase代码如下:

exports.myEndPoint = functions.https.onRequest((req, res) => {

  const assistant = new Assistant({ request: req, response: res });

  function firstHandler(assistant) {
    const i = Math.floor(Math.random() * 200);

    // I want to append contextOut to the response for api.ai here 

    assistant.ask(message generated using i);
  }

  function secondHandler(assistant) {

    // I want to retrieve i from the request from api.ai here

    assistant.tell(a different message generated using i from earlier);
  }

  const actionMap = new Map();
  actionMap.set('first', firstHandler);
  actionMap.set('second', secondHandler);
  assistant.handleRequest(actionMap);

});

我在创建助手实例之前尝试了res.contextOutres.data以及在制作实例后assistant.contextOutassistant.data。我无法在api.ai控制台中看到json响应中的变量。

谁能告诉我我失踪了什么?谢谢!

1 个答案:

答案 0 :(得分:0)

我为我的问题设法解决了问题。

以下是简化代码:

exports.myEndPoint = functions.https.onRequest((req, res) => {

  const assistant = new Assistant({ request: req, response: res });

  function firstHandler(assistant) {
    const i = Math.floor(Math.random() * 200);
    const obj = {
      "index": i,
    }

    // I create my own context here.         

    assistant.setContext('index', 2, obj);

    assistant.ask(message generated using i);
  }

  function secondHandler(assistant) {

    // Use the created context here.

    const context = assistant.getContext('index');
    const i = context.parameters.index;

    assistant.tell(a different message generated using i from earlier);
  }

  const actionMap = new Map();
  actionMap.set('first', firstHandler);
  actionMap.set('second', secondHandler);
  assistant.handleRequest(actionMap);

});

解决方案使用文档found here

我很确定我也可以使用api.ai的现有上下文来做这个但我还没有测试过。