dialogflow-es

what are the changes in HTTP request format while migrating from Dialogflow v1 to Dialogflow v2


The below python code is what I am using as a http request to query dialogflow-V1.I want to migrate to v2.But I am facing issues when I changed parameters with respect to v2 naming.

headers = {
'Authorization': 'Bearer CLIENT_ACCESS_TOKEN'
}

params = (
    ('v', '20150910'),
    ('lang', 'en'),
    ('query', 'query_str'),
    ('sessionId', "10000"), #UNIQUE ID
    ('timezone', 'America/New_York'),
    )

response = requests.get('https://api.dialogflow.com/v1/query', headers=headers, params=params)

Help me migrate the above DFv1 code to DFv2 format below is what I tried

headers = {
'Authorization': 'Bearer #Client access token'
}

params = (
    ('queryInput.text.languageCode ', 'en'),
    ('detectIntent', 'query_str'),
    ('session', 'projects/your-project-id/agent/sessions/session-id'),  
    ('queryParams.timeZone ', 'America/New_York'),
    )

response = requests.get('https://api.dialogflow.com/v2/query', headers=headers, params=params)

I am not sure of what exactly is session id in DFv2.The response here is not is any format as expected.Below is the response

b'Dialogflow@-moz-keyframes blink {0%{opacity:1;} 50%{opacity:0;} 100%{opacity:1;}}@-webkit-keyframes blink {0%{opacity:1;} 50%{opacity:0;} 100%{opacity:1;}}@-ms-keyframes blink {0%{opacity:1;} 50%{opacity:0;} 100%{opacity:1;}}@keyframes blink {0%{opacity:1;} 50%{opacity:0;} 100%{opacity:1;}}@-moz-keyframes fadein {from{opacity:0;} to{opacity:1;}}@-webkit-keyframes fadein{from{opacity:0;} to{opacity:1;}}@-ms-keyframes fadein {from{opacity:0;} to{opacity:1;}}@keyframes fadein {from{opacity:0;} to{opacity:1;}}#loading-screen{background: #fff; position: fixed; top: 0; left: 0; height: 100%; width: 100%; z-index: 999999; opacity: 1; filter: alpha(opacity=100); -webkit-transition: opacity 500ms ease; transition: opacity 500ms ease;}#loading-screen #logo {display: block; width: 109px; height: 39px; background-repeat: no-repeat; background-image: url(\'https://www.gstatic.com/dialogflow-console/common/assets/img/logo@2x-black.png\'); background-size: contain; position: absolute; top: 50%; left: 50%; margin: -20px 0 0 -55px; -moz-transition:all 1s ease-in-out; -webkit-transition:all 1s ease-in-out; -o-transition:all 1s ease-in-out; -ms-transition:all 1s ease-in-out; transition:all 1s ease-in-out; -moz-animation:blink normal 2s infinite ease-in-out; -webkit-animation:blink normal 2s infinite ease-in-out; -ms-animation:blink normal 2s infinite ease-in-out; animation:blink normal 2s infinite ease-in-out;}#loading-screen #assistant-preview {width: 400px; height: 200px; position: absolute; top: 50%; left: 50%; margin: -120px 0 0 -200px; text-align: center; background: white; -webkit-animation: fadein 500ms; -moz-animation: fadein 500ms; -ms-animation: fadein 500ms; animation: fadein 500ms;}#loading-screen #assistant-preview .logo {width: 46px; height: 46px; background-repeat: no-repeat; background-image: url(\'https://www.gstatic.com/dialogflow-console/common/assets/img/logo_icon_48dp.png\'); background-size: contain; position: absolute; left:45%;}#loading-screen #assistant-preview .title {margin-top: 100px; font-size: 23px;}#loading-screen #assistant-preview .progress-container {margin: 25px 50px;}#loading-screen #assistant-preview .progress-container md-progress-linear, #loading-screen #assistant-preview .progress-container md-progress-linear .md-container, #loading-screen #assistant-preview .progress-container md-progress-linear .md-bar, #loading-screen #assistant-preview .progress-container md-progress-linear .md-dashed {height: 2px;}window.INTERNALIZED = true; window.DF_FLAGS = {email: "", backend: "https:\\/\\/api.dialogflow.com", opBackend: "https:\\/\\/dialogflow.clients6.google.com", apiKey: "xxxxx",}; window.saveAs = undefined; window.i18n = undefined; window.d3 = undefined; window.addStyleString = function(str){var node = document.createElement(\'style\'); node.innerHTML = str; document.head.appendChild(node);};window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);}gtag(\'js\', new Date()); gtag(\'config\', \'UA-98266305-2\'); gtag(\'config\', \'UA-98266305-8\');Updating Actions on Google...var loadDeferredStyles = function() {var addStylesNode = document.getElementById("deferred-styles"); var replacement = document.createElement("div"); replacement.innerHTML = addStylesNode.textContent; document.body.appendChild(replacement); addStylesNode.parentElement.removeChild(addStylesNode);}; var raf = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame; if (raf) {raf(function() { window.setTimeout(loadDeferredStyles, 0); });} else {window.addEventListener(\'load\', loadDeferredStyles);}if (window.location.hash.endsWith(\'assistant_preview\')) {setTimeout(function () {document.getElementById(\'assistant-preview\').style.display = \'block\';}, 200);} else {document.getElementById(\'logo\').style.display = \'block\';}window.WIZ_global_data = {"nQyAE":{"EtBwpb":"false","He6Wuc":"false","xJW1Ve":"true","tm22M":"false","aVzR2d":"false","ox9Ggd":"false","Dk1LPb":"false","CXZ8Fd":"false","c9LSR":"true","tvMknd":"true","eiw7Vc":"false","tLPClf":"false","lAsnAb":"false","buULxf":"false","adjIR":"false"}};var AF_initDataKeys = []\n; var AF_dataServiceRequests = {}; var AF_initDataChunkQueue = []; var AF_initDataCallback; var AF_initDataInitializeCallback; if (AF_initDataInitializeCallback) {AF_initDataInitializeCallback(AF_initDataKeys, AF_initDataChunkQueue, AF_dataServiceRequests);}if (!AF_initDataCallback) {AF_initDataCallback = function(chunk) {AF_initDataChunkQueue.push(chunk);};}'

Please help me resolve this. Thanks


Solution

  • The move from v1 to v2 is not that straightforward. There are a number of things you need to address. The "query" API call has changed to the detectIntent API call. This has a lot of consequences.

    The first is that the URL path has dramatically changed. Consult the reference page linked above, but it needs to be something more like

    https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/agent/sessions/SESSION_ID:detectIntent
    

    It also needs to be an HTTPS POST request - you can't use GET anymore.

    Moving to POST also means that the request body is more complicated. While v1 had most of the parameters in the top level of a JSON structure, v2 has these in nested JSON objects. So although the migration guide says that "timezone" is replaced with "queryParams.timeZone", this really means that your JSON response would need to have a structure including

    {
      ...
      "queryParams": {
        ...
          "timeZone": "America/NewYork"
        ...
      }
      ...
    }
    

    As you note in the comments, the biggest change is how you need to do auth. Instead of using a single shared secret, as you did with v1, you need to get a Google Cloud Service Account and use the secret from that account to generate an access token, which usually has a limited time to live. The good news is that the Google Cloud libraries or SDK will generate (and sometimes manage) the access token for you.