简体   繁体   English

如何连接 Google Actions 和 Watson?

[英]how do I connect Google Actions and Watson?

I'm trying to connect Watson to Google Actions, my function is in firebase, and when I test only the connection with Watson, it works.我正在尝试将 Watson 连接到 Google Actions,我的函数在 firebase 中,当我仅测试与 Watson 的连接时,它可以工作。 But when I try to test it in Google console, it closes the project.但是当我尝试在 Google 控制台中对其进行测试时,它会关闭该项目。

This is my code in Firebase Cloud Functions:这是我在 Firebase Cloud Functions 中的代码:

const {actionssdk} = require('actions-on-google');
const functions = require('firebase-functions');

const app = actionssdk({debug: true});
var AssistantV1 = require('watson-developer-cloud/assistant/v1');

app.intent('actions.intent.MAIN', (conv) => {
    conv.ask('Olá, como posso lhe ajudar?');
});


app.intent('actions.intent.TEXT', (conv, input) => {
    var AssistantV1 = require('watson-developer-cloud/assistant/v1');
    var assistant = new AssistantV1({
        username: '###############',
        password: '###############',
        url:'###############',
        version: '2018-07-10'
    });

    assistant.message(
    {
        workspace_id: '###############',
        input: { text: result },
        headers: {'Custom-Header': 'custom',
        'Accept-Language': 'custom'
        }
    },
    function(response) {
        conv.ask(response.output.text[0]);
    }
    );
});

exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);`

The issue is that assistant.message() takes a callback function as a parameter to handle the response, but the actions-on-google library expects you to return a Promise when you are dealing with async calls.问题是assistant.message()将回调函数作为参数来处理响应,但是 actions-on-google 库希望您在处理异步调用时返回 Promise。 You will need to wrap this async call in a Promise and then return this Promise to the aog library so it knows to wait for the callback to complete.您需要将此异步调用包装在 Promise 中,然后将此 Promise 返回给 aog 库,以便它知道等待回调完成。

I haven't tested the code, but I suspect something like this should work:我还没有测试过代码,但我怀疑这样的事情应该可以工作:

return new Promise( (resolve, reject) => {
  assistant.message({
    workspace_id: '###############',
    input: { text: result },
    headers: {
      'Custom-Header': 'custom',
      'Accept-Language': 'custom'
    }
  },
  function(response) {
    conv.ask(response.output.text[0]);
    resolve();
  });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM