我试图显示轮播从Firebase获取数据,Card工作正常,但显示轮播不起作用,但是让conv = agent.conv();始终为Null。但是,我遵循此处的指示:Using the Actions on Google library integration:。请不要生气,我正在尝试学习。 谢谢
'use strict';
const functions = require('firebase-functions');
const {WebhookClient, Card} = require('dialogflow-fulfillment');
const requestLib = require('request');
const {dialogflow, Carousel}= require('actions-on-google');
const app = dialogflow({debug: true});
// Constant for image URLs
const IMG_URL_AOG = 'https://developers.google.com/actions/images/badges' +
'/XPM_BADGING_GoogleAssistant_VER.png';
// Constant for image URLs
const IMG_URL_GOOGLE_ALLO = 'https://allo.google.com/images/allo-logo.png';
const IMG_URL_GOOGLE_HOME = 'https://lh3.googleusercontent.com' +
'/Nu3a6F80WfixUqf_ec_vgXy_c0-0r4VLJRXjVFF_X_CIilEu8B9fT35qyTEj_PEsKw';
const IMG_URL_GOOGLE_PIXEL = 'https://storage.googleapis.com/madebygoog/v1' +
'/Pixel/Pixel_ColorPicker/Pixel_Device_Angled_Black-720w.png';
const IMG_URL_MEDIA = 'http://storage.googleapis.com/automotive-media/album_art.jpg';
const MEDIA_SOURCE = 'http://storage.googleapis.com/automotive-media/Jazz_In_Paris.mp3';
// initialise DB connection
const admin = require('firebase-admin');
admin.initializeApp({
// credential:admin.credential.applicationDefault(),
databaseURL:'ws://test-catWorldXXXXX.com/'
});
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function handelModel (agent){
const chosenModel = agent.parameters["toyota-models"].toLowerCase();
let conv = agent.conv();
if (chosenModel == "corolla") {
return admin.database().ref('corolla').once("value")
.then(function(snapshot) {
snapshot.forEach(function(model) {
var value = model.val();
agent.add("Nombre: " + value.nombre);
Conv shows valor NULL...
agent.add("conv " + conv);
conv.ask(new Carousel({
items: {
// Add the first item to the carousel
[SELECTION_KEY_ONE]: {
synonyms: [
'synonym of title 1',
'synonym of title 2',
'synonym of title 3',
],
title: 'Title of First Carousel Item',
description: 'This is a description of a carousel item.',
image: new Image({
url: IMG_URL_AOG,
alt: 'Image alternate text',
}),
},
// Add the second item to the carousel
[SELECTION_KEY_GOOGLE_HOME]: {
synonyms: [
'Google Home Assistant',
'Assistant on the Google Home',
],
title: 'Google Home',
description: 'Google Home is a voice-activated speaker powered by ' +
'the Google Assistant.',
image: new Image({
url: IMG_URL_GOOGLE_HOME,
alt: 'Google Home',
}),
},
// Add third item to the carousel
[SELECTION_KEY_GOOGLE_PIXEL]: {
synonyms: [
'Google Pixel XL',
'Pixel',
'Pixel XL',
],
title: 'Google Pixel',
description: 'Pixel. Phone by Google.',
image: new Image({
url: IMG_URL_GOOGLE_PIXEL,
alt: 'Google Pixel',
}),
},
// Add last item of the carousel
[SELECTION_KEY_GOOGLE_ALLO]: {
title: 'Google Allo',
synonyms: [
'Allo',
],
description: 'Introducing Google Allo, a smart messaging app that ' +
'helps you say more and do more.',
image: new Image({
url: IMG_URL_GOOGLE_ALLO,
alt: 'Google Allo Logo',
}),
},
},
}));
agent.add(conv);
});
});
}
else {
return admin.database().ref('yaris').once("value")
.then(function(snapshot) {
snapshot.forEach(function(model) {
var value = model.val();
const a11yText = 'Google Assistant Bubbles';
const googleUrl = 'https://google.com';
title: value.nombre,
imageUrl: 'https://developers.google.com/actions/assistant.png',
text: 'This is the body text of a card. You can even use line\n breaks and emoji! ',
buttonText: 'This is a button',
buttonUrl: 'https://assistant.google.com/'
})
);
});
});
}
}
// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('ask-models', handelModel);
agent.handleRequest(intentMap);
});
答案 0 :(得分:1)
我在https://github.com/dialogflow/dialogflow-fulfillment-nodejs/issues/37#issuecomment-437036370这个问题上发现了一些东西:
就像jorgecasar指出的那样,您需要设置agent.requestSource, 否则,agent.conv()返回null
const agent = new WebhookClient({ request, response });
agent.requestSource = agent.ACTIONS_ON_GOOGLE;
然后agent.conv()不会返回null
答案 1 :(得分:1)