Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a DialogFlow session's timezone?

When I use a 'date' entity in a Sialogflow 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.

like image 644
eran Avatar asked Apr 17 '18 16:04

eran


1 Answers

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))
like image 102
Marcos Casagrande Avatar answered Sep 30 '22 05:09

Marcos Casagrande