【问题标题】:DialogFlow with Telegram: How to receive an image and save it along with the conversationDialogFlow with Telegram:如何接收图像并将其与对话一起保存
【发布时间】:2019-08-22 21:33:40
【问题描述】:

我正在使用 DialogFlow 为 Telegram 开发一个聊天机器人,但我无法浏览两个主题,也找不到它们的文档。

对话的流程,是用户回答一些封闭的问题并发送图像。 我如何得到这个图像? 并与其他答案一起拯救她?

答案需要保存为表格/调查,而不是对话历史记录。

【问题讨论】:

  • 您到底想达到什么目的?将答案保存为“表格/调查”,它非常广泛。您想将答案存储在像 firebase 这样的数据库中吗?或者您希望它们在电子表格中?另外,如果您能描述一下您已经尝试过的方法会有所帮助
  • 它可以是任何选项,也许电子表格是更好的选择。问题是,我今天可以与机器人交谈,但我不知道如何获得这些答案并将它们存储在某个地方

标签: dialogflow-es telegram chatbot telegram-bot


【解决方案1】:

我的聊天机器人中有类似的设置。我将答案存储在 Firebase 数据库中。

为了与 Firestore 数据库交互,您应该实现 Fulfillment

您可以查看有关如何为 DialogFlow here 实施 Firebase 的指南

在这里您可以看到我的代码示例。一般来说,在建立与 Firebase 数据库的连接后,您只想使用 intentMap.set 将您的意图映射到您的函数。

正如您所说,您使用的是封闭式答案,您可以设置整数来处理响应,并且每个“最终”意图将触发一个不同的函数,该函数将向数据库写入不同的消息。

要将响应写入 Firesbase 数据库,您只需使用信息和所需结构实现 admin.database().ref().push().set({})

在我的示例中,我还存储了来自聊天有效负载的对话 ID 和日期。

'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
//const DialogflowApp = require('actions-on-google').DialogflowApp;

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

admin.initializeApp({
  credential : admin.credential.applicationDefault(),
  databaseURL: 'ws://YOURDATABASE.firebaseio.com/'
});

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 userId;
  let conv = agent.conv();
  const ROOTREF = admin.database().ref();

  const actions = new Map();
  let intentMap = new Map();
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('NoTunel',  handleWriteToDbNoTunnel(agent));
  agent.handleRequest(intentMap);

  function assignConv(agent){
    userId = agent.parameters.UserId;
    return admin.database().ref('Users/'+ userId).set({
        Apellido:"XXXX",
        Nombre:"XXXX",
        chatId:333, 
  });}

  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
  }      
  var token = "YOUR TOKEN HERE";
  var url = "https://api.telegram.org/bot"+ token;
  function handleWriteToDbNoTunnel(agent){
    const Dia = new Date();
    if(matricula !== "")
    return admin.database().ref('Limpieza/').push().set({
        chatId: request.body.queryResult.outputContexts[3].parameters.telegram_chat_id+'"',
        Field1: answer1,
        Field2: answer2,
        day: day.getTime()
      });
   }
});

此外,如果您想存储带有用户响应的图像,您可以从telegram api 实现getfile 方法并存储图像代码或图像本身

【讨论】:

    猜你喜欢
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    相关资源
    最近更新 更多