'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.Когда это намерение запускается успешным голосовым вводом, он отвечает текстом / голосом от агента и обновляет поле stage
в соответствующей базе данных Firebase.
Теперь другое внешнее приложение, при некоторых обстоятельствах, обновляет это поле stage
в базе данных firebase.
Нет этой части в коде выполнения, когда изменяются
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 Google что-то говорить, как в этом случае Thank you for granting me the access.
.
ПРИМЕЧАНИЕ: Мне не нужно увольнение (извините за путаницу ранее).Мне просто нужно, чтобы голосовой агент Google Home подтвердил это изменение / триггер.
Теперь, когда я просматриваю логи, я вижу, что они ломаются здесь agent.add('Thank you for granting me the access.');
И журнал ошибок нескольконапример:
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
at transition.on (/user_code/index.js:36:22)
at /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)
at EventList.raise (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:9727:17)
at EventQueue.raiseQueuedEventsMatchingPredicate_ (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:9681:41)
at EventQueue.raiseEventsForChangedPath (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:9665:14)
at Repo.onDataUpdate_ (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:12770:26)
at PersistentConnection.onDataPush_ (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:12070:18)
at PersistentConnection.onDataMessage_ (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:12064:18)
Таким образом, остается основной вопрос: Как заставить агента говорить / обновлять текстовый ответ и подтверждать изменение поля этой БД.