How to set a DialogFlow session's timezone?
Asked Answered
F

1

6

When I use a 'date' entity in a Dialogflow intent, I want it to interpret today's date according to a specific timezone.

That time zone might vary between sessions, as my web-client is aware of the user's specific timezone, and can convey this in the request, or when first initiating the session.

Right now, Dialogflow just uses UTC, so if the user is in California, UTC-7 and it is January 1st, 9pm in California, it is 4am on January 2nd in UTC, and so the user saying today is interpreted as January 2nd instead of the desired January 1st.

Fright answered 17/4, 2018 at 16:30 Comment(0)
C
5

According to the docs, there's a timezone parameter when sending a query:

POST /query sample
HTTPCURL
curl \
https://api.dialogflow.com/v1/query?v=20150910 \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer YOUR_CLIENT_ACCESS_TOKEN' \
-d '{
  "contexts": [
    "shop"
  ],
  "lang": "en",
  "query": "I need apples",
  "sessionId": "12345",
  "timezone": "America/New_York"
}'

So if you know the user's timezone, you only need to send it on every request.


API V2

For an API V2 agent, the timezone can be send in queryParams.timeZone

const sessionClient = new dialogflow.SessionsClient({
    keyFilename: '/path/to/google.json'
});
const sessionPath = sessionClient.sessionPath('[PROJECT_ID]', '[SESSION_ID]');

const request = {
    session: sessionPath,
    queryInput: {
        text: {
            text: 'query',
            languageCode: 'en'
        }
    },
    queryParams: {
        timeZone: 'America/New_York'
    }
};

sessionClient.detectIntent(request)
   .then(res => console.log(res))
   .catch(err => console.error(err))
Cherub answered 17/4, 2018 at 16:34 Comment(7)
Thank you! Just saw that :). Will test it and report back.Fright
Did you test it?Cherub
Indeed. Thanks! Do you know if one can set a reference datetime, so tests can be repeatable? @Marcos CasagrandeFright
@MarcosCasagrande is this valid for dialogFlow API v2? I have upgraded to V2. can we do this in dialogFlow V2 APIsWeiner
@QadirHussain it is possible, I updated my answer to reflect how to send it in API V2Cherub
@MarcosCasagrande I have done like above but I am still facing timeZone issue. described over here. github.com/dialogflow/dialogflow-nodejs-client-v2/issues/100Weiner
its working now as per above. I was using VPN connection at that time may be that was affecting.Weiner

© 2022 - 2024 — McMap. All rights reserved.