【问题标题】:how do I connect Google Actions and Watson?如何连接 Google Actions 和 Watson?
【发布时间】:2019-04-03 20:47:53
【问题描述】:

我正在尝试将 Watson 连接到 Google Actions,我的函数在 firebase 中,当我只测试与 Watson 的连接时,它可以工作。但是当我尝试在 Google 控制台中对其进行测试时,它会关闭项目。

这是我在 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);`

【问题讨论】:

    标签: node.js actions-on-google ibm-watson


    【解决方案1】:

    问题在于assistant.message() 将回调函数作为参数来处理响应,但在处理异步调用时,actions-on-google 库希望您返回 Promise。您需要将此异步调用包装在 Promise 中,然后将此 Promise 返回到 a-o-g 库,以便它知道等待回调完成。

    我没有测试过代码,但我怀疑这样的东西应该可以工作:

    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();
      });
    
    });
    

    【讨论】:

    • 谢谢,它解决了这个问题,但现在我收到了 -> "TypeError: Cannot read property 'text' of undefined"
    • 这听起来像是一个不同的问题,我建议您发布详细信息,包括您当前的代码、您遇到的错误以及新的 Stack Overflow 问题的位置。显示“响应”的值也可能很有用。如果此答案有帮助,请随时支持和/或接受它。
    猜你喜欢
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    相关资源
    最近更新 更多