在DialogFlow中显示日期时它带有YYYY-MM-DD HH-MM-SS格式如何转换为DD-MM-YYYY格式?

问题描述

机器人对用户给出的日期(例如 7 月 20 日)给出的响应是 2021-07-20T12:00:00+05:30。如何将其转换为 2021-07-20?

解决方法

我认为使用 webhook 我们可以做到这一点。这是一些代码参考。

先决条件:

  • 创建新的意图并添加一个像“dateConverter”这样的事件
  • enter image description here
  • 使用转换日期在新创建的意图中添加响应:$date(此处为我们已格式化日期的日期)
  • 为捕获日期的意图启用网络钩子。
  • 在履行部分配置了 webhook。

这是示例节点代码。 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)
  }
  })

因此,当用户在其中提供日期意图触发器的主要意图时,它将调用 webhook 并转换日期格式。转换日期格式后,它会在内部调用具有事件 dateConverter 的意图,其中包含我们在 webhook 响应中定义的特定参数。(这里我们定义了日期)。

我认为通过使用它,我们可以将日期从 YYYY-MM-DD HH-MM-SS 转换为 DD-MM-YYYY 或您想要的任何格式。

注意:对于此示例,用户捕获的日期应存储在 given-date 参数中。如果您有不同的名称,则需要更改上述代码片段中的名称。

结果: enter image description here

如果您遇到任何问题,请告诉我。

,

您可以使用 Dialogflow 的内联编辑器从 Dialogflow Essentials 提供的默认日期格式中提取 date 部分。 Inline Editor 使用 Google Cloud Functions,因此要使用内联编辑器,您需要先设置结算。

您可以参考下面提到的步骤:

  1. 创建一个 Intent 并向其添加训练短语,并将实体类型匹配到 @sys.date(日期)和 @sys.time(时间)。 enter image description here

  2. 点击“为此意图启用网络钩子调用”

  3. 转到 Fulfillment 部分并启用内联编辑器。

  4. 在内联编辑器中使用下面提到的代码。

代码:

'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

由于我们使用系统实体来定义日期和时间,因此系统实体具有其默认格式。

因此,我们提取的值不会显示在参数字段中,因为该参数的实体是使用系统实体定义的。 但是,我们可以将提取的值存储在我们的数据库中。