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

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

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

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

先决条件:

  • 创建新意图并添加事件,例如 'dateConverter'
  • 在新创建的意图中添加带有转换日期的响应:$date(我们在此处设置了日期格式)
  • 为捕获日期的意图启用 webhook。
  • 在 fulfillment 部分配置了 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 响应中定义的特定参数。(这里我们定义了 date)。

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

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

结果:

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

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

您可以参考以下步骤:

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

  2. 通过单击 “为此意图启用 webhook 调用”

    为该意图启用实现
  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);
});


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

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