从Dialogflow v1迁移到Dialogflow v2时HTTP请求格式的变化是什么

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从Dialogflow v1迁移到Dialogflow v2时HTTP请求格式的变化是什么相关的知识,希望对你有一定的参考价值。

下面的python代码是我用作查询dialogflow-V1的http请求所使用的。我想迁移到v2。但是当我更改有关v2命名的参数时,我遇到了问题。

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)

帮助我将上面的DFv1代码迁移为DFv2格式以下是我尝试过的

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)

我不确定DFv2中的会话ID到底是什么。这里的响应不是预期的任何格式。下面是响应

b'Dialogflow @ -moz-关键帧闪烁0%opacity:1; 50%opacity:0; 100%opacity:1; @-webkit-关键帧闪烁0%opacity:1; 50%opacity:0; 100%opacity:1; @-ms-关键帧闪烁0%opacity:1; 50%opacity:0; 100%opacity:1; @关键帧闪烁0%opacity:1; 50%opacity:0; 100%opacity:1; @-moz-keyframes从opacity:0;变为opacity:1; @ -webkit-keyframes淡入from opacity:0;到opacity:1; @-ms-keyframes淡入from opacity:0;到opacity:1; @ keyframes淡入from  opacity:0;至opacity:1;#loading-screen 背景:#fff;位置:固定最高:0;左:0;高度:100%;宽度:100%; z-index:999999;不透明度:1;过滤器:alpha(opacity = 100); -webkit-transition:不透明度500ms缓解;过渡:不透明度500毫秒缓解;#loading-screen #logo display:block;宽度:109px;高度:39px;背景重复:不重复;背景图片:url(\'https://www.gstatic.com/dialogflow-console/common/assets/img/logo@2x-black.png \');背景大小:包含;位置:绝对;最高:50%;左:50%;边距:-20px 0 0 -55px; -moz-transition:全部1s缓入; -webkit-transition:全部1s轻松输入; -o-transition:所有1s缓入; -ms-transition:所有1s缓入;过渡:所有1s轻松进出; -moz动画:闪烁正常2s无限缓入; -webkit-animation:闪烁正常2s无限缓入; -ms-animation:闪烁正常2s无限缓入;动画:眨眼正常2s无限缓和;#loading-screen#assistant-preview width:400px;高度:200px;位置:绝对;最高:50%;左:50%;边距:-120px 0 0 -200px;文本对齐:居中;背景:白色; -webkit-animation:淡入500ms; -moz动画:淡入500ms; -ms动画:淡入500ms;动画:fadein 500ms;#loading-screen#assistant-preview .logo width:46px;高度:46像素;背景重复:不重复;背景图片:url(\'https://www.gstatic.com/dialogflow-console/common/assets/img/logo_icon_48dp.png \');背景大小:包含;位置:绝对;左:45%;#loading-screen#assistant-preview .title margin-top:100px;字体大小:23px;#loading-screen#辅助预览.progress-container 边距: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 = 电子邮件:“”,后端:“ https:\\ / \\ / api.dialogflow.com”,opBackend:“ https:\\ / \\ / dialogflow.clients6.google.com”,apiKey: “ AIzaSyD1dO8oRagJkmtkSJ9oLI289jIT8M4Zk5s”,; window.saveAs =未定义; window.i18n =未定义; window.d3 =未定义; window.addStyleString = function(str)var node = document.createElement(\'style \'); node.innerhtml = str; document.head.appendChild(node);; window.dataLayer = window.dataLayer || [];函数gtag()dataLayer.push(arguments); gtag(\'js \',new Date()); gtag(\'config \',\'UA-98266305-2 \'); gtag(\'config \',\'UA-98266305-8 \');在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);其他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);;'

请帮助我解决此问题。谢谢

答案

从v1到v2的迁移并非如此简单。您需要解决许多问题。 “查询” API调用已更改为detectIntent API调用。这会带来很多后果。

首先是URL路径发生了巨大变化。请参阅上面链接的参考页,但它应该更像

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

它还必须是HTTPS POST请求-您不能再使用GET。

移到POST也意味着请求主体更加复杂。尽管v1在JSON结构的顶层具有大多数参数,但v2在嵌套的JSON对象中具有这些参数。因此,尽管migration guide表示“时区”已替换为“ queryParams.timeZone”,但这实际上意味着您的JSON响应将需要具有以下结构:


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

以上是关于从Dialogflow v1迁移到Dialogflow v2时HTTP请求格式的变化是什么的主要内容,如果未能解决你的问题,请参考以下文章

Firebase功能不能打印UTF8特殊字符

如何从网页访问 Dialogflow V2 API?

从 AnySlider v1.2 迁移到最新版本 (v1.8.17)

从 v1 到 2 的 Apollo GraphQL 迁移

从v1迁移到react-native-navigation v2后的不变违例

从 Ionic v1 迁移到 Ionic v3 或 v4?