Search code examples
dialogflow-estelegramchatbottelegram-bot

DialogFlow with Telegram: How to receive an image and save it along with the conversation


I'm developing a chat bot for Telegram using DialogFlow, but I can't go through two topics, and I can't find the documentation for them.

The flow of the conversation, is the user answer some closed questions and send an image. How do I get this image? And to save her along with the other answers?

The answers need to be saved as a form/survey and not as a conversation history.


Solution

  • I have a similar setup in my chatbot. I store the answers in a Firebase database.

    In order to interact with the Firestore Database you should implement a Fulfillment

    You can see a guide on how to implement Firebase for DialogFlow here

    Here you can see a sample of my code. In general lines after setting up the connection to the Firebase database you just want to map your intents to your functions using intentMap.set.

    As you said you are using closed answers you can set intets to handle the responses and each "final" intent will trigger a different function that will write a different message to the db.

    To write the response to the Firesbase database you just only need to implement admin.database().ref().push().set({}) with the information and the desired structure.

    In my example I also store the conversation Id from the chat payload and the date.

    '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()
          });
       }
    });
    

    Also if you want to store images with the user responses you can implement the getfile method from the telegram api and store the image code or the image itself