【问题标题】:DialogFlow sets 12:00:00 as a beginning of the day if time isn't specified in text如果文本中未指定时间,DialogFlow 将 12:00:00 设置为一天的开始
【发布时间】:2020-10-29 21:49:23
【问题描述】:

我正在学习使用 DialogFlow 构建聊天机器人。作为一个例子,我创建了简单的意图来报告我当前的体重。我在我的服务器上构建了端点来接收数据作为履行请求,以将其保存到数据库中。

作为数据参数,我将权重指定为@sys.unit-weight,将日期指定为@sys.date。当我通过“今天”或“明天”指定日期时,日期部分被正确解析,但 DialogFlow 将 12:00:00 添加为时间部分。这有点奇怪,我真的不明白那个时候有什么意义。看起来不正确。

我附上了在 DialogFlow 的控制台中执行意图的屏幕截图以及履行请求负载。如果没有提供时间,有人知道如何在没有时间的情况下获取日期或 00:00:00 的日期吗?

Screen shot of triggering intent by DialogFlow's console

 {
  "responseId": "518945f5-33bf-43a8-86ce-42e422033e6c-425db6e2",
  "queryResult": {
    "queryText": "yes",
    "action": "TrackWeight",
    "parameters": {},
    "allRequiredParamsPresent": true,
    "fulfillmentText": "Your weight log was saved. Do you want anything else?",
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            "Your weight log was saved. Do you want anything else?"
          ]
        }
      }
    ],
    "outputContexts": [
      {
        "name": "projects/chatbot-59dda/agent/sessions/414dfa61-4850-eb27-804a-afea25516ab8/contexts/trackweight-followup",
        "parameters": {
          "weight": {
            "amount": 50,
            "unit": "kg"
          },
          "weight.original": "50 kg",
          "date": "2020-07-09T12:00:00+03:00",
          "date.original": "today"
        }
      },
      {
        "name": "projects/chatbot-59dda/agent/sessions/414dfa61-4850-eb27-804a-afea25516ab8/contexts/__system_counters__",
        "parameters": {
          "no-input": 0,
          "no-match": 0
        }
      }
    ],
    "intent": {
      "name": "projects/chatbot-59dda/agent/intents/c8bafb9d-1c44-401c-877f-9929d4fc23c2",
      "displayName": "Track Weight - yes"
    },
    "intentDetectionConfidence": 1,
    "languageCode": "en"
  },
  "originalDetectIntentRequest": {
    "payload": {}
  },
  "session": "projects/chatbot-59dda/agent/sessions/414dfa61-4850-eb27-804a-afea25516ab8"
}

提前感谢您的帮助!

【问题讨论】:

    标签: dialogflow-es dialogflow-es-fulfillment


    【解决方案1】:

    这是 Dialogflow 报告日期/时间的格式。您需要做的就是将日期和时间分开并将其转换为您需要的格式。 您可以通过agent.parameters.dateagent.parameter.time 访问日期或时间,格式仍然相同。以下是一些可以提供帮助的功能:

    function convertParametersDateTime(date, time){
         return new Date(Date.parse(date.split('T')[0] + 'T' + time.split('T')[1].split('+')[0]));
    }
    
    
    // A helper function that adds the integer value of 'hoursToAdd' to the Date instance 'dateObj' and return a new Data instance.
    function addHours(dateObj, hoursToAdd){
        return new Date(new Date(dateObj).setHours(dateObj.getHours() + hoursToAdd));
    }
    
    // A helper funciton that converts the Date instance 'dateObj' into a string that represents this time in English.
    function getLocaleTimeString(dateObj){
        return dateObj.toLocaleTimeString('en-US', {hour: 'numeric', hour12: true});
    }
    
    // A helper dunction that converts the Date instance 'dateObj' into a string that represents this date in English
    function getLocaleDateString(dateObj){
        return dateObj.toLocaleDateString('en-US', {weekday: 'long', month: 'long', day: 'numeric'});
    }
    

    想法是您可以按'T' 拆分并使用您需要的部分

    【讨论】:

    • 谢谢,@Gray_Rhino!如果我用“T”符号分割日期和时间,我仍然会在时间部分得到 12:00:00。而且我不能依赖它,因为系统不知道是否提供了中午时间或 DialogFlow 将那个时间作为一天的开始。但我喜欢您将日期和时间拆分为不同参数的想法。日期为 sys.date,时间为 sys.time。我会暂时保留这个问题。可能有人会对这种行为进行更深入的解释。
    • 对于您应该使用 agent.parameters.date.split('T')[0] 的日期,它会为您提供正确的日期,并且时间将始终为 12:00:00。但是,您应该使用 agent.parameters.time.split('T')[1] 这将为您提供正确的时间,并且日期将始终为当前日期。例如,当您告诉代理“下周一晚上 8 点做某事”时,您的 agent.parameters.date 将为“2020-07-20T12:00:00 " 并且 agent.parameters.time 将为 "2020-07-13T20:00:00"。看出区别了吗?
    猜你喜欢
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多