如何使dialogflow Google代理响应和确认firebase的字段更改 [英] how to make a dialogflow google agent respond and acknowledge on firebase's field change

查看:82
本文介绍了如何使dialogflow Google代理响应和确认firebase的字段更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {WebhookClient} = require('dialogflow-fulfillment');

process.env.DEBUG = 'dialogflow:*'; // enables lib debugging statements

admin.initializeApp({
    credential: admin.credential.applicationDefault(),
    databaseURL: "https://my_db.firebaseio.com/",
});

var database = admin.database();
var transition = database.ref('/stage');


exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
    console.log('Inside :) yessssssss !');
    const agent = new WebhookClient({ request, response });


    function moveToStage (agent) {
        transition.set('2');
        agent.add('Welcome to xx console. Please accept the notification on your watch');
    }


    transition.on('value', (snapshot) => {
        console.log("Reading value succesfully from firebase");
        console.log(snapshot.val());
        if(snapshot.val() == '3'){
            agent.add('Thank you for granting me the access.');
            // OR
            // response.setHeader('Content-Type','applicaiton/json');
            // response.send(JSON.stringify({"fulfillmentText": 'Thank you for granting me the access.'}));
        }
    });


    let intentMap = new Map();
    intentMap.set('welcome_and_ask_to_sync', moveToStage);
    agent.handleRequest(intentMap);
});

我有一个意图 welcome_and_ask_to_sync webhook已激活。
当成功输入语音触发该Intent时,它会响应来自代理的文本/语音,并在相应的Firebase数据库中更新字段 stage

I have an intent welcome_and_ask_to_sync, which has webhook activated. When that Intent is fired by a successful voice input, it reponds with a text/voice from the agent and updates a field stage in the respective firebase DB.

现在,在某些情况下,另一个外部应用程序更新了Firebase数据库中 stage 字段的内容。

Now another external application, under some circumstences, updates that stage field in the firebase DB.

在履行代码中没有这部分,变化的地方

No this this part in the fulfillment code, wtahces that change

transition.on('value', (snapshot) => {
    console.log("Reading value succesfully from firebase");
    console.log(snapshot.val());
    if(snapshot.val() == '3'){
        agent.add('Thank you for granting me the access.');
        // OR
        // response.setHeader('Content-Type','applicaiton/json');
        // response.send(JSON.stringify({"fulfillmentText": 'Thank you for granting me the access.'}));
    }
});

这里的目的是让Google首页说些什么,例如在这种情况下感谢您授予我访问权限。

The intention here is to then make google home speak something, like in thsi case Thank you for granting me the access. .

注意: 我不需要被解雇(对不起,很抱歉造成混乱)。我只需要Google家庭语音代理即可确认此更改/触发。

现在,当我查看日志时,我看到它在这里中断了 agent.add('谢谢您授予我访问权限。');

Now when I watch the logs, I see it breaks here agent.add('Thank you for granting me the access.');

错误日志有点类似:


错误:发送标头后无法设置标头。
在ServerResponse.OutgoingMessage.setHeader(_http_outgoing.js:356:11)
在transition.on(/user_code/index.js:36:22)
在/ user_code / node_modules / firebase -admin/node_modules/@firebase/database/dist/index.node.cjs.js:4465:22
at exceptionGuard(/ user_code / node_modules / firebase-admin / node_modules / @ firebase / database / dist / index。 node.cjs.js:691:9)
在EventList.raise(/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:9727:17)
在EventQueue.raiseQueuedEventsMatchingPredicate_(/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:9681:41)
在EventQueue.raiseEventsForChangedPath(/ user_code / node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:9665:14)
在Repo.onDataUpdate_(/ user_code / node_modules / firebase-admin / node_modules / @ firebase / database / dist / index.node.cjs.js:12770:26)
在PersistentConnection.onDataPush_(/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:12070:18)在PersistentConnection.onDataMessage_(/ user_code / node_modules / firebase -admin/node_modules/@firebase/database/dist/index.node.cjs.js:12064:18)

因此,基本问题仍然存在: 我如何使座席发言/更新文本响应并确认数据库的字段更改。

推荐答案

简短的答案是您不能-当然不是您的操作方式。

The short answer is that you can't - certainly not the way you're doing it.

一个响应从您的Webhook发送回Dialogflow,HTTPS连接已关闭,任何进一步的答复都将生成您看到的错误。

Once a response is sent from your webhook back to Dialogflow, the HTTPS connection is closed, and any further replies will generate the error that you see.

此外,使用的会话模型AoG和Dialogflow的观点是,用户必须始终发起每一轮对话。此时AoG不可能宣布某些内容。

Furthermore, the conversational model used by AoG and Dialogflow is that the user must always initiate each round of the conversation. It is not possible for AoG to "announce" something at this point. That would be considered somewhat invasive.

您可以发送通知,当用户确认通知后,它将重新开始对话。但是,通知仅发送到智能手机,而不发送给扬声器。因此,这可能无法满足您的需求。

You can send a notification through Actions on Google, which would re-start the conversation when the user acknowledged the notification. However, notifications are only sent to smartphones - not to speakers. So this may not meet your needs.

如果您希望在首次发送后很快就进行一些更新,那么您可能想看看是否可以使用媒体响应来使对话与保持音乐一起进行等待更多更新。当保持音乐结束时,它将向您的操作发送一个事件,您可以重新启动保持音乐或回复。在这种情况下,您将不会使用 .on()方法来进行更新,而是需要在每次Media播放结束时检查是否有更新被未发送的更新。

If you're expecting just a few updates that take place fairly quickly after the initial send, you may want to see if you can use a Media Response to keep the conversation going with "hold music" while you wait for more updates. When the "hold music" ends, it will send an event to your Action, and you can either re-start the hold music or reply. In this case, you wouldn't use the .on() method for updates to come in, but would have to check each time the Media finishes playing to see if there have been updates that are unsent.

这篇关于如何使dialogflow Google代理响应和确认firebase的字段更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆