In DialogFlow when date is displayed it come with YYYY-MM-DD HH-MM-SS format how to convert to DD-MM-YYYY format?

Viewed 706

The response given by the bot for date given by user for example 20th july is 2021-07-20T12:00:00+05:30. How to convert this to 2021-07-20?

2 Answers

You can use the Dialogflow’s Inline Editor to extract the date part from the default date format that is provided by Dialogflow Essentials. The Inline Editor uses Google Cloud Functions, so to use the Inline Editor you need to set up billing first.

You can refer to the below mentioned steps:

  1. Create an Intent and add Training phrases to it and match entity types to @sys.date for date and @sys.time for time. enter image description here

  2. Enable Fulfillment for that intent by clicking on the “Enable webhook call for this intent”

  3. Go to the Fulfillment section and enable the Inline Editor.

  4. Use the below mentioned code in the Inline Editor.

Code:

'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
 
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 welcome(agent) {
    agent.add(`Welcome to my agent!`);
  }
 
  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
  }
  
  function slot(agent){
    
    const time=agent.parameters.time.split(`T`)[1].split('+')[0];
    const date=agent.parameters.date.split(`T`)[0];
    agent.add(`your table is booked on  ` + date + ` ,`  + time);
  }

  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('hotel', slot);
  agent.handleRequest(intentMap);
});


enter image description here

As we are using System entities for defining the date and time, System entities have their default formats.

So the value that we have extracted will not be shown in the parameters field as the entity for that parameter is defined with System entities. However we can store the extracted values in our database.

I think using the webhook we can do that. Here is some code reference.

Prerequisites:

  • Create new intent and add a event like 'dateConverter'
  • enter image description here
  • Add response in newly created intent with Converted date: $date (Here in date we have formatted date)
  • Enable the webhook for the intent for which date is being captured.
  • Configured webhook in fulfillment section.

Here is the sample Node code. const express = require('express'); const fetch = require('node-fetch');

const app = express()
const port = process.env.PORT || 3000;
app.listen(port, () => {
    console.log(`Starting server at ${port}`);
});

app.use(express.json())

app.post('/date-converter', async (req, res) => {
console.log("Dialogflow: Received a POST request");
if (!req.body) return res.sendStatus(400)
if (req.body.queryResult.parameters.hasOwnProperty('given-date')){
  let date = req.body.queryResult.parameters['given-date']
  let dateFormatter = date.split('T')[0].split("-").reverse().join("-");
  let responseObj = {
   "followupEventInput": {
     "name": "dateConverter",
     "parameters": {
      "date": dateFormatter
    }
   },
  "source": ""
  }
  return res.json(responseObj)
  }
  })

So when your main intent in which user has provided the date intent trigger then it'll call the webhook and convert the date format. After converting the date format it'll internally call the intent which has a event dateConverter with the specific parameter that we have defined in the webhook response.(Here we have define the date).

I think by using this we can convert the date from the YYYY-MM-DD HH-MM-SS to DD-MM-YYYY or whatever format you want.

NOTE: For this sample user captured date should be stored in the given-date parameter. If you have different name then you need to change the name in the above code snippet.

Result: enter image description here

Let me know if you face any issue.

Related