dialogflow / dialogflow-fulfillment-nodejs

Dialogflow agent fulfillment library supporting v1&v2, 8 platforms, and text, card, image, suggestion, custom responses
Apache License 2.0
598 stars 281 forks source link

HOW TO QUERY DATA FROM FIRESTORE AND CREATE FLEX PAYLOAD USING DIALOGFLOW INLINE EDITOR PROPERLY #297

Open narate1973 opened 4 years ago

narate1973 commented 4 years ago

I have tried to query data from firestore and use the data to create carousel payload to response the line message. But nothing occur on line even if it's in the intent.

This's how the data looks like. It's know the designed looks suck. I'm new with it. image

This occur when the flow goes in the intent. image

And this is the code. ` 'use strict';

const functions = require('firebase-functions'); const { WebhookClient, Payload } = require('dialogflow-fulfillment'); const { Card, Suggestion } = require('dialogflow-fulfillment');

const admin = require("firebase-admin"); admin.initializeApp({ credential: admin.credential.applicationDefault(), databaseURL: 'https://.firebaseio.com' }); const db = admin.firestore(); db.settings({ timestampsInSnapshots: true }); 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 test(agent) { let pproduct = String(request.body.queryResult.parameters.search);

return db.collection(pproduct).doc('get').get().then(doc => {
  const bubble0 = bubble(doc.data().img0,doc.data().price0,doc.data().brand0,doc.data().link0);
  const bubble1 = bubble(doc.data().img1,doc.data().price1,doc.data().brand1,doc.data().link1);
  const bubble2 = bubble(doc.data().img2,doc.data().price2,doc.data().brand2,doc.data().link2);
  const bubble3 = bubble(doc.data().img3,doc.data().price3,doc.data().brand3,doc.data().link3);
  const bubble4 = bubble(doc.data().img4,doc.data().price4,doc.data().brand4,doc.data().link4);

  const flexMessage = carousel(bubble0, bubble1, bubble2, bubble3, bubble4);
  let payload = new Payload(`LINE`, flexMessage, { sendAsMessage: true });
  agent.setFollowupEvent('ExtendTimeOut');
  agent.add(payload);
  agent.add(pproduct);
});

} function carousel(bubble0, bubble1, bubble2, bubble3, bubble4) { const payloadJson = { "type": "flex", "altText": "Flex Message", "contents": { "type": "carousel", "contents": [bubble0, bubble1, bubble2, bubble3, bubble4] } }; return payloadJson; } function bubble(img0, price0, brand0, product0, link0) { let color = '#7fbe26'; if (brand0 == 'Tesco') { color = "#2B51CC"; } else if(brand0=='BigC'){ color = '#7fbe26'; }else{ color= '#5dbcd2'; } const bubb = { "type": "bubble", "direction": "ltr", "header": { "type": "box", "layout": "vertical", "contents": [ { "type": "text", "text": brand0, "size": "xl", "color": color, "align": "center", "weight": "bold" } ] }, "hero": { "type": "image", "url": img0, "size": "full", "aspectRatio": "1.51:1", "aspectMode": "fit", "action": { "type": "uri", "uri": link0 } }, "body": { "type": "box", "layout": "vertical", "contents": [ { "type": "text", "text": product0, "size": "xl", "align": "center", "weight": "bold" }, { "type": "text", "text": 'ราคา'+' '+price0 +' '+'บาท', "align": "center" } ] } }; return bubb; } let intentMap = new Map(); intentMap.set('pricesearch', test); agent.handleRequest(intentMap); }); `