首页 > 解决方案 > 我如何使用 node.js 从商店 firebase 显示图像

问题描述

我尝试通过在 Dialogflow 上使用 Fulfillment(使用 Node.js 的代码)开发聊天机器人并与 Firebase 连接。

我在 Firestore 中存储了一张图片。我想展示那张图片,但我是 Node.js、Firebase 的新手,我不知道。

我已经从 Firestore 阅读了关于上传/下载 URL 的文档,但我仍然不明白它的概念。

你能向我解释一下如何搜索或搜索的措辞吗?

我想从存储中显示此图像

错误:未定义存储。

'use strict';

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

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase); 

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

var httpsReference = storage.refFromURL('https://firebasestorage.googleapis.com/v0/b/jenniebot2-94120.appspot.com/o/images%2FPicture5.jpg?alt=media&token=c853681b-2a72-45e4-ac7a-016f62396fa8');

function bodyMassIndex(agent) {
  let weight = 80;
  let height = 170/100;
  let bmi = (weight / (height * height)).toFixed(2);
  let result = "none";

  if (bmi < 18.5) {
    result = "xs";
  } else if (bmi >= 18.5 && bmi <= 22.9) {
    result = "s";
  } else if (bmi >= 23 && bmi <= 24.9) {
    result = "m";
  } else if (bmi >= 25 && bmi <= 29.9) {
    result = "l";
  } else if (bmi > 30) {
    result = "xl";
  }

  return admin.firestore().collection('bmi').doc(result).get()
  .then(doc => {agent.add(doc.data().description);});
}
function welcome(agent) {
agent.add(`Welcome to my agent!`);
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Bmi', bodyMassIndex);    
agent.handleRequest(intentMap);
});

标签: node.jsfirebasedialogflow-eschatbot

解决方案


这个问题很简单。我只是把它比它应该的更难。只需使用 Firebase Storage 的 URL 并将其放入 LINE API(.JSON) 中。然后使用 dialogflow 进行响应。

{
  "type": "image",
  "originalContentUrl": "/URL",
  "previewImageUrl": "/URL",
  "animated": false
}

推荐阅读